#requires -version 2.0 <# ----------------------------------------------------------------------------- Script: Ping-Service.ps1 Version: 0.9.5 Author: Jeffery Hicks http://jdhitsolutions.com/blog http://twitter.com/JeffHicks http://www.ScriptingGeek.com Date: 2/7/2012 Keywords: Service, Test, Ping 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 Ping-Service { <# .Synopsis Test service status. .Description Send a "ping" to a service on a computer to determine if it is running or responding. The function will return a custom object. You can pipe computernames to the function, assuming you want to test the same service on each. .Parameter Name The name of the service to test. .Parameter Computername The name of the computer to query. The default is the local computer. .Parameter Quiet If specified, return only True or False .Example PS C:\> ping-service spooler Time : 2/7/2012 9:45:17 AM Computername : SERENITY Name : spooler DisplayName : Print Spooler Status : Running Response : 2.0001 Reply : True .Example PS C:\> ping-service spooler -comp quark Time : 2/7/2012 9:46:00 AM Computername : quark Name : spooler Displayname : Status : Unknown Response : 11592.663 Reply : False .Example PS C:\> "serenity","jdhit-dc01","quark" | ping-service wuauserv | Select Computername,Status,Reply Computername Status Reply ------------ ------ ----- serenity Running True jdhit-dc01 Running True quark Unknown False .Example PS C:\> ping-service wuauserv jdhit-dc01 -quiet True .Link Get-Service #> [cmdletbinding()] Param( [Parameter(Position=0,Mandatory=$True,HelpMessage="Enter the name of a service")] [ValidateNotNullorEmpty()] [string]$Name, [Parameter(Position=1,ValuefromPipeline=$True)] [ValidateNotNullorEmpty()] [string[]]$Computername=$env:computername, [switch]$Quiet ) Begin { Write-Verbose "Starting $($myinvocation.mycommand)" Write-Verbose "Pinging service $name" } Process { Foreach ($computer in $computername) { Write-Verbose "Testing computer $computer" #get service from each computer #measure how long the command takes $measure=Measure-Command {$service=Get-Service -Name $Name -ComputerName $Computer -ErrorAction SilentlyContinue -ErrorVariable ev} if ($Quiet -and $service.status -eq "Running") { #if quiet only return True if service is running Write-Output $True } elseif ($Quiet) { #status is something other than Running Write-Output $False } elseif ($ev) { $msg="Error with {0} service on {1}. {2}" -f $Name,$Computer,$ev[0].Exception.message Write-Verbose $msg Remove-Variable ev New-Object -TypeName PSObject -Property @{ Time=(Get-Date -displayhint Time); Computername=$Computer; Name=$name; Displayname=$null; Status="Unknown"; Response=$measure.TotalMilliseconds; Reply=$False } | Select Time,Computername,Name,Displayname,Status,Response,Reply } else { #otherwise, return a custom object $service | Select @{Name="Time";Expression={(Get-Date -displayhint Time)}}, @{Name="Computername";Expression={$_.Machinename}}, Name,Displayname,Status, @{Name="Response";Expression={$measure.TotalMilliseconds}}, @{Name="Reply";Expression={ if ($_.Status -eq "Running") {$True} else {$False} }} } } #foreach } End { Write-Verbose "Ending $($myinvocation.mycommand)" } } #function