#requires -version 2.0 <# ----------------------------------------------------------------------------- Script: Test-ServicePack.ps1 Version: 1.0 Author: Jeffery Hicks http://jdhitsolutions.com/blog http://twitter.com/JeffHicks http://www.ScriptingGeek.com Date: 2/15/2012 Keywords: Comments: "Those who forget to script are doomed to repeat their work." **************************************************************** * DO NOT USE IN A PRODUCTION ENVIRONMENT UNTIL YOU HAVE TESTED * * THOROUGHLY IN A LAB ENVIRONMENT. USE AT YOUR OWN RISK. IF * * YOU DO NOT UNDERSTAND WHAT THIS SCRIPT DOES OR HOW IT WORKS, * * DO NOT USE IT OUTSIDE OF A SECURE, TEST SETTING. * **************************************************************** ----------------------------------------------------------------------------- #> Function Test-ServicePack { <# .Synopsis Test Service Pack version value .Description Test if the service pack major version is equal or greater than the specified version. By default the function will return True or False. But you can use -Detail to retrieve additional system and service pack information. Computername : CLIENT22 OperatingSystem : Microsoft Windows 7 Professional ServicePackMajorVersion : 1 ServicePack : Service Pack 1 OK : True If you want to test for a specific service pack version, use the -Exact parameter. .Parameter Computername The name of the computer to query. The default is the localhost. .Parameter Version The minimum service pack version. .Parameter Exact Test for an exact match with the service pack major version. .Parameter Detail If specified, return additional operating system and servicepack information. .Example PS C:\> test-servicepack server01 -version 1 True .Example PS C:\> test-servicepack Client22 -version 1 -detail Computername : CLIENT22 OperatingSystem : Microsoft Windows 7 Professional ServicePackMajorVersion : 1 ServicePack : Service Pack 1 OK : True .Example PS C:\> get-content computers.txt | test-servicepack -version 2 -detail | Where {!$_.OK} | Select Computername,ServicePackMajorVersion Computername ServicePackMajorVersion ------------ ----------------------- SERVER1 1 SERVER5 1 Go through the list of computers and test for at least service pack 1. Display computers that don't meet the criteria. .Link Get-WMIObject #> [cmdletbinding()] Param ( [Parameter(Position=0,ValueFromPipeline=$True)] [ValidateNotNullOrEmpty()] [Alias("name")] [string]$Computername=$env:computername, [Parameter(Position=1)] [int]$Version=0, [switch]$Exact, [switch]$Detail ) Begin { Write-Verbose "Starting $($myinvocation.mycommand)" #a subset of WMI Properties to retrieve $props="ServicePackMajorVersion","CSName","CSDVersion","Caption" } Process { Write-Verbose "Getting OS information from $computername" Try { $OS=Get-WmiObject -Class Win32_OperatingSystem -Property $props -ComputerName $computername -ErrorAction Stop Write-verbose ("Testing SP version of {0}" -f $OS.Caption) if ($Exact) { Write-Verbose "Testing for exact match" $pass=$OS.ServicePackMajorVersion -eq $Version } else { Write-Verbose "Testing for at least version $Version" $pass=$OS.ServicePackMajorVersion -ge $Version } #write details if specified if ($Detail) { $os | Select @{Name="Computername";Expression={$_.CSName}}, @{Name="OperatingSystem";Expression={$_.Caption}}, ServicePackMajorVersion, @{Name="ServicePack";Expression={$_.CSDVersion}}, @{Name="OK";Expression={$Pass}} } else { Write $pass } } #Try Catch { $msg="Failed to retrieve OS information from {0}. {1}." -f $computername.toUpper(),$_.exception.message Write-Warning $msg } #catch Finally { #clean up before next computer if ($OS) { Remove-Variable OS } } #finally } #process End { Write-Verbose "Ending $($myinvocation.mycommand)" } } #close function