#Import-Module OperationsManager -Verbose
#=================================================================================
# This file gets Alerts from the Pure Array command line
# If you want to test it, remember to generate the alerts with severity either warning or critical,
# because in the XML file, I use severity to separate alerts.
# In our real alerts, the severity is either warning or critical or info (no alerts raised in the console).
#=================================================================================
function AlertsRule {
# Import the SCOM PowerShell module
$ScriptName = "AlertsRule.ps1"
$NUMBER_ALERT_FIELDS = 11
LoadOperationsManagerModule $ScriptName
# Gather script start time
$StartTime = Get-Date
# Gather who the script is running as
$WhoAmI = whoami
# Connect to FlashArray and get alert messages
$Password = $Password | ConvertTo-SecureString -AsPlainText -Force
try {
$IgnoreCertErrors = GetIgnoreCertErrors $true
$FlashArray = New-PfaArray -EndPoint $Endpoint -UserName $Username -Password $Password -ClientName $GLOBAL:clientName -ClientVersion $GLOBAL:mpVersion -IgnoreCertificateError:$IgnoreCertErrors -HttpTimeOutInMilliSeconds 60000
} catch {
Log $ScriptName $GLOBAL:ERROR_LEVEL "Connection to array failed on these credentials. $Username. Check that the PurePowershell cmdlet is installed, and your credentials are correct. Error: $_"
exit
}
# $CLIMessages is the String output of CLI. Only get all the open alerts, if there is no open messages, only output ""
$CLIMessages = New-PfaCLICommand -EndPoint $Endpoint -UserName $Username -Password $Password -CommandText "puremessage list --open --csv --notitle"
$CLIMessages = $CLIMessages.trim()
Log $ScriptName $Global:VERBOSE_LEVEL "CLI message output: $CLIMessages"
$Warning = "No"
# Get group of PureArray
$Group = Get-SCOMGroup -DisplayName "*Pure Array*"
# Get the instances of this particular array
$ArrayInstance = $Group.GetRelatedMonitoringObjects() | where {$_.DisplayName -eq $Endpoint}
# Get Alerts sorted with most recent first
$SCOMAlerts = Get-SCOMAlert -Instance $ArrayInstance -ResolutionState 0 | where {$_.name -like "*REST API*"}
$SCOMAlerts = $SCOMAlerts | Sort-Object -Descending LastModified
Log $ScriptName $GLOBAL:INFO_LEVEL "SCOM Alerts: $SCOMAlerts"
# If the alerts are null, we don't need to do anything or else we need to resolve the CLOSED alerts in the console
if ($SCOMAlerts -ne $null)
{
$allCLIMessages = New-PfaCLICommand -EndPoint $Endpoint -UserName $Username -Password $Password -CommandText "puremessage list --csv --notitle"
$allCLIMessages = $allCLIMessages.trim()
$allCLIMessages = $allCLIMessages.split("`n")
$ClosedCLIMessages = @()
foreach($allMessage in @($allCLIMessages).trim())
{
if(@($CLIMessages) -notcontains $allMessage)
{
$ClosedCLIMessages += $allMessage
}
}
# If $CLIMessages are null, just return bag with no warnings
if ($CLIMessages -eq $null -or $CLIMessages.Length -eq 0) {
Log $ScriptName $GLOBAL:INFO_LEVEL "No alert messages"
$Warning = "No"
$bag = $momapi.CreatePropertyBag()
$bag.AddValue('Warning',$Warning)
$bag
} else {
# Parse the output of the command line. The first line is attributes names, the last line is "" (empty string).
# The other lines in the $MessageArrays are open alert messages that are separated by commas
# All the fields(11): ID, Time, Severity, Category, Code, Component, Name, Event, Expected, Actual, Details
# $AlertMessages is an array of alert messages
$NewAlertMessages = $CLIMessages.Split("`n") | foreach {$_.trim()}
$length = @($NewAlertMessages).Length
Log $ScriptName $GLOBAL:WARNING_LEVEL "`nFound $length open alert messages"
# Adding new alerts
Log $ScriptName $GLOBAL:INFO_LEVEL "`nNo SCOM Alerts exist, adding all new CLI alerts...`n"
$Warning = "Yes"
foreach($AlertMessage in $NewAlertMessages) {
if ($AlertMessage.Length -eq 0) {
continue
}
$dict = Parse-Alert $AlertMessage
$bag = $momapi.CreatePropertyBag()
# Add the necessary properties to the object and add the object to the return bag
$bag.AddValue('Warning',$Warning)
$bag.AddValue('ID',$Id)
$bag.AddValue('TimeOpened',$TimeOpened)
$bag.AddValue('Severity',$Severity)
$bag.AddValue('Category',$Category)
$bag.AddValue('Code',$Code)
$bag.AddValue('Component',$Component)
$bag.AddValue('Name',$Name)
$bag.AddValue('Event',$Event)
$bag.AddValue('Expected',$Expected)
$bag.AddValue('Actual',$Actual)
$bag.AddValue('Details',$Details)
$bag.AddValue('URL',$URL)
Log $ScriptName $GLOBAL:WARNING_LEVEL "`nNew alert: $Code, go to $URL"
$bag
#}
}
}
$EndTime = Get-Date
$ScriptTime = ($EndTime - $StartTime).TotalSeconds
Log $ScriptName $GLOBAL:INFO_LEVEL "Script has completed at $Endpoint. Runtime was ($ScriptTime) seconds."