首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将.bat脚本转换为MS PowerShell

将.bat脚本转换为MS PowerShell
EN

Stack Overflow用户
提问于 2020-12-21 23:10:07
回答 1查看 62关注 0票数 0

我是一个全新的编码新手,正在将这个脚本转换为Powershell。我对first for循环感到困惑。"skip=2“在做什么,%%A是什么,运行系统信息的意义是什么?

代码语言:javascript
复制
@echo off

SET file=f:\srvapps\webdata\wwwroot\VMuptime\HMIlist.txt
SET OUpath="OU=Packaging,OU=Process Control,OU=XP Workstations Remediated,DC=fcb,DC=corp,DC=anheuser-busch,DC=com"
SET output=f:\srvapps\webdata\wwwroot\VMuptime\uptimes.txt
SET modified=f:\srvapps\webdata\wwwroot\VMuptime\modifiedUptimes.txt

setlocal enabledelayedexpansion1

If exist %output% del /F %output% 
If exist %modified% del /F %modified%
rem pause 
date /T >>%output%
for /F "skip=2"  %%A in (%hmi%) do (
    systeminfo /s %%A | findstr /c:"System Up Time" /c:"Host Name">>%output%
)
rem pause

date /T >>%modified%
for /F "tokens=1-10" %%G in (%output%) do (
    if "%%G"=="Host" (
    set line1=%%I 
    )  else echo !line1!, %%J %%K %%L %%M %%N %%O>>%modified%   
) 
EN

回答 1

Stack Overflow用户

发布于 2021-01-02 07:55:06

这更像是一个.bat文件问题。%hmi%是此处未显示的设置为文件名的变量。然后,skip=2跳过文件中的2行,并将%%A设置为之后文件中的每个主机名。在每个主机名上运行systeminfo,并在输出中搜索正常运行时间。

powershell中的这一部分将是:

代码语言:javascript
复制
$hmi = 'file'
foreach ($a in get-content $hmi | select-object -skip 2) {
  systeminfo /s $a | select-string 'system up time','host name' 
}

当我尝试它的时候,我得不到正常工作时间。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65395214

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档