1

我有一个脚本,它将从文本文件中读取服务器名称,然后搜索可以正常工作的特定 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);
 }
}
4

2 回答 2

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";
  }
}

请享用 ;-)

于 2016-04-22T21:22:48.580 回答
0

您需要将 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);
 } }
}
于 2015-03-18T18:57:35.637 回答