PowerShell queryService–Wait for a Dependency Starting Service

Posted on June 3, 2011. Filed under: Powershell Tangents | Tags: , , , , , |

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

Download Code

   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!


Read Full Post | Make a Comment ( None so far )

Recently on Business and Information Technology Tangents...

Verizon Wireless–4G LTE Technology / Sales Disconnection–Very Frustrating

Posted on December 8, 2010. Filed under: Product Review Tangents |

SQL Server 2008 Security Permissions – Windows / Active Directory Authentication Issues

Posted on October 4, 2010. Filed under: Software Tangents |

Nested User Groups (Groups in Groups) / Built-in Local Groups Issue

Posted on July 13, 2010. Filed under: Server Tangents |

Powershell Import-GPO : The Data Is Invalid Fixed

Posted on April 20, 2010. Filed under: Powershell Tangents |

PowerShell Script: Encrypting / Decrypting A String – Function Encrypt-String

Posted on March 20, 2010. Filed under: Powershell Tangents |

PowerShell Script: Finding A Distinguished Name of a Group/User: Function Find-DN

Posted on March 8, 2010. Filed under: Powershell Tangents |

2010 Volkswagen CC Review – A test Drive Bashing

Posted on February 23, 2010. Filed under: Car Tangents |

PowerShell Script: Retrieving Distinguished name (DN) from A Fully Qualified Domain Name (FQDN)

Posted on February 22, 2010. Filed under: Powershell Tangents |

PowerShell Script: Distinguished Name / Fully Qualified Domain Name to string

Posted on February 22, 2010. Filed under: Powershell Tangents |

Default Domain Policies Windows Server 2003 SP2 / Windows server 2008 R2

Posted on February 3, 2010. Filed under: Server Tangents |

    About

    Business and Information Technology Tangents is dedicated to providing quality content while informing the world about technology.

    RSS

    Subscribe Via RSS

    • Subscribe with Bloglines
    • Add your feed to Newsburst from CNET News.com
    • Subscribe in Google Reader
    • Add to My Yahoo!
    • Subscribe in NewsGator Online
    • The latest comments to all posts in RSS

    Meta

Liked it here?
Why not try sites on the blogroll...

Follow

Get every new post delivered to your Inbox.