#Connect to OM server
AddTraceMessage -message "Getting OM management server to connect to."
$managementServerName = 'localhost'
'if($mg -ne $null)
{
$managementServerName = $mg.OpsMgrServerName;
}
else
{
Throw [System.NullReferenceException] "Failed to connect get OM MG connection. This is fatal error, script will fail.";
}
'
AddTraceMessage -message "Connecting to $managementServerName OM management server."
if($results -ne $null)
{
$diagnoseResultsCount = $results.DiagnoseResults.Count
AddTraceMessage -message "Number of storage Diagnostics results found = $diagnoseResultsCount";
}
foreach($diagnosticsResult in $results.DiagnoseResults)
{
$storageFaultId = $diagnosticsResult.FaultId
# it is very unlikely that we will have result with a null or empty fault id string,
# but a safe check because this script closes all alerts based on fault id pattern, and in case of empty faultid string it may end up closing all active OM alerts for this object
if([string]::IsNullOrEmpty($storageFaultId))
{
AddTraceMessage -message "Null or empty storageFaultId in the Diagnostics Result. Skipping this fault";
continue
}
AddTraceMessage -message "An existing fault found on storage object $storageObjectUniqueId : $storageFaultId";
# Check if there are SCOM alerts for this fault Id
$FaultIdpatternToLookForInAlertContext = ("*" + $diagnosticsResult.FaultId +":" + $storageObjectUniqueId + "*");
AddTraceMessage -message "Searching OM Alert with this pattern: `n $FaultIdpatternToLookForInAlertContext"
$omAlertsForStorageFault = (Get-SCOMAlert -Instance $scomMonitoringObject) | Where-Object {$_.Context -like $FaultIdpatternToLookForInAlertContext}
if($omAlertsForStorageFault.Count -ne 0) #Alert already present
{
AddTraceMessage -message "No need to generate an alert for fault id $storageFaultId as there is already an alert generated for this fault Id."
}
else
{
# Delta between faults in Diagnose() results that are not in OMAlerts
AddTraceMessage -message "No alert found for fault id storageFaultId. Adding this fault Id to the list of new alerts to be generated."
$newAlertsNeeded.Add($diagnosticsResult.FaultId, $diagnosticsResult);
}
}
if($newAlertsNeeded -ne $null)
{
$numberOfNewAlerts = $newAlertsNeeded.Count;
AddTraceMessage -message "Number of alerts to be generated = $numberOfNewAlerts"
}
#Generate new alerts for new faults
foreach($newAlertsNeededItem in $newAlertsNeeded.GetEnumerator())
{
$faultIdForNewAlert = $newAlertsNeededItem.Key;
$diagnosticsResultForNewAlert = $newAlertsNeededItem.Value;
#Create new alert property bag data
$propertyBag = $momAPI.CreatePropertyBag()
$propertyBag.AddValue("FaultId", $diagnosticsResultForNewAlert.FaultId);
$propertyBag.AddValue("FaultingObjectDescription", $diagnosticsResultForNewAlert.FaultingObjectDescription);
$propertyBag.AddValue("Reason", $diagnosticsResultForNewAlert.Reason);
$propertyBag.AddValue("RecommendedActions", $diagnosticsResultForNewAlert.RecommendedActions -join "`n");
$propertyBag.AddValue("SourceUniqueId", $storageObjectUniqueId);
Try
{
# Get All active storage alerts
$allOMAlerts = Get-SCOMAlert -Instance $scomMonitoringObject | where-object {($_.Context -like '*FaultId*') -and ($_.ResolutionState -eq $ResolutionStateActive)}
# Get list of all FaultIds from all active storage alerts
$allOMAlertsFaultIds = ($allOMAlerts | ForEach-Object { $context = [xml]$_.Context; ($context.DataItem.Property | where {$_.Name -eq "FaultId"}).InnerText})
AddTraceMessage -message "Found alerts with storage faults on this object.`nAlert exist for these FaultIds = $allOMAlertsFaultIds"
$allDiagnosticcsResultsFaults = $results.DiagnoseResults.FaultId
AddTraceMessage -message "Diagnose() found these faults are still present = $allDiagnosticcsResultsFaults"
# Delta between OMAlerts that are not in the Diagnose() results anymore
$closeAlertsForTheseFaults = $allOMAlertsFaultIds | ?{$results.DiagnoseResults.FaultId -notcontains $_}
$numberOfStaleAlerts = $closeAlertsForTheseFaults.Count
AddTraceMessage -message "Number of alerts to be closed = $numberOfStaleAlerts"
# Close Alerts
foreach($faultIdToBeClosed in $closeAlertsForTheseFaults)
{
CloseAlert -faultId $faultIdToBeClosed
}
}
Catch
{
$momAPI.LogScriptEvent("DiagnoseFileShareVolume.ps1", $GenericWarningEvent, 0, "Failed to close active storage alerts for closed storage faults, please close the alert manually once the recommended actions for any such alerts are taken. Exception message: " + $_.Exception.Message);
}