Checking only "Automatic" services with powershell
I've seen lots of scripts out there for manually stopping/starting services in a list, but how can I generate that list programatically of -just- the automatic services. I want to script some reboots, and am looking for a way开发者_开发技巧 to verify that everything did in fact start up correctly for any services that were supposed to.
Get-Service
returns System.ServiceProcess.ServiceController
objects that do not expose this information. Thus, you should use WMI for this kind of task: Get-WmiObject Win32_Service
. Example that shows the required StartMode
and formats the output a la Windows control panel:
Get-WmiObject Win32_Service |
Format-Table -AutoSize @(
'Name'
'DisplayName'
@{ Expression = 'State'; Width = 9 }
@{ Expression = 'StartMode'; Width = 9 }
'StartName'
)
You are interested in services that are automatic but not running:
# get Auto that not Running:
Get-WmiObject Win32_Service |
Where-Object { $_.StartMode -eq 'Auto' -and $_.State -ne 'Running' } |
# process them; in this example we just show them:
Format-Table -AutoSize @(
'Name'
'DisplayName'
@{ Expression = 'State'; Width = 9 }
@{ Expression = 'StartMode'; Width = 9 }
'StartName'
)
精彩评论