Hi Team,

I would like to seek you advise if my script are correct.

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,

Please advise if the loop construction of my script are correct.

Thanks

clear-host

$gdslist=Get-Content c:\temp\list.txt
foreach($tomservice in $gdslist){
Try{
$tomcat=get-service -ComputerName $tomservice|where {$_.Name -eq  "Tomcat8"}
if($tomcat.Status -eq "Running"){
Write-host "$($tomcat.DisplayName) is Running on $($tomservice) server" -ForegroundColor Green
}else{
Write-host "$($tomcat.DisplayName) is stopped State on $($tomservice) server" -ForegroundColor Red
''
write-host "Starting $($tomcat.DisplayName) to $($tomservice)" -ForegroundColor Red -NoNewline
$started=get-service -ComputerName $($tomservice) $($tomcat.ServiceName)|Set-Service -Status "Running"
Write-Host "..Done!!!" -ForegroundColor green
''
Start-Sleep -Seconds 3

foreach($validationn in $gdslist){
write-host "Validating $($tomcat.ServiceName) in $($validationn) " -ForegroundColor Red -NoNewline

$tomcat=get-service -ComputerName $validationn|where {$_.Name -eq  "Tomcat8"}
write-host "...Done!!" -ForegroundColor Green
if($tomcat.Status -eq "Running"){
Write-host "$($tomcat.DisplayName) is Running on $($tomservice) server" -ForegroundColor Green
}

}

}
}catch {$null}

}
2 Spice ups

make it a lil simple

$gdslist=Get-Content c:\temp\list.txt
foreach($tomservice in $gdslist){
$ErrorActionPreference='stoP'
Try{
$check=get-service -ComputerName $tomservice -Name 'Tomcat8'
if ($check.Status -eq "Running")
{
Write-host "$($check.DisplayName) is Running on $($tomservice) server" -ForegroundColor Green
}
else{
Write-host "$($check.DisplayName) is stopped State on $($tomservice) server" -ForegroundColor Red

write-host "Starting $($check.DisplayName) to $($tomservice)" -ForegroundColor Red -NoNewline
$started=get-service -ComputerName $($tomservice) $($check.Name)|Start-Service -Verbose
Start-Sleep -Seconds 3
Write-Host "..Done!!!" -ForegroundColor green
$check=get-service -ComputerName $($tomservice) $($check.Name)
IF($check.Status -eq 'running'){
'Validation Sucess'
}
Else{
"Tomcat Not started on $($tomservice)"
}
Start-Sleep -Seconds 3

}
}catch {$null}

}

more https://community.spiceworks.com/scripts/show/4071-monitor-service-with-powershell-html-email-report

Ill try this one. And get back to you
Thanks

Thanks Jitensh.