Search code examples
powershellpowershell-remoting

Start all automatic services not running


I'm trying to further automate our Windows patching to automatically attempt to start any services that are set to Auto but are not running.

Below is what I've tried so far with no success:

$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
}

Here is the error I'm getting:

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

Is there something I'm missing?


Solution

  • I ended up using Adrian R's suggestion and it works great. Here's the final version:

    #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"

    FYI if you don't exclude the SPPSVC you'll get the below error: Set-Service : Service 'Software Protection (sppsvc)' cannot be configured due to the following error: Access is denied

    Thanks everyone!