2017-09-25 95 views
0

我将创建(使用PowerShell脚本)一个表并将结果(正面/负面)添加到它。 我有一个文本文件computers.txt,其中列出了所有的PC。如何检查PC中是否安装了修补程序KBxxxxxx(示例:KB4012212)?

像这样

 
CSNAME   Hotfixinfo 
PC1    is installed 
PC2    is not installed 
PC3    is installed 
etc. 

用我的实际脚本我只能看到积极的结果。

Get-Content .\computers.txt | Where { 
    $_ -and (Test-Connection $_ -Quiet) 
} | foreach { 
    Get-Hotfix -Id KB4012212 -ComputerName $_ | 
     Select CSName,HotFixID | 
     ConvertTo-Csv | 
     Out-File "C:\$_.csv" 
} 
+1

你过滤掉与'(测试连接$ _some_坏的结果_ -Quiet)'',其余部分使用'Get-Hotfix -id KB4012212 -computername $ _'作为空值不会被传递到pipt。如果您希望看到更多,则需要为每台计算机运行这些命令并保存结果。检查结果,它没有任何你可以传递一个空对象或类似的东西[pscustomobject] @ {CSName = $ _; HotFixID =“N/A”} – Matt

+0

Your computers.txt是一个TSV文件? – TheIncorrigible1

回答

0

我建议通过分析和处理的阳性和阴性结果(也快于管道ForEach-Object):

:parse ForEach ($Computer in (Get-Content C:\Path\computers.txt)) 
{ 
    If (Test-Connection $Computer -Quiet) 
    { 
     $Result = Get-Hotfix -Id KB4012212 -ComputerName $Computer -ErrorAction 'SilentlyContinue' 

     If ($Result) 
     { 
      $Result | 
       Select-Object -Property CSName,HotFixID | 
       ConvertTo-Csv -NoTypeInformation | 
       Out-File "C:\$Computer.csv" 
      Continue :parse 
     } 
     "`"CSName`",`"HotFixID`"`r`n`"$Computer`",`"NUL`"" | 
      Out-File "C:\$Computer.csv" 
    } Else { Write-Host 'Unable to connect to $Computer' } 
} 
相关问题