Use PowerShell to check if specific hotfix has been installed on servers

In this article we will focus on how to check if specific hotfix has been installed on servers?

Most of you had probably similar issue when after patching cycle something stopped working or some service functionality was limited. Often culprit is one of new installed patches. This is why I created simple function which will help us to search if specific KB has been installed on servers.

Usage:

Get-Updates -Servers (Get-Content "c:\temp\input.txt")
Get-Updates -Servers ADFS01
Get-Updates -Servers ADFS01,ADFS02
Get-Updates -Servers ADFS01 -Days 10
Get-Updates -Servers ADFS01 -KB 1042345
Get-Updates -Servers ADFS01 -KB KB1042345
Get-Updates -Servers ADFS01 -Days 10 -KB 1042345 

Final script:


function Get-Updates {
    param
    (
            [Parameter(Position=0, HelpMessage="Server name", ValueFromPipeline = $true)] 
            $Servers,

            [Parameter(Position=1, HelpMessage="Number of days to search for. Default: Forever", ValueFromPipeline = $true)]
            [int]$Days = 0,

            [Parameter(Position=2, HelpMessage="KB", ValueFromPipeline = $true)] 
            [string]$KB = $null
    )
    
    # Looping servers
    ForEach($Server in $Servers)
    {
            Write-Host "Processing $Server" -ForegroundColor Green
      
            # Checking if server exist
            Try
            {
               $Check = [System.Net.Dns]::GetHostAddresses($Server) 
            }
            Catch
            {
                $_.Exception.Message
                Write-Output "Server does not exist!"
                Write-Host " "
                Continue
            }
            
            # Checking installed update on remote server
            Invoke-Command -ComputerName $Server -ScriptBlock {param($Days,$KB)

                        # Both KB and number of Days specified
                        If(($KB -ne $null) -and ($Days -ne 0))
                        {     
                            Get-HotFix | Where-Object { (($_.HotFixId -like "*$KB*") -and ($_.InstalledOn -gt (Get-Date).AddDays(-$Days))) } | 
                            Select-Object Installedon,Hotfixid,Description,Installedby | Sort-Object InstalledOn | Format-Table -AutoSize                                              
                        }

                        # Only KB specified
                        ElseIf(($KB -ne $null) -and ($Days -eq 0))
                        {
                            Get-HotFix | Where-Object { $_.HotFixId -like "*$KB*" } | 
                            Select-Object Installedon,Hotfixid,Description,Installedby | Sort-Object InstalledOn | Format-Table -AutoSize                                   
                        }
                        
                        # Only number of Days specified
                        ElseIf (($KB -eq $null) -and ($Days -ne 0))
                        {
                            Get-HotFix | Where-Object { $_.InstalledOn -gt (Get-Date).AddDays(-$Days) } | 
                            Select-Object Installedon,Hotfixid,Description,Installedby | Sort-Object InstalledOn | Format-Table -AutoSize
                        }

                        # All updates (no KB or number of Days specified)
                        Else
                        {
                            Get-HotFix | Select-Object Installedon,Hotfixid,Description,Installedby | Sort-Object InstalledOn | Format-Table -AutoSize                           
                        }  
 
            } -ArgumentList $Days,$KB 
    }
} 


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.