# ============================================================================================== # # Microsoft PowerShell Source File -- Created with SAPIEN Technologies PrimalScript 4.1 # # NAME: SCOM-Resolve-HardwareAlerts.ps1 # # AUTHOR: Jeremy D. Pavleck , JPavleck@GMail.com # DATE : 6/11/2008 # # COMMENT: When run, will gather all open HP alerts and mark them as resolved, setting a user # defined comment as well. It will then log to the event viewer it has done so. # # NOTES: The "Object Name" we use to determine what rules we want to resolve comes from the # MonitoringObjectFullName field of Get-Alert. # Also, you'll need to either set this command to start in your SCOM2007 dir (By default # C:\Program Files\System Center 2007 or edit Microsoft.EnterpriseManagement.OperationsManager.ClientShell.Startup.ps1 # in said directory and change the dot source reference from current directory to the complete path. # # When calling this from an OpsMgr scheduled command, use # powershell -PSConsoleFile "C:\Program Files\System Center Operations Manager 2007\Microsoft.EnterpriseManagement.OperationsManager.ClientShell.Console.psc1" -command "& {C:\Script\Path.ps1}" # # ============================================================================================== # Ensure that the OpsMgr snap-in is there Get-PSSnapin -name Microsoft.EnterpriseManagement.OperationsManager.Client -ErrorAction SilentlyContinue If (!$?) { throw "OpsMgr Console not loaded - please run with -PSConsolfile 'X:\Path\To\Microsoft.EnterpriseManagement.OperationsManager.ClientShell.Console.psc1'" } else { # CHANGE THIS to match the path in your system. Or don't. . "C:\Program Files\System Center Operations Manager 2007\Microsoft.EnterpriseManagement.OperationsManager.ClientShell.Startup.ps1" # Load OpsMgr stuff. } # Create some counters $iinfo = 0 $iwarn = 0 $ierr = 0 $icrit = 0 $iunk = 0 ### Configuration Section ### $objectName = "HewlettPackard" # All of the HP objects start with this $comment = "Automatically Resolved via PowerShell" # Added to alert # Create the SCOM Script API object, so we can shove this info into the database $momapi = New-Object -comObject "MOM.ScriptAPI" # Grab all alerts that match MonitoringObjectFullName and are not Closed # Time the whole thing for no reason $findAlertsTime = Measure-Command { $openHPAlerts = get-alert | Where-Object { ($_.MonitoringObjectFullName -match $objectName) -and ($_.ResolutionState -ne 255) } } # Let's grab some stats about what we grabbed first, before we resolve them. $openCount = $openHPAlerts.Count $totalFindTime = ([datetime]($findAlertsTime.ticks)).ToString("HH:mm.ss") # Create a property bag to hold values to send to SCOM $pbag = $momapi.CreatePropertyBag() $pbag.AddValue("Total_Open", $openCount) $pbag.AddValue("Total_FindTime", $totalFindTime) # Resolving them couldn't be simpler # Lets count the severities we're clearing, though. foreach($alert in $openHPAlerts) { switch ($alert.Severity) { "Information" {$iinfo++} "Warning" {$iwarn++} "Error" {$ierr++} "Critical" {$icrit++} } $progress += "Server: " + $alert.NetBiosComputerName + " - Rule '" + $alert.Name + "' - Repeat Count: " + $alert.RepeatCount + " `n"; # $pbag.AddValue("AutoResolveFor", $alert.NetBiosComputerName) resolve-alert -comment $comment -Alert $alert } $pbag.AddValue("Info_HP_Alerts", $iinfo) $pbag.AddValue("Warn_HP_Alerts", $iwarn) $pbag.AddValue("Err_HP_Alerts", $ierr) $pbag.AddValue("Crit_HP_Alerts", $icrit) # Submit property bag to SCOM $momapi.Return($pbag) # Log eventviewer event to let us know what we did # Severities: 1 = Error, 2 = Warning, 4 = Informational - "Script Name", "Event ID", "Severity", "Description" $momapi.LogScriptEvent("SCOM-Resolve-HardwareAlerts.ps1", 926, 4, "Successfully resolved " + $openCount + " alerts. `nReport:`n" + $progress)