2010-05-07 35 views
2

我已经看到很多用于在列表中手动停止/启动服务的脚本,但是如何以编程方式生成该列表 - 只是自动服务。我想编写一些重新启动脚本,并且正在寻找一种方法来验证所有服务都应该正确启动。仅使用powershell检查“自动”服务

回答

11

Get-Service返回System.ServiceProcess.ServiceController不公开此信息的对象。因此,您应该使用WMI执行此类任务:Get-WmiObject Win32_Service。例如,显示所需StartMode并格式化输出香格里拉的Windows控制面板:

Get-WmiObject Win32_Service | 
Format-Table -AutoSize @(
    'Name' 
    'DisplayName' 
    @{ Expression = 'State'; Width = 9 } 
    @{ Expression = 'StartMode'; Width = 9 } 
    'StartName' 
) 

你有兴趣的是自动服务,但没有运行:

# 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' 
) 
+0

非常感谢,这是一直困扰我最长的时间,只是不能完全弄清楚。 – Lee 2010-05-07 03:50:50

相关问题