Forum Discussion

Chandra318's avatar
Chandra318
Copper Contributor
Mar 04, 2021
Solved

PowerShell script for alert if app down

Hi All, I have 10 vms and an app running on all. If any app or vm down need get mail to specified. how i can get this powershell? pls share your ideas...  
  • Seshadrr's avatar
    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 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"


    }  
      }
        }
           }

Resources