Fun with the Get-Hotfix cmdlet

With the Get-Hotfix cmdlet you can query the list of hotfixes that have been applied to computers.

?View Code POWERSHELL
Get-Hotfix | Format-Table -AutoSize;

This will display the list of hotfixes installed on the local computer.

Source Description     HotFixID  InstalledBy          InstalledOn
------ -----------     --------  -----------          -----------
SO0590 Update          982861    NT AUTHORITY\SYSTEM  07/07/2011 00:00:00
SO0590 Update          KB958830  host\Administrator   07/07/2011 00:00:00
SO0590 Update          KB958830  host\Administrator
SO0590 Update          KB971033  host\Administrator
SO0590 Update          KB2264107 NT AUTHORITY\SYSTEM  06/08/2011 00:00:00
SO0590 Security Update KB2305420 host\Administrator   03/10/2011 00:00:00
SO0590 Security Update KB2393802 host\Administrator   03/10/2011 00:00:00
SO0590 Security Update KB2425227 host\Administrator   03/10/2011 00:00:00
SO0590 Security Update KB2475792 host\Administrator   03/10/2011 00:00:00
SO0590 Security Update KB2476490 NT AUTHORITY\SYSTEM
SO0590 Security Update KB2479628 host\Administrator   03/10/2011 00:00:00
SO0590 Security Update KB2479943 host\Administrator   03/10/2011 00:00:00
SO0590 Update          KB2484033 host\Administrator   03/10/2011 00:00:00
SO0590 Security Update KB2485376 host\Administrator   03/10/2011 00:00:00
SO0590 Update          KB2487426 host\Administrator   03/10/2011 00:00:00
SO0590 Update          KB2488113 NT AUTHORITY\SYSTEM  06/09/2011 00:00:00
SO0590 Security Update KB2491683 NT AUTHORITY\SYSTEM  06/09/2011 00:00:00
SO0590 Update          KB2492386 NT AUTHORITY\SYSTEM  06/09/2011 00:00:00

Not overly impressive in itself but Powershell really comes into its own when querying multiple computers. For example you may wish to confirm that a bunch of computers all have an important hotfix installed. This script will do it. Just alter the $HotFixID and $computers as needed.

?View Code POWERSHELL
# HotFixId to check for
$HotFixID = "KB958830";
 
# Array of computers to check
$computers = @("server1", "server2", "server3", "server4", "server5");
 
# Query each computer
foreach($computer in $computers)
{
	$hf = Get-HotFix -ComputerName $computer | Where-Object {$_.HotFixId -eq $HotFixID} | Select-Object -First 1;
	if($hf -eq $null)
	{
		Write-Host "Hotfix $HotFixID is not installed on $computer";
	}
	else
	{
		Write-Host "Hotfix $HotFixID was installed on $computer on by " $($hf.InstalledBy);
	}
}

In a few moments you can see which computers you need to take action on;

Hotfix KB958830 was installed on server1 on by  SMARTODDS\campbellr
Hotfix KB958830 was installed on server2 on by  SMARTODDS\campbellr
Hotfix KB958830 is not installed on server3
Hotfix KB958830 is not installed on server4
Hotfix KB958830 is not installed on server5

Powershell; the eliminator of tedium!


Leave a Reply