簡體   English   中英

啟動所有未運行的自動服務

[英]Start all automatic services not running

我試圖進一步自動化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

有什么我想念的嗎?

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

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

我最終使用了Adrian 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

感謝大家!

-ExpandProperty選項將起作用。 您也可以使用以下示例:

$stoppedServices = Get-WmiObject win32_service -ComputerName $computer -Filter "startmode = 'auto' AND state != 'running'" | foreach {$_.Name}

將結果輸入foreach將為您提供一系列價值。

參考: http : //blogs.msdn.com/b/powershell/archive/2009/09/14/select-expandproperty-propertyname.aspx

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM