Hello everyone,<\/p>\n
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.<\/p>\n
$servers = “Ilist of server names”<\/p>\n
$service = “list of service names”<\/p>\n
#$ErrorActionPreference = “SilentlyContinue”<\/p>\n
ForEach ($computername in $servers) {<\/p>\n
Get-Service -ComputerName $servers -Name $service | Start-Service -Verbose<\/p>\n
}<\/p>","upvoteCount":4,"answerCount":8,"datePublished":"2019-06-26T01:46:08.000Z","author":{"@type":"Person","name":"spiceuser-f9r4j","url":"https://community.spiceworks.com/u/spiceuser-f9r4j"},"suggestedAnswer":[{"@type":"Answer","text":"
Hello everyone,<\/p>\n
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.<\/p>\n
$servers = “Ilist of server names”<\/p>\n
$service = “list of service names”<\/p>\n
#$ErrorActionPreference = “SilentlyContinue”<\/p>\n
ForEach ($computername in $servers) {<\/p>\n
Get-Service -ComputerName $servers -Name $service | Start-Service -Verbose<\/p>\n
}<\/p>","upvoteCount":4,"datePublished":"2019-06-26T01:46:08.000Z","url":"https://community.spiceworks.com/t/powershell-script-help/718165/1","author":{"@type":"Person","name":"spiceuser-f9r4j","url":"https://community.spiceworks.com/u/spiceuser-f9r4j"}},{"@type":"Answer","text":"
$servers is the list. $computername is the current iteration of that list.<\/p>\n
Your Get-Service should be referenceing $computername.<\/p>\n
Also your ErrorActionPreference is commented out by the #.<\/p>\n
I’m not in a place I can test this for you, but those are the obvious issues.<\/p>\n
If you are going to do a list of services, you would have to separate them with a comma.<\/p>\n
So something like $services=“winrm,spooler”<\/p>","upvoteCount":0,"datePublished":"2019-06-26T02:32:03.000Z","url":"https://community.spiceworks.com/t/powershell-script-help/718165/2","author":{"@type":"Person","name":"PatrickFarrell","url":"https://community.spiceworks.com/u/PatrickFarrell"}},{"@type":"Answer","text":"
This has homework written all over it…<\/p>\n
Your not using the current item variable from your for loop in your service query cmdlet.<\/p>\n
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.<\/p>\n
I’m sure greater minds will provide something else…<\/p>","upvoteCount":0,"datePublished":"2019-06-26T04:27:07.000Z","url":"https://community.spiceworks.com/t/powershell-script-help/718165/3","author":{"@type":"Person","name":"austenlowe2","url":"https://community.spiceworks.com/u/austenlowe2"}},{"@type":"Answer","text":"
Even when the service does not exist it will continue<\/p>\n
$servers = \"list\"\n$service =@('bits','vss','xxxx')\nForEach ($computername in $servers) \n{\nGet-Service -ComputerName $computername -Name $service -ErrorAction SilentlyContinue |where{$_.Status -ne 'Running'}|\nStart-Service -Verbose \n}\n<\/code><\/pre>\nFew things you have missed, please check -ComputerName $computername<\/p>","upvoteCount":3,"datePublished":"2019-06-26T04:30:11.000Z","url":"https://community.spiceworks.com/t/powershell-script-help/718165/4","author":{"@type":"Person","name":"jitensh","url":"https://community.spiceworks.com/u/jitensh"}},{"@type":"Answer","text":"