Table of Contents



Introduction



Sometimes it can be useful to have a daily email of all alerts older than a specified time range  that are "New".   The following script will check SCOM for all alerts with a resolution state of 0 and older than 48 hours.  It also splits it up in to Rule-based alerts and Monitor alerts.
Add this script into a scheduled task to automate it daily.

Script


# MUOverdueAlertsNotication.ps1
# This script will gather alerts older than 48 hours and send an email
# Script by Darren Joyce, July 2015
 
 
function mailresults
{
    [CmdletBinding()]                   # Allows us to define the ErrorAction to be taken when run
    Param()
    $Sent = $false                      # Flags mail as unsent
    [int]$Retrycount = "0"
     
    # Creates a loop that will attempt to retry sending of mail every 5 seconds for 5 attempts.
     
    do
    {
        try
        {
            #Sends mail
$mailrecipients
            send-mailmessage -To $mailrecipients -Subject $mailsubject -bodyAsHTML -body "$mailbody" -From sendermail.xxxxxx.com -smtpServer smtp.xxxxxx.com
            $Sent = $true
            "Mail retry count: " + $Retrycount
        }
        catch                           # Catches any errors that may occur
        {
            if ($Retrycount -gt 5)      # Fails sending only if error count is larger than 5 attempts
            {
                Write-Host "Email failed after 5 attempts."
                $Stoploop = $true
            }
            else
            {
                Start-Sleep -Seconds 5  # Sleeps 5 seconds before re-attempt
                $Retrycount = $Retrycount + 1
            }
        }
    }
    While ($Sent -eq $false)
}
 
# Set up some parameters. These can be changed to suit your preference.
$age = "48"
$mailrecipients = "someone@email.com", "someoneelse@email.com"
$mailsubject = "Open SCOM alerts longer than $age hours"
 
#Set up the email stylesheet
$style = "<style>"
$style = $style + "BODY{background-color:white;font:12px segoe UI;}"
$style = $style + "TABLE{border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;font:12px segoe UI;}"
$style = $style + "TH{border-width: 1px;padding: 0px;border-style: solid;border-color: black;background-color:thistle}"
$style = $style + "TD{border-width: 1px;padding: 0px;border-style: solid;border-color: black;}"
$style = $style + "</style>"
 
# gather monitor alerts
$monitoralerts = Get-SCOMAlert -criteria "ResolutionState=0 and IsMonitorAlert=1" | where {$_.severity -eq "Error" -and $_.TimeRaised -le ((Get-Date).addhours(-$age)).ToUniversalTime()} | select Name,MonitoringObjectPath, MonitoringObjectDisplayName, TimeRaised
$monitoralerts = $monitoralerts | ConvertTo-Html -head $style -body "<h3>The following Monitor alerts are more than $age hours old<h3>"
 
# gather rule-based alerts
$rulealerts = Get-SCOMAlert -criteria "ResolutionState=0 and IsMonitorAlert=0" | where {$_.severity -eq "Error" -and $_.TimeRaised -le ((Get-Date).addhours(-$age)).ToUniversalTime()} | select Name,MonitoringObjectPath, MonitoringObjectDisplayName, TimeRaised
$rulealerts = $rulealerts | ConvertTo-Html -head $style -PreContent "<h3>The following Rule-based alerts are more than $age hours old<h3>"
 
# assemble the email.
$mailbody = "$monitoralerts <br>$rulealerts"
 
#send the mail
MailResults