mystikal1000
asked on
Script to start a windows service if stopped.
I need a PS that will check a txt file of servers and verify if the Spooler service is stopped, if so start the service. I need this asap.
Thanks!
Thanks!
ASKER
I am receiving an error... I am trying to use a different service name instead of spooler.
Receive this error
Workflow Start-Spooler {
Get-Service -Name "Hol CoreService" | Start-Service -PassThru
}
$computerNames = Get-Content C:\temp\computers.txt
Start-Service "Hol CoreService" -PSComputerName $computerNames
Receive this error
Start-Service : A parameter cannot be found that matches parameter name 'PSComputerName'.
At line:5 char:35
+ Start-Service "Hol EmCoreService" -PSComputerName $computerNames
+ ~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Start-Service], ParameterBindingException
+ FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.Pow erShell.Co mmands.Sta rtServiceC ommand
Because you changed the workflow call and replaced with a call to the standards cmdlet (and the names in your error message and the script differ).
Note: The service name must be the "short" name (as seen in the "Service Name" field in the properties of the Services MMC), not the display name.
Note: The service name must be the "short" name (as seen in the "Service Name" field in the properties of the Services MMC), not the display name.
Workflow Start-MyService {
Get-Service -Name "Hol EmCoreService" | Start-Service -PassThru
}
$computerNames = Get-Content C:\temp\ComputerList.txt
Start-MyService -PSComputerName $computerNames
ASKER
Yes that is the correct service name, yes its two words. When I run it, I receive a different error.
A parameter cannot be found that matches parameter name 'InformationAction'.
+ CategoryInfo : InvalidArgument: (:) [Get-Service], ParameterBindingException
+ FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.Pow erShell.Co mmands.Get ServiceCom mand
+ PSComputerName : [localhost]
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I am on Windows 10 1703, 5.1 WMF.
Your machine may be there, but not (all of) the target machines you're running this against ...
ASKER
Makes sense!
I ran the script and some machines are not online and keeps looping waiting for service...anyway to bypass it so it goes to the next server?
I ran the script and some machines are not online and keeps looping waiting for service...anyway to bypass it so it goes to the next server?
Yes, an example of what @odba wants you to have in the "ComputerList.txt" file is like this:
C:\temp\ComputerList.txt
With one server/computer per line.
C:\temp\ComputerList.txt
Computer1
DomainController1
WindowsServer02
With one server/computer per line.
Should eventually timeout with an error and then continue with the next machine.
This pings first:
This pings first:
Get-Content C:\temp\ComputerList.txt | ForEach-Object {
Write-Host "Processing $($_) ..." -NoNewline
Try {
If (-not (Test-Connection -Count 1 -ComputerName $_ -Quiet)) {
Throw "Not responding to ping!"
}
Get-Service -Name "Hol EmCoreService" -ComputerName $_ -ErrorAction Stop | Start-Service -ErrorAction Stop
Write-Host " OK." -ForegroundColor Green
} Catch {
Write-Host " ERROR: $($_.Exception.Message)" -ForegroundColor Red
}
}
Open in new window