![用於取得啟動類型為「自動」的所有「已停止」服務的腳本 -- Windows](https://rvso.com/image/1510187/%E7%94%A8%E6%96%BC%E5%8F%96%E5%BE%97%E5%95%9F%E5%8B%95%E9%A1%9E%E5%9E%8B%E7%82%BA%E3%80%8C%E8%87%AA%E5%8B%95%E3%80%8D%E7%9A%84%E6%89%80%E6%9C%89%E3%80%8C%E5%B7%B2%E5%81%9C%E6%AD%A2%E3%80%8D%E6%9C%8D%E5%8B%99%E7%9A%84%E8%85%B3%E6%9C%AC%20--%20Windows.png)
因此,如果服務的啟動類型為“自動”或“手動”,但目前已停止,我想運行命令以查看這些問題。
在 powershell 和 CMD 中,我可以看到其中一個或另一個,但我沒有找到一種簡單的方法來過濾我需要的資料。
我基本上是在尋找為故障排除服務製作腳本。它將能夠確定是否有任何應該啟動(基於其啟動類型)但未運行(停止或暫停)的服務。
我遇到的問題是 powershell 或 CMD 不允許對結果進行深度過濾或管道傳輸。有人有辦法幫我解決這個問題嗎?
我該如何解決這個問題?
答案1
透過查看這個問題你可以為舊版的 PowerShell 想出這個:
Get-WmiObject -Class Win32_Service | Select-Object Name,State,StartMode | Where-Object {$_.State -ne "Running" -and $_.StartMode -eq "Auto"}
對於較新的版本(至少 5 個,也許 3/4),您也可以使用(JC2k8 建議的):
Get-Service | Select-Object -Property Name,Status,StartType | Where-Object {$_.Status -eq "Stopped" -and $_.StartType -eq "Automatic"}
在舊版的 PowerShell 中,Get-Service
cmdlet 不提供具有StartType
.
PowerShell 支援大量過濾和管道。 :)
答案2
(get-service|?{ $_.Status -eq "Stopped" -and $_.StartType -eq "Automatic"})|
select DisplayName, StartType, Status
答案3
Get-Service | Where-Object {$_.Status -eq "Stopped"} | where starttype -match automatic
答案4
如果適合您的問題,您可以嘗試此操作。以下程式碼可協助您啟動與 Sophos AV 相關的所有已停止服務,這些服務應運行,因為它們具有自動啟動類型。
# Start specific automatic start services not running
$service = "*sophos*"
$server = "<server_name>"
$stoppedServices = (Get-WmiObject Win32_Service -ComputerName $server | Where-Object {$_.Name -like $service -and $_.StartMode -eq 'Auto' -and $_.State -ne "Running"}).Name
foreach ($stoppedService in $stoppedServices) {
Write-Host -NoNewline "Starting Server/Service: "; Write-Host -ForegroundColor Green $server"/"$stoppedService
Get-Service -ComputerName $server -Name $stoppedService | Start-Service
}