【问题标题】:search for existing multiple KB files on multiple servers在多个服务器上搜索现有的多个 KB 文件
【发布时间】:2015-03-18 17:43:55
【问题描述】:

我有一个脚本,它会从文本文件中读取服务器名称,然后搜索可以正常工作的特定 KB 更新文件名。

但是,如果我想让每个服务器在 serverlist.txt 文件中搜索多个 KB 更新文件,该怎么办?我怎么能这样做?

$CheckComputers = get-content c:\temp\Path\serverlist.txt
# Define Hotfix to check
$CheckHotFixKB = "KB1234567";
foreach($CheckComputer in $CheckComputers)
{
 $HotFixQuery = Get-HotFix -ComputerName $CheckComputer | Where-Object {$_.HotFixId -eq $CheckHotFixKB} | Select-Object -First 1;
 if($HotFixQuery -eq $null)
 {
  Write-Host "Hotfix $CheckHotFixKB is not installed on $CheckComputer";
 }
 else
 {
  Write-Host "Hotfix $CheckHotFixKB was installed on $CheckComputer on by " $($HotFixQuery.InstalledBy);
 }
}

【问题讨论】:

    标签: powershell hotfix


    【解决方案1】:

    也许一个查询更适合检查多个修补程序

    $NeededHotFixes = @('KB2670838','KB2726535','KB2729094','KB2786081','KB2834140')
    Write-Host "Verify prerequisites hotfixes for IE11."
    $InstalledHotFixes = (Get-HotFix).HotFixId
    $NeededHotFixes | foreach {
      if ($InstalledHotFixes -contains $_) {
         Write-Host -fore Green "Hotfix $_ installed";
       } else {
         Write-Host -fore Red "Hotfix $_ missing";
      }
    }
    

    享受 ;-)

    【讨论】:

      【解决方案2】:

      您需要将 KB 设置为数组:

      $CheckHotFixKB = @(
      "KB1234567"
      "KB5555555"
      "KB6666666"
      "KB7777777"
      )
      

      然后做一个嵌套的foreach:

      foreach($CheckComputer in $CheckComputers)
      {
       foreach ($hotfix in $CheckHotFixKB) {
       $HotFixQuery = Get-HotFix -ComputerName $CheckComputer | Where-Object {$_.HotFixId -eq $hotfix} | Select-Object -First 1;
       if($HotFixQuery -eq $null)
       {
        Write-Host "Hotfix $hotfix is not installed on $CheckComputer";
       }
       else
       {
        Write-Host "Hotfix $hotfix was installed on $CheckComputer on by " $($HotFixQuery.InstalledBy);
       } }
      }
      

      【讨论】:

      • 没问题,如果您不介意,请点击“已回答”复选框:)
      • 一件事是输出将列出每一行中针对每个服务器的所有 KB 文件,因此如果缺少一个 KB 文件,它不会识别出哪个。
      • 忘记更新 if/else 语句中的变量,现在应该好了。
      • 很好,现在看起来很干净,现在应该设置为“已回答”,再次感谢。
      猜你喜欢
      • 2023-04-04
      • 1970-01-01
      • 2018-08-03
      • 2011-04-16
      • 1970-01-01
      • 2014-09-21
      • 1970-01-01
      • 2017-10-02
      • 1970-01-01
      相关资源
      最近更新 更多