About Tao Yang

Posts by Tao Yang:

0

PowerShell Functions: Get OpsMgr Alert Generating Rules and Monitors

Posted by Tao Yang on 12/04/2014 in PowerShell, SCOM |

This is my second post today. Bad weather, both wife and daughter have got flu. So I’m sitting home catching up with blogs…

I wrote 2 functions as part of a PowerShell script I’ve been working on: Get-AlertRules and Get-AlertMonitors.

As the names suggest, these two functions get all Rules / Monitors of a particular monitoring class that generate alerts.

I didn’t end up using these 2 functions in my script, but I thought they are too good to be trashed. so I thought I’ll put them here for future reference.

Get-AlertRules:

Function Get-AlertRules
{
PARAM (
[Parameter(Mandatory=$true,HelpMessage="OpsMgr Management Group Connection" )][Microsoft.EnterpriseManagement.ManagementGroup] $ManagementGroup,
[Parameter(Mandatory=$false,HelpMessage="Monitoring Class Name" )][string] $MonitoringClassName = $null
)
$arrAlertRules = New-object System.Collections.ArrayList
#Get GenerateAlert WriteAction module
$HealthMPId = [guid]"0abff86f-a35e-b08f-da0e-ff051ab2840c" #this is unique
$HealthMP = $MG.GetManagementPack($HealthMPId)
$AlertWA = $HealthMP.GetModuleType("System.Health.GenerateAlert")
$AlertWAId = $AlertWA.Id
#firstly get all monitoring classes
#Populate Search criteria
If ($MonitoringClassName)
{
$strClassCriteria = "Name = '$MonitoringClassName'"
} else {
$strClassCriteria = "Name LIKE '%'"
}
$ClassCriteria = New-Object Microsoft.EnterpriseManagement.Configuration.MonitoringClassCriteria($strClassCriteria)
$MonitoringClasses = $MG.GetMonitoringClasses($ClassCriteria)
Foreach ($MC in $MonitoringClasses)
{
$MCId = $MC.Id
$strRuleCriteria = "TargetMonitoringClassId = '$MCId'"
$RuleCriteria = New-Object Microsoft.EnterpriseManagement.Configuration.MonitoringRuleCriteria($strRuleCriteria)
$Rules = $MG.GetMonitoringRules($RuleCriteria)
Foreach ($rule in $Rules)
{
#Unfortunately, we cannot use a member module name/id in MonitoringRUleCriteria.
#So we have to manually filter out the rules with GenerateAlert Write Action Module
#Check if it has a GenerateAlert WriteAction module
$bAlertRule = $false
Foreach ($WAModule in $Rule.WriteActionCollection)
{
if ($WAModule.TypeId.Id -eq $AlertWAId)
{
#this rule generates alert
$bAlertRule = $true
} else {
#need to detect if it's using a customized WA which the GenerateAlert WA is a member of
$WAId = $WAModule.TypeId.Id
$WASource = $MG.GetMonitoringModuleType($WAId)
#Check each write action member modules in the customized write action module...
Foreach ($item in $WASource.WriteActionCollection)
{
$itemId = $item.TypeId.Id
If ($ItemId -eq $AlertWAId)
{
$bAlertRule = $true
}
}
}

if ($bAlertRule)
{
#Add to arraylist
[void]$arrAlertRules.Add($rule)
}
}
}
}
,$arrAlertRules
}

Get-AlertMonitors:

Function Get-AlertMonitors
{
PARAM (
[Parameter(Mandatory=$true,HelpMessage="OpsMgr Management Group Connection" )][Microsoft.EnterpriseManagement.ManagementGroup] $ManagementGroup,
[Parameter(Mandatory=$false,HelpMessage="Monitoring Class Name" )][string] $MonitoringClassName = $null
)
$arrAlertMonitors = New-object System.Collections.ArrayList
#firstly get all monitoring classes
#Populate Search criteria
If ($MonitoringClassName)
{
$strClassCriteria = "Name = '$MonitoringClassName'"
} else {
$strClassCriteria = "Name LIKE '%'"
}
$ClassCriteria = New-Object Microsoft.EnterpriseManagement.Configuration.MonitoringClassCriteria($strClassCriteria)
$MonitoringClasses = $MG.GetMonitoringClasses($ClassCriteria)
Foreach ($MC in $MonitoringClasses)
{
$MCId = $MC.Id
$strMonitorCriteria = "TargetMonitoringClassId = '$MCId' AND AlertOnState IS NOT NULL"
$MonitorCriteria = New-Object Microsoft.EnterpriseManagement.Configuration.MonitorCriteria($strMonitorCriteria)
$Monitors = $MG.getmonitors($MonitorCriteria)
Foreach ($Monitor in $Monitors)
{
#Add to arraylist
[void]$arrAlertMonitors.Add($Monitor)
}
}
,$arrAlertMonitors
}

Both functions are expecting an OpsMgr management group connection and the name (not the display name, but the Class ID from the management pack where the class is defined). so in order to use these 2 functions, I’ll need 2 other functions:

Load-SDK:

function Load-SDK()
{
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.EnterpriseManagement.OperationsManager.Common") | Out-Null
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.EnterpriseManagement.OperationsManager") | Out-Null
}

Get-MonitoringClass:

Function Get-MonitoringClass
{
PARAM (
[Parameter(Mandatory=$true,HelpMessage="OpsMgr Management Group Connection" )][Microsoft.EnterpriseManagement.ManagementGroup] $ManagementGroup,
[Parameter(Mandatory=$true,HelpMessage="Monitoring Class Display Name" )][string] $MonitoringClassDisplayName
)
#Populate Search criteria
$strClassCriteria = "DisplayName = '$MonitoringClassDisplayName'"
$ClassCriteria = New-Object Microsoft.EnterpriseManagement.Configuration.MonitoringClassCriteria($strClassCriteria)
#Search monitoring class
$MonitoringClasses = $MG.GetMonitoringClasses($ClassCriteria)
$MonitoringClasses
}

as the name suggests, Load-SDK function loads OpsMgr SDK, when can then create the connection to the management group. Get-MonitoringClass function gets the Monitoring Class object based on it’s display name (the name you see in the Operations Console), such as this one:

SNAGHTML1f11d89

Here’s an example of how to these functions:

#Load SDK DLL's
Load-SDK

#Connect to the management group via management server "OpsMgrMS01":
$MGConnSetting = New-Object Microsoft.EnterpriseManagement.ManagementGroupConnectionSettings("OPSMGRMS01")
$MG = New-Object Microsoft.EnterpriseManagement.ManagementGroup($MGConnSetting)

#Get the monitoring class
$MonitoringClassDisplayName = "Data Access Service"
$MonitoringClass = Get-MonitoringClass $MG $MonitoringClassDisplayName
$MonitoringClassName = $MonitoringClass.Name

#Display Monitoring Class Name
$MonitoringClassName

#Get Alert rules
$AlertRules = Get-AlertRules $MG $MonitoringClassName

#Rule Count
$alertRules.count

#Alert Monitors
$AlertMonitors = Get-AlertMonitors $MG $MonitoringClassName

#Monitor Count
$AlertMonitors.Count

SNAGHTML1f339a2

Logics in these 2 functions:

Get-AlertRules:

Searching for rules either have the “System.Health.GenerateAlert” module from System.Health.Library MP as a Write Action member module, or one of the rule’s Write Action member modules has “System.Health.GenerateAlert” as its member.

Get-AlertMonitors:

This function is much easier to write than Get-AlertRules. I’ simply search for monitors that “AlertOnState” property is not NULL. Please keep in mind this function does not only return unit monitors, but also aggregate and dependency monitors.

Both functions return a “System.Collections.ArrayList” containing the rules / monitors. Since I used the OpsMgr SDK directly, instead of it’s PowerShell snapin or module. these functions should work in both 2007 and 2012. – And this is one of the reasons why I always just use SDK, hardly use the snapin or the module :)

I’ve also zipped up all the code used in this article. You can download them HERE. I know it’s a bit hard to read the code in WordPress :)

Tags: ,

2

Remotely Powering On and Off My Entire Home Lab

Posted by Tao Yang on 12/04/2014 in PowerShell |

My home lab consists of 3 PCs running Hyper-V and a HP Proliant Microserver N54L running SCVMM. I have previously blogged the lab setup in a 2-part blog posts (Part 1, Part 2). These 2 blog articles was written back in October 2012, although there are few changes in the current setup (new hardware, etc), […]

Tags: ,

0

PowerShell Module to resize console – Updated

Posted by Tao Yang on 08/04/2014 in PowerShell |

Few days ago I wrote a PowerShell module that contains 1 cmdlet / function to resize the PowerShell console windows. It was a quick job that I did less than half an hour. I wrote it primarily to make PowerShell Web Access (PSWA) more user friendly. Over the last couple of days, I spent a […]

Tags: ,

7

PowerShell Module: Resize-Console

Posted by Tao Yang on 05/04/2014 in PowerShell |

I’m currently working on a solution that relies on PowerShell Web Access (Hopefully I can finish tonight and blog it in next couple of days). I have been a bit hesitate to use use PWSA every since I firstly tried it out briefly back in 2012 (and blogged my experience here). Why am I hesitated? […]

Tags: ,

1

SQL DB Engines Not Discovered in SCOM and My Troubleshooting Experiences

Posted by Tao Yang on 03/04/2014 in SCOM |

Few days ago while I was in the OpsMgr 2012 console, I realised that all 3 SQL clusters hosting my OpsMgr Ops DBs are not discovered by MS SQL management packs. All other SQL clusters with same version of SQL got discovered (i.e. clusters hosting ConfigMgr 2012 site databases, etc.). Since I backup both sealed […]

Tags:

3

VMware Windows Guest Management Pack

Posted by Tao Yang on 03/04/2014 in SCOM |

This Monday, I was configuring monitoring for all our System Center 2012 servers as per our Windows support team’s requirement. Since all the virtual machines for our System Center 2012 servers are hosted on VMware ESX, they want to monitor the VMware Tools service on these guest VM’s. Since it was Monday morning and I […]

Tags: ,

1

Discovery for MS Clusters of Any Kind

Posted by Tao Yang on 29/03/2014 in SCOM |

Often when developing an OpsMgr management pack for server class applications, we need to be cluster-aware. Sometimes workflows don’t need to run on a cluster, sometimes, the workflow should only be executed on a cluster. (i.e. I wrote a monitor that runs on a Windows 2008 R2 Hyper-V cluster once a day and check if […]

Tags: ,

0

Discoveries in Fujitsu PRIMERGY Server Management Pack

Posted by Tao Yang on 19/03/2014 in SCOM |

I have been dealing this the Fujitsu PRIMERGY Server MP from Fujitsu ever since I started working for my current employer about 2.5 years ago. We found many issues with the previous version back in SCOM 2007 days and now as I’m setting up SCOM 2012 R2 management groups and having to implement the latest version […]

Tags: ,

1

ConfigMgr 2012 (R2) Clients Management Pack Released

Posted by Tao Yang on 18/03/2014 in SCCM, SCOM |

Time flies, I can’t believe it’s been over 7 months since I posted the beta version of the ConfigMgr 2012 client MP for testing. I haven’t forgotten about this MP (because it’s one of the deliverables for the System Center 2012 upgrade project that I’ve been working on for the last 12 months or so). […]

Tags: , , ,

5

How To Configure OpsMgr 2012 Web Console Single Sign-On

Posted by Tao Yang on 27/02/2014 in SCOM |

I was trying to configure single sign-on for the OpsMgr 2012 Web Consoles so dashboard users don’t need to enter credentials on shared display screens. I spent almost a day trying to make it work until I gave up and called Microsoft Premier Support yesterday. On all my management groups, web consoles are installed on […]

Tags:

Copyright © 2010-2014 Tao Yang's System Management Blog All rights reserved.
This site is using the Desk Mess Mirrored theme, v2.2.4.1, from BuyNowShop.com.