启动所有未运行的自动服务
我试图进一步自动化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
有什么事?
最后我用阿德里安 - [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
谢谢大家!
您需要使用参数-Expand
,否则你还是有一个对象与属性Name
而不是该属性的值:
$stoppedServices = Get-WmiObject win32_service ... | select -Expand name
的-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
我建议以下,如果你只需要一个简单的一行: GET-WmiObject可以win32_service时-ComputerName $计算机筛选器 “!STARTMODE = 'Auto' 和状态= '运行'” | Invoke-WmiMethod -Name StartService –