Hello everyone,

I’m working on a PowerShell script that will look at a list of server names and run a start-service from a list. If the service isn’t present on the server then continuing to the next one. I’m not real good with PS so any help would be appreciated. I’ve put what I have so far.

$servers = “Ilist of server names”

$service = “list of service names”

#$ErrorActionPreference = “SilentlyContinue”

ForEach ($computername in $servers) {

Get-Service -ComputerName $servers -Name $service | Start-Service -Verbose

}

4 Spice ups

$servers is the list. $computername is the current iteration of that list.

Your Get-Service should be referenceing $computername.

Also your ErrorActionPreference is commented out by the #.

I’m not in a place I can test this for you, but those are the obvious issues.

If you are going to do a list of services, you would have to separate them with a comma.

So something like $services=“winrm,spooler”

This has homework written all over it…

Your not using the current item variable from your for loop in your service query cmdlet.

I would create a another for loop inside that loop to loop through the starting of those services, that being said if there’s just two then I would just issue two cmds in the one for loop.

I’m sure greater minds will provide something else…

Even when the service does not exist it will continue

$servers = "list"
$service  =@('bits','vss','xxxx')
ForEach ($computername in $servers) 
{
Get-Service -ComputerName $computername -Name $service -ErrorAction SilentlyContinue |where{$_.Status -ne 'Running'}|
Start-Service -Verbose 
}

Few things you have missed, please check -ComputerName $computername

3 Spice ups

Hi, next time please post code in its proper tag :slight_smile:
Thanks
Sean

1 Spice up

I can see how it would read that way, but this is for work. I have 11 servers that are working together to host a software so each server has dependencies on other servers but the company that created the software doesn’t have any sort of startup process and we have yet to get a smooth shutdown and start up whenever we have to patch. One of the main issues is there are between 4 and 5 services on each server that fail to start up and prevent the software from functioning properly so I’m trying to automate that part of it.

Simply issuing a start for all of them will work. Ideally you probably want to build some error checking in to check and see if the in fact started. Also you can edit the services themselves on the servers and set them to retry on 1st and second failure.

Let’s say you get one service in a variable:
$myService = get-service “EventLog”
And why not; the Windows Event Log service is pretty important.
$myService is a service Object with a property named Status.
If you pipe $myService to Get-Member: $myService | GM # gm is the alias for get-member
you’ll see the properties, methods, et al for these objects.
Status property
Start method
Stop method

You get the idea.
Throw in a couple of variable assignments from the object and an if/else statement or two and you’re in control.

Untested code

$svc = get-service “eventlog”
$stat = $svc | select -exp status
If ($stat -ne “running”) {
$svc | Start-service
}