1

I have a script that will read server names in from a text file and then search for a specific KB update filename which works fine.

But what if I want to have each server searched in the serverlist.txt file searched for multiple KB update files? How could I do that?

$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);
 }
}
MickH
  • 13
  • 1
  • 4

2 Answers2

1

Perhaps one query is better for check multi hotfixes

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

enjoy ;-)

cxman
  • 11
  • 1
0

You'll need to set your KB's in an array:

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

And then do a nested 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);
 } }
}
Paul Savage
  • 135
  • 1
  • 1
  • 8