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?
Given this behavior, is there a better way to write this code?
Thank you
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.
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.
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.
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.
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' } }
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With