我试图进一步自动化我们的Windows修补程序,以自动尝试启动任何设置为Auto但未运行的服务。
下面是我迄今尝试过但没有成功的地方:
$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我遗漏了什么吗?
发布于 2015-08-24 12:11:31
我最终采用了禤浩焯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
谢谢大家!
发布于 2015-08-21 20:13:34
-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
https://stackoverflow.com/questions/32148258
复制相似问题