Pages

7/26/2013

Powershell: Status of Windows Hotfix

Powershell: Check Status of Windows Hotfix


##################################################################################
#
#   check-hotfix.ps1
#
#       Confirm hotfix has been installed on all Windows2008 servers
#
#       Requires admin permission on every server
#
##################################################################################

$hotfix = "KB2520155"

$ServerList = ".\SUCCESS.TXT" #servers where hotfix is installed
$ErrorList = ".\ERRORS.TXT" #servers where hotfix is not installed
$ListFile = ".\SERVERS.TXT" #all the servers I checked
New-Item $ListFile -Type file -Force >$nul
New-Item $ServerList -Type file -Force >$nul
New-Item $ErrorList -Type file -Force >$nul

$today = get-date
$day = $today.Day
$mth = $today.Month
$year = $today.Year
$hour = $today.Hour
$min = $today.Minute
$sec = $today.Second
$date = "$year-$mth-$day-$hour$min$sec"

@"
$date
Servers Responding to PING
--------------------------------------------------------------------------
"@  | out-file -encoding ASCII -filepath $ListFile

@"
$date
Servers with hotfix $hotfix
--------------------------------------------------------------------------
"@ | out-file -encoding ASCII -filepath $ServerList

@"
$date
Servers without hotfix $hotfix
--------------------------------------------------------------------------
"@ | out-file -encoding ASCII -filepath $ErrorList


$List = ""

"Execution in progress..."

# Create $list of AD machine accounts for Windows Servers
$strCategory = "computer"
$strOS = "Windows*Server*2008*"
$objDomain = New-Object System.DirectoryServices.DirectoryEntry
$objSearcher = New-Object System.DirectoryServices.DirectorySearcher
$objSearcher.SearchRoot = $objDomain
$objSearcher.Filter = ("OperatingSystem=$strOS")
$colProplist = "dnshostname", "operatingsystem"
foreach ($i in $colPropList){$objSearcher.PropertiesToLoad.Add($i)}
$colResults = $objSearcher.FindAll()
foreach ($objResult in $colResults) {
    $objComputer = $objResult.Properties;
    $Server = $objComputer.dnshostname
    $OS = $objComputer.operatingsystem
    $Server = $Server -replace "\.usa\.DOMAIN\.com", ""
    $Server = $Server -replace "\s{2,}", ""
    $OS = $OS -replace "$([char]0x00AE)" , "" #remove "registered trademark" symbol
    if ($Server) {#skip null
        "$Server , $OS"
        if (Test-Connection -ComputerName $Server -quiet -count 1) {#PING OK
            "    Responds to PING"
            "$Server , $OS" | out-file -encoding ASCII -filepath $ListFile -append

            #Get Hotfix info
            $installed = get-wmiobject -class "Win32_QuickFixEngineering" -namespace "root\CIMV2" -computername $strComputer `
               -Filter "HotFixID='$hotfix'" 

            if ($installed) {
                "        $hotfix INSTALLED!"
                write-output "$Server , $OS" | out-file -encoding ASCII -filepath $ServerList -append
            }
            else {
                "        $hotfix NOT installed"
                write-output "$Server , $OS" | out-file -encoding ASCII -filepath $ErrorList -append    
            }

        }#end if PING OK, do nothing if PING fails
    }#if not null, do nothing if null
}#foreach

No comments: