Search code examples
powershellhotfix

search for existing multiple KB files on multiple servers


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

Solution

  • 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);
     } }
    }