Hello all, I posted about this previously but it was decided to take this a step further.<\/p>\n
We have servers with services that do not always start on their own. They start if stopped, but they do not always start on their own; even after a reinstall and setting the service to a delayed start. Others are working on this part.<\/p>\n
In the meantime, we would like a script that starts the services if stopped, waits, checks again, and emails out the results to let us know the services are started and/or others failed to start. I have gotten up to the script waiting and confirming the service status again and I am now stuck. I am looking into this on my own, but I am still very new to Powershell.<\/p>\n
$ServiceName = 'service1','service2'\n$arrService = Get-Service -Name $ServiceName\n\nwhile ($arrService.Status -ne 'Running')\n{\n\n Start-Service $ServiceName\n write-host $arrService.status\n write-host 'Services starting'\n Start-Sleep -seconds 60\n $arrService.Refresh()\n if ($arrService.Status -eq 'Running')\n {\n Write-Host 'Services are now Running'\n }\n\n<\/code><\/pre>\nI’m only utilizing a script because it seems the most reliable (in this instance), and it’s free. However, if anyone knows of a free product that does this same thing, that is also welcome.<\/p>\n
Thank you all.<\/p>","upvoteCount":4,"answerCount":12,"datePublished":"2021-06-11T16:21:06.000Z","author":{"@type":"Person","name":"Jackal-Lear","url":"https://community.spiceworks.com/u/Jackal-Lear"},"acceptedAnswer":{"@type":"Answer","text":"
i have something like this, not sure if that’s overkill<\/p>\n
clear-host\n$services = 'service1','service2'\n\n$serviceReport = \nforeach($service in $services){\n $serviceStatus = Get-service $service\n \n if($serviceStatus.status -eq 'running'){\n $serviceStatus\n }\n else{\n start-service $service\n $statusReport = \n for($c=0;$c -lt 6;$c++){\n $serviceStatus = Get-service $service\n Start-Sleep -Seconds 10\n if($serviceStatus.status -eq 'running'){\n $serviceStatus\n break\n }\n }\n $serviceStatus\n }\n}\n\n$serviceReport \n\n$mail = @{\n from = \"[email protected]\"\n to = \"[email protected]\"\n subject = \"Service Status\"\n smtpserver = \"smtp.server.domain\"\n port = \"25\"\n body = $serviceReport | selectobject status,name | ConvertTo-Html | out-string\n bodyAsHTML = $true\n }\nSend-MailMessage @mail\n\n<\/code><\/pre>","upvoteCount":1,"datePublished":"2021-06-11T18:12:44.000Z","url":"https://community.spiceworks.com/t/powershell-check-service-start-if-needed-email-results/802539/6","author":{"@type":"Person","name":"Neally","url":"https://community.spiceworks.com/u/Neally"}},"suggestedAnswer":[{"@type":"Answer","text":"Hello all, I posted about this previously but it was decided to take this a step further.<\/p>\n
We have servers with services that do not always start on their own. They start if stopped, but they do not always start on their own; even after a reinstall and setting the service to a delayed start. Others are working on this part.<\/p>\n
In the meantime, we would like a script that starts the services if stopped, waits, checks again, and emails out the results to let us know the services are started and/or others failed to start. I have gotten up to the script waiting and confirming the service status again and I am now stuck. I am looking into this on my own, but I am still very new to Powershell.<\/p>\n
$ServiceName = 'service1','service2'\n$arrService = Get-Service -Name $ServiceName\n\nwhile ($arrService.Status -ne 'Running')\n{\n\n Start-Service $ServiceName\n write-host $arrService.status\n write-host 'Services starting'\n Start-Sleep -seconds 60\n $arrService.Refresh()\n if ($arrService.Status -eq 'Running')\n {\n Write-Host 'Services are now Running'\n }\n\n<\/code><\/pre>\nI’m only utilizing a script because it seems the most reliable (in this instance), and it’s free. However, if anyone knows of a free product that does this same thing, that is also welcome.<\/p>\n
Thank you all.<\/p>","upvoteCount":4,"datePublished":"2021-06-11T16:21:06.000Z","url":"https://community.spiceworks.com/t/powershell-check-service-start-if-needed-email-results/802539/1","author":{"@type":"Person","name":"Jackal-Lear","url":"https://community.spiceworks.com/u/Jackal-Lear"}},{"@type":"Answer","text":"