Scenario: You want a Powershell method for finding any services that are set to start automatically but have stopped in error or by failure.
Solution: The script below looks for any Exchange servers that start with 2013* and then uses the logic “Find a Service that is set to Automatic that is currently not running and an Exit Code of the service is not 0” for each server. Then attempt to restart that service on each server. An exit code of 0 means the service was stopped manually OR stopped by Windows as it was no longer required to run. Here is a list of Error Code/Exit Code descriptions: Exit Code Descriptions
#Collect Servers into a Variable $1 = Get-exchangeserver 2013* #Collect Services that are not started due to failure or error $2 = $1 |%{Get-CimInstance win32_service -Filter "startmode = 'auto' AND state != 'running' AND Exitcode !=0 " -ComputerName $_ | select systemname, name, startname, exitcode} #View the Services that are stopped for each server $2 #Restart each failed service $2 | %{Get-service $_.Name -computername $_.SystemName | Start-service -passthru}