detecting service pack 2

Introduction

Even though Service Pack 2 has been out for over two years now, there's still a chance that I run across one without it where I work. To do some remote detection, I made a quick PowerShell script to loop through the computers of an OU and check what service pack they're on.

This script assumes that there are only computers in the OU.

The Script

Looping through the results of an LDAP query, first I check to see if the computer is online using win32_pingstatus. If the computer is online, I'll query it for the service pack version. Pretty simple.

$ldapString = "LDAP://ou=someOU,dc=someDomain,dc=com"
$searchResult = new directoryservices.directoryentry($ldapString)
$computers = @($searchResult.psbase.children)

foreach ($computer in $computers) {
    $c = $computer.name
    $query = "select * from win32_pingstatus where address = '$c'"
    $result = Get-WmiObject -query $query
    if ($result.protocoladdress) {
            $s = get-wmiobject -class "win32_operatingsystem" -computername $c
            $servicepack = $s.servicepackmajorversion
            "$c`t$servicepack" | out-host
    } else {
            "$c`tNot Responding" | out-host
    }
}