Help with PowerShell script

Borislav Vitanov 81 Reputation points
2022-01-31T13:31:56.207+00:00

Hello everyone,

I need some help with one script. I've set the script to run after the scheduled reboot to check about stopped services and run them.

$server = "srv-01.domain.com"
$stoppedServices = (Get-WmiObject Win32_Service -ComputerName $server | Where-Object {$_.StartMode -eq 'Auto' -and $_.State -ne "Running" -and $_.Name -ne 'RemoteRegistry'}).Name
foreach ($stoppedService in $stoppedServices) {
  Write-Host -NoNewline "Starting Server/Service: "; Write-Host -ForegroundColor Green $server"/"$stoppedService
  Get-Service -ComputerName $server -Name $stoppedService | Start-Service
}

Now, usually everything is fine but the colleague installed Brave and Edge and I decided to add them after the RemoteRegistry.

If I add them like this:

$server = "srv-01.domain.com"
$stoppedServices = (Get-WmiObject Win32_Service -ComputerName $server | Where-Object {$_.StartMode -eq 'Auto' -and $_.State -ne "Running" -and $_.Name -ne 'RemoteRegistry, brave, edgeupdate'}).Name
foreach ($stoppedService in $stoppedServices) {
  Write-Host -NoNewline "Starting Server/Service: "; Write-Host -ForegroundColor Green $server"/"$stoppedService
  Get-Service -ComputerName $server -Name $stoppedService | Start-Service
}

I receive:

Starting Server/Service: srv-01.domain.com/BITS
Starting Server/Service: srv-01.domain.com/brave
Start-Service : Failed to start service 'Brave Update Service (brave) (brave)'.
At C:\Script\test_stopped.ps1:5 char:61
+ ... t-Service -ComputerName $server -Name $stoppedService | Start-Service
+                                                             ~~~~~~~~~~~~~
    + CategoryInfo          : OpenError: (System.ServiceProcess.ServiceController:ServiceController) [Start-Service], ServiceCommandException
    + FullyQualifiedErrorId : StartServiceFailed,Microsoft.PowerShell.Commands.StartServiceCommand

Starting Server/Service: srv-01.domain.com/edgeupdate
Start-Service : Failed to start service 'Microsoft Edge Update Service (edgeupdate) (edgeupdate)'.
At C:\Script\test_stopped.ps1:5 char:61
+ ... t-Service -ComputerName $server -Name $stoppedService | Start-Service
+                                                             ~~~~~~~~~~~~~
    + CategoryInfo          : OpenError: (System.ServiceProcess.ServiceController:ServiceController) [Start-Service], ServiceCommandException
    + FullyQualifiedErrorId : StartServiceFailed,Microsoft.PowerShell.Commands.StartServiceCommand

Starting Server/Service: srv-01.domain.com/RemoteRegistry
Starting Server/Service: srv-01.domain.com/sppsvc
Starting Server/Service: srv-01.domain.com/wuauserv

I thought that if I use "-ne" then I just to have to add the names of the services which I want to exclude from checking and starting with comma separated and it should be working. Can someone help with my issue?
I've tried as well to put RemoteRegistry,brave,edgeupdate in "" but this doesn't help, with space or without after the comma, doesn't work as well. How can I add more services in the list of exclusions?

Thanks

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,344 questions
0 comments No comments
{count} votes

Accepted answer
  1. MotoX80 31,556 Reputation points
    2022-01-31T14:33:46.67+00:00

    Try this.

    $Ignore = @('RemoteRegistry', 'brave', 'edgeupdate')
    $stoppedSAervices = (Get-WmiObject Win32_Service  | Where-Object {$_.StartMode -eq 'Auto' -and $_.State -ne "Running" }).Name | Where-Object {$_ -notin $ignore}
    

1 additional answer

Sort by: Most helpful
  1. Borislav Vitanov 81 Reputation points
    2022-02-01T10:28:21.657+00:00

    Hi @MotoX80

    Thanks, the first one worked without problem. Still for me is unclear why PS ignored in my script the service names.

    Best regards