Link to home
Start Free TrialLog in
Avatar of mystikal1000
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!
Avatar of oBdA
oBdA

Brute force:
Workflow Start-Spooler {
	Get-Service -Name Spooler | Start-Service -PassThru
}
$computerNames = Get-Content C:\temp\ComputerList.txt
Start-Spooler -PSComputerName $computerNames

Open in new window

Avatar of mystikal1000

ASKER

I am receiving an error...  I am trying to use a different service name instead of spooler.

Workflow Start-Spooler {
	Get-Service -Name "Hol CoreService" | Start-Service -PassThru
}
$computerNames = Get-Content C:\temp\computers.txt
Start-Service "Hol CoreService" -PSComputerName $computerNames

Open in new window


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.PowerShell.Commands.StartServiceCommand
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.
Workflow Start-MyService {
	Get-Service -Name "Hol EmCoreService" | Start-Service -PassThru
}
$computerNames = Get-Content C:\temp\ComputerList.txt
Start-MyService -PSComputerName $computerNames 

Open in new window

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.PowerShell.Commands.GetServiceCommand
    + PSComputerName        : [localhost]
ASKER CERTIFIED SOLUTION
Avatar of oBdA
oBdA

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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 ...
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?
Yes, an example of what @odba wants you to have in the "ComputerList.txt" file is like this:

C:\temp\ComputerList.txt
Computer1
DomainController1
WindowsServer02

Open in new window


With one server/computer per line.
Should eventually timeout with an error and then continue with the next machine.
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