Forum Discussion
PowerShell script for alert if app down
- Apr 13, 2021
Use the following script with specified service name to alert
##############################
# Service-Alert.PS1
# # This script Auto stopped & Stop Pending Service notification
##############################
#Machine to be monitored$Computer = "Srv1"
#Create an array of all services running
$GetService = get-service -ComputerName $Computer
#Create a subset of the previous array for services you want to monitor
$ServiceArray = "Service name"
foreach ($Service in $GetService)
{
foreach ($srv in $ServiceArray)
{
if ($Service.name -eq $srv)
{
#check if a service is hungif ($Service.status -eq "StopPending")
{
# Email to notify if a service is down & Email Body Set Here, Note You can use HTML, including Images.
$body = "<b><font face=Times New Roman><font color=Black</font>"
$body = "The $srv service was found stopped on $Computer, Kindly restart the Service to resume <br>"
Send-Mailmessage -body $body -bodyasHTML -Priority High -to "name@contso.com" -Subject "$srv is hung on $Computer" -from "Autoserverappstatuse@contoso.com" -SmtpServer "0.0.0.0.0"
}# check if a service is stopped
elseif ($Service.status -eq "Stopped")
{
# Email to notify if a service is down & Email Body Set Here, Note You can use HTML, including Images.$body = "<b><font face=Times New Roman><font color=Black</font>"
$body = "The $srv service was found stopped on $Computer, Kindly restart the Service to resume <br>"Send-Mailmessage -body $body -bodyasHTML -Priority High -to "name@contso.com" -Subject "$srv is stopped on $Computer" -from "Autoserverappstatuse@contoso.com" -SmtpServer "0.0.0.0.0"
}
}
}
}
Use the following script with specified service name to alert
##############################
# Service-Alert.PS1
# # This script Auto stopped & Stop Pending Service notification
##############################
#Machine to be monitored
$Computer = "Srv1"
#Create an array of all services running
$GetService = get-service -ComputerName $Computer
#Create a subset of the previous array for services you want to monitor
$ServiceArray = "Service name"
foreach ($Service in $GetService)
{
foreach ($srv in $ServiceArray)
{
if ($Service.name -eq $srv)
{
#check if a service is hung
if ($Service.status -eq "StopPending")
{
# Email to notify if a service is down & Email Body Set Here, Note You can use HTML, including Images.
$body = "<b><font face=Times New Roman><font color=Black</font>"
$body = "The $srv service was found stopped on $Computer, Kindly restart the Service to resume <br>"
Send-Mailmessage -body $body -bodyasHTML -Priority High -to "name@contso.com" -Subject "$srv is hung on $Computer" -from "Autoserverappstatuse@contoso.com" -SmtpServer "0.0.0.0.0"
}
# check if a service is stopped
elseif ($Service.status -eq "Stopped")
{
# Email to notify if a service is down & Email Body Set Here, Note You can use HTML, including Images.
$body = "<b><font face=Times New Roman><font color=Black</font>"
$body = "The $srv service was found stopped on $Computer, Kindly restart the Service to resume <br>"
Send-Mailmessage -body $body -bodyasHTML -Priority High -to "name@contso.com" -Subject "$srv is stopped on $Computer" -from "Autoserverappstatuse@contoso.com" -SmtpServer "0.0.0.0.0"
}
}
}
}