Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powershell script to check if service is started, if not then start it

Tags:

powershell

I have built a small powershell script to check if a service is started. If it is not started, try to start it then wait one minute and check again. Keep repeating this process until the service start successfully. I have found the loop does not behave the way I expected in that I seem to have to re-assign the service variable within the loop in order to get the updated status. Here is my code:

$ServiceName = 'Serenade' $arrService = Get-Service -Name $ServiceName  if ($arrService.Status -ne 'Running'){ $ServiceStarted = $false} Else{$ServiceStarted = $true}  while ($ServiceStarted -ne $true){ Start-Service $ServiceName write-host $arrService.status write-host 'Service started' Start-Sleep -seconds 60 $arrService = Get-Service -Name $ServiceName #Why is this line needed? if ($arrService.Status -eq 'Running'){ $ServiceStarted = $true} } 

If I run the code without the third last line (the one with the comment), I get the following output. I can check in the Windows service manager and the service was clearly started after the first loop. Why is that third last line required?

enter image description here

Given this behavior, is there a better way to write this code?

Thank you

like image 388
Dave.Gugg Avatar asked Jan 28 '16 15:01

Dave.Gugg


People also ask

How do you check if a service is running or not in PowerShell?

Use PowerShell to Check Service Status on a Remote Computer You can use the Get-Service cmdlet to get the status of services not only on the local but also on remote computers. To do this, use the –ComputerName parameter. You can use the NetBIOS, FQDN name, or an IP address as a computer name.

How check Windows service is running or not?

Windows has always used the Services panel as a way to manage the services that are running on your computer. You can easily get there at any point by simply hitting WIN + R on your keyboard to open the Run dialog, and typing in services. msc.

How can I tell if a service is running on a remote computer?

Windows natively has a command line tool which can be used to check if a service is running or not on a remote computer. The utility/tool name is SC.exe. SC.exe has parameter to specify the remote computer name. You can check service status only on one remote computer at a time.

What is $_ in PowerShell?

The “$_” is said to be the pipeline variable in PowerShell. The “$_” variable is an alias to PowerShell's automatic variable named “$PSItem“. It has multiple use cases such as filtering an item or referring to any specific object.


2 Answers

I think you may have over-complicated your code: If you are just checking to see if a service is running and, if not, run it and then stop re-evaluating, the following should suffice:

Good point on the refresh.

$ServiceName = 'Serenade' $arrService = Get-Service -Name $ServiceName  while ($arrService.Status -ne 'Running') {      Start-Service $ServiceName     write-host $arrService.status     write-host 'Service starting'     Start-Sleep -seconds 60     $arrService.Refresh()     if ($arrService.Status -eq 'Running')     {         Write-Host 'Service is now Running'     }  } 
like image 50
Nick Eagle Avatar answered Sep 28 '22 15:09

Nick Eagle


Given $arrService = Get-Service -Name $ServiceName, $arrService.Status is a static property, corresponding to the value at the time of the call. Use $arrService.Refresh() when needed to renew the properties to current values.

MSDN ~ ServiceController.Refresh()

Refreshes property values by resetting the properties to their current values.

like image 40
rivy Avatar answered Sep 28 '22 14:09

rivy