1

如何在输出文件中正确使用 $_?这是我的代码:

get-content computers.txt | 
  Where {$_ -AND (Test-Connection $_ -Quiet)} |
    foreach { Get-Hotfix -computername $_ } | 
      Select CSName,Description,HotFixID,InstalledBy,InstalledOn |
        convertto-csv | out-file "C:\$_.csv"

我正在尝试为文本文件中列出的所有计算机执行 get-hotfix,然后我希望它们以计算机名称作为文件名导出到 CSV。

4

3 回答 3

1

您需要一个管道来处理computers.txt 文件,并在其中嵌套一个管道来处理foreach每台计算机的修补程序列表:

get-content .\computers.txt |
  Where {$_ -AND (Test-Connection $_ -Quiet)} |
    foreach { 
      Get-Hotfix -computername $_ |
        Select CSName,Description,HotFixID,InstalledBy,InstalledOn |
          convertto-csv | out-file "C:\$_.csv" 
    }

编辑:更改computers.txt.\computers.txt,因为这是 powershell 中的本地路径所必需的

于 2012-08-23T01:15:10.790 回答
0

我可以看到:get-content .\computers.txt | 其中 {$_ -AND (Test-Connection $_ -Quiet)} | foreach{ Get-Hotfix -id KB4012212 -computername $_ | 选择 CSName、描述、HotFixID、InstalledBy、InstalledOn | 转换成csv | out-file "C:\$_.csv" } 我只能看到安装了修复程序 (KB4012212) 的 PC。可以看到以下内容

  • CSNAME 修复(Inst/NotInst)

    PC1 已安装

    PC2 Fix未安装

    PC3 未安装

.. .. ETC

于 2017-09-24T15:53:49.360 回答
0

在我使用这个组合之前,我一直在玩这个,但我发现在线上没有任何效果。 

我使用的方法是这个线程,但它太慢了,我想了解更多关于使用作业的信息,所以这最终在 Windows 7 PS Ver 4 上为我工作。所有其他选项要么太慢,要么没有从远程系统。

$VMs = get-content C:\WinVms.txt #Generate your hostnames list however you deem best.
foreach ($vm in $vms)
{
Write-Host "Attempting to get hotfixes on:" $vm
invoke-command -computername $vm -ScriptBlock {start-job -scriptblock {(get-hotfix | sort installedon)[-1]} | wait-job | receive-job} -AsJob
}
start-sleep 60 # give it a minute to complete
get-job | ? { $_.state -eq "Completed"} | receive-job -keep | export-csv c:\temp\win-patch.csv

你也可以像这样检查你的失败: 

get-job | ? { $_.state -eq "Failed"} 
于 2018-08-20T17:53:16.250 回答