Hi Team,<\/p>\n
I would like to seek you advise if my script are correct.<\/p>\n
My script is setting the service to running if found stopped, and it will query again all the target machine to check or display the service status,<\/p>\n
Please advise if the loop construction of my script are correct.<\/p>\n
Thanks<\/p>\n
clear-host\n\n$gdslist=Get-Content c:\\temp\\list.txt\nforeach($tomservice in $gdslist){\nTry{\n$tomcat=get-service -ComputerName $tomservice|where {$_.Name -eq \"Tomcat8\"}\nif($tomcat.Status -eq \"Running\"){\nWrite-host \"$($tomcat.DisplayName) is Running on $($tomservice) server\" -ForegroundColor Green\n}else{\nWrite-host \"$($tomcat.DisplayName) is stopped State on $($tomservice) server\" -ForegroundColor Red\n''\nwrite-host \"Starting $($tomcat.DisplayName) to $($tomservice)\" -ForegroundColor Red -NoNewline\n$started=get-service -ComputerName $($tomservice) $($tomcat.ServiceName)|Set-Service -Status \"Running\"\nWrite-Host \"..Done!!!\" -ForegroundColor green\n''\nStart-Sleep -Seconds 3\n\nforeach($validationn in $gdslist){\nwrite-host \"Validating $($tomcat.ServiceName) in $($validationn) \" -ForegroundColor Red -NoNewline\n\n$tomcat=get-service -ComputerName $validationn|where {$_.Name -eq \"Tomcat8\"}\nwrite-host \"...Done!!\" -ForegroundColor Green\nif($tomcat.Status -eq \"Running\"){\nWrite-host \"$($tomcat.DisplayName) is Running on $($tomservice) server\" -ForegroundColor Green\n}\n\n}\n\n}\n}catch {$null}\n\n}\n<\/code><\/pre>","upvoteCount":2,"answerCount":4,"datePublished":"2021-04-02T06:08:16.000Z","author":{"@type":"Person","name":"marlon3","url":"https://community.spiceworks.com/u/marlon3"},"acceptedAnswer":{"@type":"Answer","text":"make it a lil simple<\/p>\n
$gdslist=Get-Content c:\\temp\\list.txt\nforeach($tomservice in $gdslist){\n$ErrorActionPreference='stoP'\nTry{\n$check=get-service -ComputerName $tomservice -Name 'Tomcat8'\nif ($check.Status -eq \"Running\")\n{\nWrite-host \"$($check.DisplayName) is Running on $($tomservice) server\" -ForegroundColor Green\n}\nelse{\nWrite-host \"$($check.DisplayName) is stopped State on $($tomservice) server\" -ForegroundColor Red\n\nwrite-host \"Starting $($check.DisplayName) to $($tomservice)\" -ForegroundColor Red -NoNewline\n$started=get-service -ComputerName $($tomservice) $($check.Name)|Start-Service -Verbose\nStart-Sleep -Seconds 3\nWrite-Host \"..Done!!!\" -ForegroundColor green\n$check=get-service -ComputerName $($tomservice) $($check.Name)\nIF($check.Status -eq 'running'){\n'Validation Sucess'\n}\nElse{\n\"Tomcat Not started on $($tomservice)\"\n}\nStart-Sleep -Seconds 3\n\n}\n}catch {$null}\n\n}\n<\/code><\/pre>\n