How about doing it this way? It creates a Datatable, goes through all the Alerts and checks if an Alert of the same type is already in there. If it doesn't, it just creates the entry. If it does, it just merges the Count (Repeatcount + 1).
Check it out and tell me if this does what you want it to do, then I'll document it more detailed.
#Create Datatable
$AlertTable = New-Object System.Data.DataTable "AlertTable"
$AlertTable.Columns.Add((New-Object System.Data.DataColumn ID,([string])))
$AlertTable.Columns.Add((New-Object System.Data.DataColumn Name,([string])))
$AlertTable.Columns.Add((New-Object System.Data.DataColumn AlertCount,([int])))
$AlertTable.Columns.Add((New-Object System.Data.DataColumn IsMonitorAlert,([string])))
foreach ($Alert in (Get-Alert))
{
#Check if Alert exists already.
$AlertExists = $false
foreach ($Row in $AlertTable.Rows)
{
if ($Row.ID -eq $Alert.MonitoringRuleId.ToString())
{
$AlertExists = $true
#In case it does, we just merge the Repeatcount
$Row.AlertCount = $Row.AlertCount + ($Alert.RepeatCount + 1)
}
}
if ($AlertExists)
{
}
else
{
#If the Alert doesn't exist, we add it to the DataTable.
$NewRow = $AlertTable.NewRow()
$NewRow.ID = $Alert.MonitoringRuleId.ToString()
$NewRow.Name = $Alert.Name
$NewRow.AlertCount = ($Alert.RepeatCount + 1)
$NewRow.IsMonitorAlert = $Alert.IsMonitorAlert
$AlertTable.Rows.Add($NewRow)
}
}
$AlertTable = ($AlertTable | sort AlertCount -Descending)
$AlertTable | format-table -AutoSize | select-object -first 10