PowerShell Function: Find-Hotfix

April 9, 2012 by Joe

Every now and then you’ll need to verify whether or not a given hotfix is installed on a particulat computer (or group of computers). This function takes all the work out of that task.

##-------------------------------------------------------------------------- ## FUNCTION.......: Find-Hotfix ## PURPOSE........: Finds computers with a specified Microsoft Hotfix ## installed. ## REQUIREMENTS...: PowerShell v2.0 ## NOTES..........: ##-------------------------------------------------------------------------- Function Find-Hotfix { <# .SYNOPSIS Finds computers with a specified Microsoft Hotfix installed. .DESCRIPTION This function reads a list of computernames (one per line), checks each to determine if the specified hotfix is installed, and displays a list of all computers and the status of the hotfix for that computer. .PARAMETER FileName Full path and filename of the file containing a list of computers to check (one computer name per line). THis is a mandatory parameter. If you omit it, you will be prompted to enter a value before the function continues. .PARAMETER KB The KB number of the hotfix to check for (just the number). This is a mandatory parameter. If you omit it, you will be prompted to enter a value before the function continues. .EXAMPLE C:\PS>Find-Hotfix c:\list.txt 2564958 This example will check all computers listed in the file "c:\list.txt" for hotfix KB2564958. Sample output: Wkstn01 HOTFIX NOT FOUND Wkstn02 Security Update Wkstn03 HOTFIX NOT FOUND Svr01 Security Update Svr02 Security Update .EXAMPLE C:\PS>'c:\list.txt' | Find-Hotfix -KB:2564958 This example does the same thing as Example 1, but the list of computers to check is being passed to the Function using pipelining. .NOTES NAME......: Find-Hotfix AUTHOR....: Joe Glessner LAST EDIT.: 21MAR12 CREATED...: 10APR11 .LINK https://joeit.wordpress.com/ #> [CmdletBinding()] Param ( [Parameter(Mandatory=$True, ValueFromPipeline=$True, Position=0)] [Alias('file,f')] [String]$FileName, [Parameter(Mandatory=$True, ValueFromPipeline=$False, Position=1)] [String]$KB )#End Param $ErrorActionPreference = "SilentlyContinue" $ComputerNames = Get-Content $FileName $KBN = "kb" + $KB ForEach ($Computer In $ComputerNames) { $StrQuery = "select * from win32_pingstatus where address = '" + $Computer + "'" $WMI = Get-WMIObject -query $StrQuery If ($wmi.statuscode -eq 0) { $CheckKB = GWMI Win32_QuickFixEngineering -computer $Computer | Where-Object {$_.hotfixid -eq $KBN} | Select-Object hotfixid, description If ($CheckKB.hotfixid -eq $KBN) { Write-Host -f green $Computer ` "`t" $CheckKB.Description "`r" }#END: If ($CheckKB.hotfixid -eq $KBN) Else { Write-Host -f red $Computer ` "`t" "HOTFIX NOT FOUND" "`r" }#END: Else }#END: If ($wmi.statuscode -eq 0) Else { Write-Host -f yellow $Computer "`t" "Ping failed!" "`r" }#END: Else }#END: ForEach ($Computer In $ComputerNames) }#END: Function Find-HotFix