Powershell script to check whether multiple KB are installed or not in servers

Welcome Forums General PowerShell Q&A Powershell script to check whether multiple KB are installed or not in servers

This topic contains 10 replies, has 4 voices, and was last updated by

Jon
 
Participant
9 months, 3 weeks ago.

  • Author
    Posts
  • #87911

    Participant
    Points: 0
    Rank: Member

    Powershell Script to find patched and unpatched server!

    I have multiple KB's, I am trying to find whether those KB's have been patched in those servers.

    I have two text file, One contains servers list and other one contains KB list.

    Please find my script below.

    $computers = Get-Content -path "C:\Temp\Shalomon\MultipleKB\server_list.txt"
    $Patch = Get-Content -path "C:\Temp\Shalomon\MultipleKB\kb_list.txt"
    foreach ($computer in $computers)
    {
    if (get-hotfix -id $Patch -ComputerName $computer -ErrorAction SilentlyContinue)
    {
    Add-content "$Patch is Present in $computer" -path "C:\Temp\Shalomon\MultipleKB\Hotfix-Present.txt"
    }
    Else
    {
    Add-content "$Patch is not Present in $computer" -path "C:\Temp\Shalomon\MultipleKB\Missing-Hotfix.txt"
    }
    }

    **************But i get the output as

    KB4025337 KB4025252 is Present in vmpip-xxxx
    KB4025337 KB4025252 is Present in vmpip-yyyy
    KB4025337 KB4025252 is Present in VMPIP-zzzz

    Note: I do not get the servers in which the KB's are missing, Can someone help me out in this code!

  • #87919

    Participant
    Points: 22
    Rank: Member

    Here, use this one. Made it a while back to do exactly that.
    https://www.jeremycorbello.com/index.php/powershell/general-scripts/5-get-mshotfix-ps1

  • #87931
    Jon

    Participant
    Points: 37
    Rank: Member

    Change this:

    get-hotfix -id $Patch -ComputerName $computer -ErrorAction SilentlyContinue

    To:

    get-hotfix -id $Patch -ComputerName $computer -ErrorAction SilentlyContinue -outvariable patchresult

    Then do if your if/else on the $patchresult variable, if the variable is not null then you know you have the patch, if it's null then you don't have it.

    • #94311

      Participant
      Points: 0
      Rank: Member

      @Jon

      I tried doing if/else on $patchresult variable as per your comment. But still it is not working out.
      Can you help me out

      Thanks,
      Shalomon.N

  • #94363

    Participant
    Points: -11
    Rank: Member

    Hi

    If you have -errorAction why not to use Stop on that and use Try, Catch? I don't know what the get-hotfix is and never tried but if this could help or then remove the $patch froeach and change $kb to $patch.

    Below typed here not tested in any way:

    $computers = Get-Content -path "C:\Temp\Shalomon\MultipleKB\server_list.txt"
    $Patch = Get-Content -path "C:\Temp\Shalomon\MultipleKB\kb_list.txt"
    
    $data = foreach ($computer in $computers) {
    
        ForEach ($kb in $patch) {
    
        Try {
            get-hotfix -id $kb -ComputerName $computer -ErrorAction Stop
    
            [PSCustomObject]@{
                Server = $computer
                KB = $kb
                Status = 'Installed'
            }
    
        } Catch {
            [PSCustomObject]@{
                Server = $computer
                KB = $kb
                Status = 'Not Installed'
            }
        }
    }
    }
    
    $data | Export-csv alldata.csv -notypeinformation -delimiter ';' -encoding utf8
    
    

    Regards

    Jake

    • #94398

      Participant
      Points: 0
      Rank: Member

      Hi Jake,

      Thanks for your reply,

      I tried the above script, It has worked out in someway.

      The only issue is that the information which is received in alldata.csv file is not arranged in a proper way.

      PSComputerName;"InstalledOn";"__GENUS";"__CLASS";"__SUPERCLASS";"__DYNASTY";"__RELPATH";"__PROPERTY_COUNT";"__DERIVATION";"__SERVER";"__NAMESPACE";"__PATH";"Caption";"CSName";"Description";"FixComments";"HotFixID";"InstallDate";"InstalledBy";"Name";"ServicePackInEffect";"Status"
      12345-xxxxxx;"8/6/2017 12:00:00 AM";"2";"Win32_QuickFixEngineering";"CIM_LogicalElement";"CIM_ManagedSystemElement";"Win32_QuickFixEngineering.HotFixID=""KB4025337""
      ;;;;;;;;;;;;;;;;;;;;;"Installed"
      ;;;;;;;;;;;;;;;;;;;;;"Not Installed"
      VM123-yyyyyy;"8/6/2017 12:00:00 AM";"2";"Win32_QuickFixEngineering";"CIM_LogicalElement";"CIM_ManagedSystemElement";"Win32_QuickFixEngineering.HotFixID=""KB4025337""
      ;;;;;;;;;;;;;;;;;;;;;"Installed"
      ;;;;;;;;;;;;;;;;;;;;;"Not Installed"
      ;;;;;;;;;;;;;;;;;;;;;"Not Installed"
      VM456-zzzzzz;"8/6/2017 12:00:00 AM";"2";"Win32_QuickFixEngineering";"CIM_LogicalElement";"CIM_ManagedSystemElement";"Win32_QuickFixEngineering.HotFixID=""KB4025252""
      ;;;;;;;;;;;;;;;;;;;;;"Installed"

    • #94401

      Participant
      Points: -11
      Rank: Member

      Hi

      What your kblist.txt includes? I think that needs to be sort out after get-content.

      Regards

      Jake

    • #94405

      Participant
      Points: 0
      Rank: Member

      The KB's which are mentioned in kb_list.txt file, We are using it to check whether those KB's are installed or not in those virtual machines

      kb_list.txt includes the KB number

      Example
      Kb567234
      Kb562435
      Kb724314

  • #94419
    Jon

    Participant
    Points: 37
    Rank: Member

    This works for me

    $computers = gc C:\scripts\computers\computers.txt
    $patches = gc C:\scripts\computers\kb.txt
    
    foreach ($computer in $computers){
     foreach ($patch in $patches){
    
     Get-HotFix -id $patch -ComputerName $computer -OutVariable results -ErrorAction SilentlyContinue
    
     if ($results -ne $null) {
    
     $results | Out-File C:\scripts\computers\report.txt -Append -Force
    
     }
    
     else {
    
     Add-content "$Patch is not Present in $computer" -path "C:\scripts\computers\report2.txt"
    
     }
    
     }
     }
    
    • #94459

      Participant
      Points: 0
      Rank: Member

      Hi Jon,

      Thanks for your timely reply, Above script works fine.

      Just need a help with small modification, I need to catch VM's separately in a text file which i am not able to connect.

      Is it possible to do it in above script?

      Thanks
      Shalomon.N

    • #94480
      Jon

      Participant
      Points: 37
      Rank: Member

      You would probably need to do some sort of try / catch statement read this to find out about it and how to implement it in your code. Also check the 'articles' section at the top and look through the iron scripter preqeuel commentary articles, they have some good examples of try catch in there.

The topic ‘Powershell script to check whether multiple KB are installed or not in servers’ is closed to new replies.