2015-08-21 46 views
1

我試圖進一步自動化Windows修補以自動嘗試啓動任何設置爲自動但未運行的服務。啓動所有未運行的自動服務

下面是我一直沒有成功到目前爲止已經試過:

$stoppedServices = Get-WmiObject win32_service -ComputerName $computer -Filter "startmode = 'auto' AND state != 'running'" | select name 

foreach ($stoppedService in $stoppedServices) { 
    Set-Service -Service $stoppedService -Status Running 
} 

這是我得到的錯誤:我失去了

Set-Service : Service @{name=RemoteRegistry} was not found on computer '.'. 
At line:4 char:13 
+    Set-Service -Service $stoppedService -Status Running 
+    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
+ CategoryInfo   : ObjectNotFound: (.:String) [Set-Service], InvalidOperationException 
+ FullyQualifiedErrorId : InvalidOperationException,Microsoft.PowerShell.Commands.SetServiceCommand 

有什麼事?

+0

我建議以下,如果你只需要一個簡單的一行: GET-WmiObject可以win32_service時-ComputerName $計算機篩選器 「!STARTMODE = 'Auto' 和狀態= '運行'」 | Invoke-WmiMethod -Name StartService –

回答

1

最後我用阿德里安 - [R的建議和它的偉大工程。下面是最終版本:

#Start all non-running Auto services Get-WmiObject win32_service -ComputerName $computer -Filter "startmode = 'auto' AND state != 'running' AND name != 'sppsvc'" | Invoke-WmiMethod -Name StartService #Output any services still not running $stoppedServices = Get-WmiObject win32_service -ComputerName $computer -Filter "startmode = 'auto' AND state != 'running' AND name != 'sppsvc'" | select -expand Name Write-Host "$env:ComputerName : Stopped Services: $stoppedServices"

僅供參考,如果你不排除SPPSVC你會得到下面的錯誤: Set-Service : Service 'Software Protection (sppsvc)' cannot be configured due to the following error: Access is denied

謝謝大家!

1

您需要使用參數-Expand,否則你還是有一個對象與屬性Name而不是該屬性的值:

$stoppedServices = Get-WmiObject win32_service ... | select -Expand name