2010-05-07 75 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