[PowerShell] Script Lets You Check Patches’ Status

I was asked to modify the script posted at http://windowsitpro.com/scripting/powershell-script-lets-you-check-patches-status

such that given a list of computers, check if any PC has missed a KB. So here it is:

$erroractionpreference = “SilentlyContinue”
clear-host

if ($args.count -lt 2)
{
write-host -f blue “Syntax Error : Must have 2 parameters.”
write-host -f blue “Eg powershell.exe \kb.ps1 ”
break
}

$filename = $args[0]
$computer = $args[1]
$kbs = get-content $filename
$kbTotal = 0
$kbNotFound = 0
write-host -f green “Checking on”$computer

foreach ($kb in $kbs)
{
$global:kbTotal++
$strQuery = “select * from win32_pingstatus where address = ‘” + $computer + “‘”
$wmi = get-wmiobject -query $strQuery
if ($wmi.statuscode -eq 0)
{
$checkkb = get-wmiobject Win32_QuickFixEngineering -computer $computer | where-object {$_.hotfixid -eq $kb} | select-object hotfixid, description
# BEGIN CALLOUT A

if ($checkkb.hotfixid -eq $kb)
{write-host -f green $kb “`t” $checkkb.description “`r”}
else
{write-host -f red $kb “`t” “Patch not found.” “`r”
$global:kbNotFound++
}
}
else
{write-host $computer “`t” “Ping failed.” “`r”}
# END CALLOUT A
}

if ($global:kbNotFound -eq 0)
{write-host -ForegroundColor Green $global:kbNotFound “KBs missing out of”$global:kbTotal “required”}
else
{write-host -ForegroundColor Red $global:kbNotFound “KBs missing out of”$global:kbTotal “required”}

Below is the the version where it outputs to a text file, I had modified it to output to c:\temp instead:

# Function : to check for kbxxxx patches on remote pc
# This version redirects the output to a text file.

$erroractionpreference = “SilentlyContinue”

cls
if ($args.count -ne 2)
{
write-host -f blue “Syntax Error : Must have 2 parameters.”
write-host -f blue “Eg powershell.exe \kb.ps1 ”
break
}

$filename = $args[0]
$computer = $args[1]
$kbs = get-content $filename

write-host -f green “Checking on”$computer
“Checking on” +$computer | out-file c:\temp\results.txt -append

foreach ($kb in $kbs)
{

$strQuery = “select * from win32_pingstatus where address = ‘” + $computer + “‘”
$wmi = gwmi -query $strQuery

if ($wmi.statuscode -eq 0)
{
$checkkb = gwmi Win32_QuickFixEngineering -computer $computer | where {$_.hotfixid -eq $kb} | `
select-object hotfixid, description

if ($checkkb.hotfixid -eq $kb)
{
write-host -f green $kb “`t” $checkkb.description “`r”
$kb + ” ” + $checkkb.description | out-file c:\temp\results.txt -append
}

else
{
write-host -f red $kb “`t” “Patch not found.” “`r”
$kb + ” patch not found” | out-file c:\temp\results.txt -append
}

}
else
{
write-host $computer “`t” “Ping fail.” “`r”
$computer + ” ping fail”| out-file c:\temp\results.txt -append
}

}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s