PowerShell; Start Automatic Services and Report Status

Many environments have patching processes or other updates that reboot large numbers of systems. Occasionally, when those systems boot up, some of their services fail to start. It takes too long to logon to each server and manually check and start failed services. PowerShell can do the job for us.

The code below will import a list of server names (text file with only the hostnames) and scan each server in the list for services that are set to automatic. If any of the servers’ automatic services are discovered to be stopped it will try to restart them.

The script will then report on all of the automatic services except those that stopped normally.

$servers = Get-Content C:\servers.txt
$report = @()
Foreach ($Server in $Servers) {
Get-WMIObject win32_service -ComputerName $server -Filter "startmode = 'auto' AND state != 'running'" | Invoke-WmiMethod -Name StartService
$wmi = Get-wmiobject win32_service -Filter "startmode = 'auto' AND Exitcode =0" -ComputerName $server
$report += $wmi | select-object @{n="ServerName";e={Get-WmiObject win32_computersystem -ComputerName $server|select name -ExpandProperty name}}, @{n="ServiceName";e={$_.name}},@{n="Status";e={$_.state}},@{n="Start Account";e={$_.startname}}
$report|export-csv C:\Services-Report.csv -NoTypeInformation

You can make this operation more dynamic by replacing the $servers variable with code to find all of the $servers on your network. It would also be easy to output the $report to an email message or HTML dashboard.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s