This script list all servers in Active Directory running on Windows 2008 and Windows 2008 R2 and check the service pack level (SP2 for Windows 2008 and SP1 for Windows 2008 R2)

Script (with Microsoft Active Directory module loaded : import-module activedirectory) :

$list = Get-Adcomputer -LDAPFilter "(operatingsystem=*2008*)" -properties operatingsystem,operatingSystemServicePack | select name,operatingsystem,operatingSystemServicePack
foreach ($computer in $List) {
	$computername = $computer.name
	if ($computer.operatingsystem -like "*R2*"){
		if ($computer.operatingSystemServicePack -eq "Service Pack 1") {
			Write-Host $computername "`t" $computer.operatingsystem "`t" $computer.operatingSystemServicePack -Fore "Green"
		}
		else {
			Write-Host $computername "`t" $computer.operatingsystem "`t" $computer.operatingSystemServicePack -Fore "Red"
		}
	}
	if ($computer.operatingsystem -notlike "*R2*") {
		if ($computer.operatingSystemServicePack -eq "Service Pack 2") {
			Write-Host $computername "`t" $computer.operatingsystem "`t" $computer.operatingSystemServicePack -Fore "Green"
		}
		else {
			Write-Host $computername "`t" $computer.operatingsystem "`t" $computer.operatingSystemServicePack -Fore "Red"
		}
	}
}

Script (with Quest Active Directory module) :

$list = Get-QADComputer -DontUseDefaultIncludedProperties -SearchAttributes @{operatingSystem='*2008*'} -IncludedProperties operatingsystem,operatingSystemServicePack | select name,operatingsystem,operatingSystemServicePack
foreach ($computer in $List) {
	$computername = $computer.name
	if ($computer.operatingsystem -like "*R2*"){
		if ($computer.operatingSystemServicePack -eq "Service Pack 1") {
			Write-Host $computername "`t" $computer.operatingsystem "`t" $computer.operatingSystemServicePack -Fore "Green"
		}
		else {
			Write-Host $computername "`t" $computer.operatingsystem "`t" $computer.operatingSystemServicePack -Fore "Red"
		}
	}
	if ($computer.operatingsystem -notlike "*R2*") {
		if ($computer.operatingSystemServicePack -eq "Service Pack 2") {
			Write-Host $computername "`t" $computer.operatingsystem "`t" $computer.operatingSystemServicePack -Fore "Green"
		}
		else {
			Write-Host $computername "`t" $computer.operatingsystem "`t" $computer.operatingSystemServicePack -Fore "Red"
		}
	}
}
Get service pack level on Windows 2008 servers

Leave a Reply

Your email address will not be published. Required fields are marked *