Monitor and alert Windows Services status

This script is extension of below script that can monitor the windows services from remote servers.

https://gallery.technet.microsoft.com/scriptcenter/Monitor-Report-5d1998d4

Now I have updated it to add a alert if services are not running.

Download and extract the script from below link , update alerting parameters.

https://gallery.technet.microsoft.com/scriptcenter/Monitor-and-alert-Windows-07c3d5a0

—————————————————————

$alert = “Yes” ###specify NO if you don’t want alerts

$report = “.\report.htm”

$smtphost = “smtp.test.com”

$from1 = “SystemsMonitoring@labtest.com”

$to = “VikasSukhija@labtest.com”

————————————————————-

Define variables to get services and servers data(example is in the download)

$EVServerList = Get-Content “.\EvServer.txt”

$EVServicesList = Get-Content “.\EvServices.txt”

$EVJserverList = Get-Content “.\EvJserver.txt”

$EVJservicesList = Get-Content “.\EvJservices.txt”

$EVDAserverList =  Get-Content “.\EvDAserver.txt”

$EVDAservicesList = Get-Content “.\EvDAservices.txt”

—————————————————————

browse to the end of the script and call the function as below

servicestatus $EVServerList $EVServicesList

servicestatus $EVJserverList $EVJservicesList

servicestatus $EVDAserverList $EVDAservicesList

—————————————————————

You can schedule the the script to runevery 15 mins or as per your requirements.(don’t for get to remove the pause statement from the batch file)

If you want to run it manually just run the batch file.

PowerShell
############################################################################# 
#       Author: Mahesh Sharma 
#       Reviewer: Vikas SUkhija       
#       Date: 06/10/2013 
#        Modified:06/23/2013 - made it to run from any Location 
#       Updated: 10/01/2017 - for alerting 
#       Description: Services Monitor 
############################################################################# 
$alert = "Yes" 
$report = ".\report.htm" 
$smtphost = "smtp.test.com" 
$from1 = "SystemsMonitoring@labtest.com" 
$to = "VikasSukhija@labtest.com" 
 
##############Funstions######################### 
function Send-Email 
{ 
    [CmdletBinding()] 
    param 
    ( 
        [Parameter(Mandatory = $true)] 
        $From, 
        [Parameter(Mandatory = $true)] 
        [array]$To, 
        [array]$bcc, 
        [array]$cc, 
        $body, 
        $subject, 
        $attachment, 
        [Parameter(Mandatory = $true)] 
        $smtpserver 
    ) 
     
    $message = new-object System.Net.Mail.MailMessage 
    $message.From = $from 
    if ($To -ne $null) 
    { 
        $To | ForEach-Object{ 
            $to1 = $_ 
            $to1 
            $message.To.Add($to1) 
        } 
    } 
    if ($cc -ne $null) 
    { 
        $cc | ForEach-Object{ 
            $cc1 = $_ 
            $cc1 
            $message.CC.Add($cc1) 
        } 
    } 
    if ($bcc -ne $null) 
    { 
        $bcc | ForEach-Object{ 
            $bcc1 = $_ 
            $bcc1 
            $message.bcc.Add($bcc1) 
        } 
    } 
    $message.IsBodyHtml = $True 
    if ($subject -ne $null) 
    { 
        $message.Subject = $Subject 
    } 
    if ($attachment -ne $null) 
    { 
        $attach = new-object Net.Mail.Attachment($attachment) 
        $message.Attachments.Add($attach) 
    } 
    if ($body -ne $null) 
    { 
        $message.body = $body 
    } 
    $smtp = new-object Net.Mail.SmtpClient($smtpserver) 
    $smtp.Send($message) 
} 
############################Define Variables for server & services############## 
$EVServerList = Get-Content ".\EvServer.txt" 
$EVServicesList = Get-Content ".\EvServices.txt" 
 
$EVJserverList = Get-Content ".\EvJserver.txt" 
$EVJservicesList = Get-Content ".\EvJservices.txt" 
 
$EVDAserverList =  Get-Content ".\EvDAserver.txt" 
$EVDAservicesList = Get-Content ".\EvDAservices.txt" 
############################################################################## 
 
$checkrep = Test-Path ".\report.htm"  
 
If ($checkrep -like "True") 
 
{ 
 
Remove-Item ".\report.htm" 
 
 
} 
 
New-Item ".\report.htm" -type file 
 
################################ADD HTML Content############################# 
 
 
Add-Content $report "<html>"  
Add-Content $report "<head>"  
Add-Content $report "<meta http-equiv='Content-Type' content='text/html; charset=iso-8859-1'>"  
Add-Content $report '<title>Service Status Report</title>'  
add-content $report '<STYLE TYPE="text/css">'  
add-content $report  "<!--"  
add-content $report  "td {"  
add-content $report  "font-family: Tahoma;"  
add-content $report  "font-size: 11px;"  
add-content $report  "border-top: 1px solid #999999;"  
add-content $report  "border-right: 1px solid #999999;"  
add-content $report  "border-bottom: 1px solid #999999;"  
add-content $report  "border-left: 1px solid #999999;"  
add-content $report  "padding-top: 0px;"  
add-content $report  "padding-right: 0px;"  
add-content $report  "padding-bottom: 0px;"  
add-content $report  "padding-left: 0px;"  
add-content $report  "}"  
add-content $report  "body {"  
add-content $report  "margin-left: 5px;"  
add-content $report  "margin-top: 5px;"  
add-content $report  "margin-right: 0px;"  
add-content $report  "margin-bottom: 10px;"  
add-content $report  ""  
add-content $report  "table {"  
add-content $report  "border: thin solid #000000;"  
add-content $report  "}"  
add-content $report  "-->"  
add-content $report  "</style>"  
Add-Content $report "</head>"  
Add-Content $report "<body>"  
add-content $report  "<table width='100%'>"  
add-content $report  "<tr bgcolor='Lavender'>"  
add-content $report  "<td colspan='7' height='25' align='center'>"  
add-content $report  "<font face='tahoma' color='#003399' size='4'><strong>Service Status Report</strong></font>"  
add-content $report  "</td>"  
add-content $report  "</tr>"  
add-content $report  "</table>"  
  
add-content $report  "<table width='100%'>"  
Add-Content $report "<tr bgcolor='IndianRed'>"  
Add-Content $report  "<td width='10%' align='center'><B>Server Name</B></td>"  
Add-Content $report "<td width='50%' align='center'><B>Service Name</B></td>"  
Add-Content $report  "<td width='10%' align='center'><B>Status</B></td>"  
Add-Content $report "</tr>"  
 
 
######################################################################################################## 
 
################################## Get Services Status ################################################# 
 
Function servicestatus ($serverlist$serviceslist) 
 
{ 
 
foreach ($machineName in $serverlist)  
 
 {  
  foreach ($service in $serviceslist) 
     { 
   
      $serviceStatus = get-service -ComputerName $machineName -Name $service 
     
         if ($serviceStatus.status -eq "Running") { 
  
         Write-Host $machineName `t $serviceStatus.name `t $serviceStatus.status -ForegroundColor Green  
         $svcName = $serviceStatus.name  
         $svcState = $serviceStatus.status          
         Add-Content $report "<tr>"  
         Add-Content $report "<td bgcolor= 'GainsBoro' align=center>  <B> $machineName</B></td>"  
         Add-Content $report "<td bgcolor= 'GainsBoro' align=center>  <B>$svcName</B></td>"  
         Add-Content $report "<td bgcolor= 'Aquamarine' align=center><B>$svcState</B></td>"  
         Add-Content $report "</tr>"  
               
                                                   } 
 
            else  
                                                   { 
                Write-Host $machineName `t $serviceStatus.name `t $serviceStatus.status -ForegroundColor Red 
                if ($alert -eq "yes") 
                { 
                    $sename = $serviceStatus.name 
                    $sestatus = $serviceStatus.status 
                    Send-Email -From $from1 -To $to -subject "Open Critical: $machineName $sename $sestatus" -smtpserver $smtphost 
                } 
                $svcName = $serviceStatus.name  
         $svcState = $serviceStatus.status           
         Add-Content $report "<tr>"  
         Add-Content $report "<td bgcolor= 'GainsBoro' align=center>$machineName</td>"  
         Add-Content $report "<td bgcolor= 'GainsBoro' align=center>$svcName</td>"  
         Add-Content $report "<td bgcolor= 'Red' align=center><B>$svcState</B></td>" 
         Add-Content $report "</tr>" 
                 
          
                                                  }  
 
              
 
       }  
 
 
 }  
 
} 
 
############################################Call Function############################################# 
 
servicestatus $EVServerList $EVServicesList 
servicestatus $EVJserverList $EVJservicesList 
servicestatus $EVDAserverList $EVDAservicesList 
 
############################################Close HTMl Tables######################################### 
 
 
Add-content $report  "</table>"  
Add-Content $report "</body>"  
Add-Content $report "</html>"  
 
##################################################################################################### 
 Thanks for reading
  Vikas Sukhija

Leave a comment