Windows PowerShell script to check for specific hotfix

I was asked by our IT department to write something that would check if a specific Windows Update hotfix was installed on a number of servers (they gave me list), and they wanted it ASAP.

So, I thought, hmm…this is the perfect opportunity to give Microsoft’s new scripting tool, Windows PowerShell, a try. I tip my hat to Microsoft for creating this tool – it was about time – since something like this has been in need to combat the “last mile” in the IT world.

It shouldn’t take more than 5 minutes to write this script, but being familiar with the .NET Framework is sort of a prerequisite:

# Get content
$computers = get-content c:\computers.txt

# Get all the info using WMI
$results = get-wmiobject -class “Win32_QuickFixEngineering” -namespace “root\CIMV2” -computername $computers

# Loop through $results and look for a match then output to screen
foreach ($objItem in $results)
{
    if ($objItem.HotFixID -match “KB932168”)
    {
        write-host $objItem.CSName
        write-host “Hotfix KB932168 installed”
        write-host
    }
}

To use it:

1. Copy-and-paste this into notepad and save it as scriptname.ps1.

2. Set the execution policy, as Microsoft intentionally set it up like *nix scripts (right on!) wherein you need to chmod it, like so: Set-ExecutionPolicy Unrestricted.

Note: There are different execution policies, such as “AllSigned.” Google it if you want to know more about it.

3. Run it from the powershell by typing .\scriptname.ps1.

4. That’s it! Now wait for the results.

This script, unfortunately, will only print out the servers that have the specific “KB932168” (read: an example) hotfix installed. I could’ve expanded the script to output a list of servers that didn’t have it and output it to a text file using the Out-File cmdlet, but, at the end, it served its purpose and got the results our IT department needed.

BTW, this script will work on Win2k3, XP, Win2k, and Win98 – but the corresponding .NET Framework (version 2.0 is good; version 3.0 is already out) must be installed on the target machine. Also, know that the Windows PowerShell only works on Windows.

Hope this helps! =0)

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.