[英]Powershell to display all automatic services that are stopped and attempt to start those services
[英]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.