þÿ# This Function Will Query A Dependancy Service and wait until the timer expires OR for the service to start. function QueryService { param($Service,$timer1) $success = "" write-host "Waiting on $Service Service..." # Create a for loop to INC a timer Every Second for ($b=1; $b -lt $timer1; $b++) { $servicestat = get-service $Service $status = $servicestat.status $b2 = $timer1 - $b # Determine the Percent Complete for the seconds. $percent = $b * (100 / $timer1) # Display the progress on the Screen Write-Progress -Activity "Waiting on $Service Service..." -PercentComplete $percent -CurrentOperation "$b2 Seconds Remaining" -Status "Current WMI Status: $status" # Determine if the Process is Running. If not, reloop. If so exit loop. if ($status -eq "Running") { write-host "$Service Service Started Successfully: $status in $b Seconds" [int]$b = $timer1 $success = "yes" # Tells the Loop to Stop Incrementing as the Service is running Write-Progress -Activity "Completed" -Status "Current $Service Status: $status in $b Seconds" -Completed } # Start-Sleep is available for the write-progress. Its value is in seconds. start-sleep 1 } # 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. if ($success -ne "yes") { write-host "ERROR in Script: $Service Service Did Not Start In $timer1 Seconds. Status: $status" # Stop the Script BREAK } } # The service must be the actual executible name, not the friendly name # The Below Examples are Querying SQL Services for startup waiting 120 seconds for a timeout. QueryService "SQLServerAgent" "120" QueryService "MSSQLSERVER" "120"