【问题标题】:How to check if hotfix KBxxxxxx (example: KB4012212) is installed in the PC or not?如何检查 PC 中是否安装了修补程序 KBxxxxxx(例如:KB4012212)?
【发布时间】:2018-03-06 09:41:51
【问题描述】:

我将(使用 PowerShell 脚本)创建一个表并将结果(正/负)添加到其中。 我有一个文本文件computers.txt,其中列出了所有PC。

这样

CSNAME 修补程序信息 PC1 已安装 PC2 未安装 PC3 已安装 等等。

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

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

【问题讨论】:

  • 你用(Test-Connection $_ -Quiet) 过滤掉 一些 不好的结果,其余的用Get-Hotfix -id KB4012212 -computername $_ 过滤,因为空值不会被传递到 pipt 。如果您想查看更多信息,您需要为每台计算机运行这些命令并保存结果。检查结果,它们没有任何你可以传递空对象或类似 [pscustomobject]@{CSName=$_;HotFixID="N/A"}
  • 您的computers.txtTSV 文件?

标签: powershell if-statement hotfix


【解决方案1】:

我建议解析并处理正负结果(也比管道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' }
}

【讨论】:

    猜你喜欢
    • 2011-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多