PowerShell queryService–Wait for a Dependency Starting Service
By: Brenton Blawat
There have been several occasions while scripting in PowerShell that the script is dependent on a specific service in the running state. A good example is the need to wait for the WMI Service to startup prior to making a WMI call, or SQL Server where when you restart the services, you have to wait for the services to be in a running state before you can execute commands.
The following function solves this issue. It also displays the proper implementation of a PowerShell wait timer to wait for a service to become operational before continuing in the script.
PowerShell Code Waiting for Service
1: # This Function Will Query A Dependancy Service and wait until the timer expires OR for the service to start.
2: function QueryService { param($Service,$timer1)
3: $success = ""
4: write-host "Waiting on $Service Service..."
5:
6: # Create a for loop to INC a timer Every Second
7: for ($b=1; $b -lt $timer1; $b++) {
8: $servicestat = get-service $Service
9: $status = $servicestat.status
10: $b2 = $timer1 - $b
11:
12: # Determine the Percent Complete for the seconds.
13: $percent = $b * (100 / $timer1)
14:
15: # Display the progress on the Screen
16: Write-Progress -Activity "Waiting on $Service Service..." -PercentComplete $percent -CurrentOperation "$b2 Seconds Remaining" -Status "Current WMI Status: $status"
17:
18: # Determine if the Process is Running. If not, reloop. If so exit loop.
19: if ($status -eq "Running") {
20: write-host "$Service Service Started Successfully: $status in $b Seconds"
21: [int]$b = $timer1
22: $success = "yes"
23:
24: # Tells the Loop to Stop Incrementing as the Service is running
25: Write-Progress -Activity "Completed" -Status "Current $Service Status: $status in $b Seconds" -Completed
26:
27: }
28:
29: # Start-Sleep is available for the write-progress. Its value is in seconds.
30: start-sleep 1
31:
32: }
33: # The script will now stop as the above loop has meet its time criteria and the success is not set to yes.time has expired.
34: if ($success -ne "yes") {
35: write-host "ERROR in Script: $Service Service Did Not Start In $timer1 Seconds. Status: $status"
36: # Stop the Script
37: BREAK
38: }
39: }
40:
41: # The service must be the actual executible name, not the friendly name
42: # The Below Examples are Querying SQL Services for startup waiting 120 seconds for a timeout.
43: QueryService "SQLServerAgent" "120"
44: QueryService "MSSQLSERVER" "120"
The implementation of the QueryService is pretty straight forward. The syntax is
QueryServices “EXENAME” “TIMEOUT_IN_SECONDS”
The exe name is what is found in the Services Management Console listed under the "Service Name”. Using the “Display Name” will not work with this function.
Please note: If a service is running, there will be approximately a second delay in the script while the system is verifying if the service is running. This is a small sacrifice for error handling in the instance that the service may not be running.
Happy Coding!


