OpsMgr: Command Shell tips and tricks when using a criteria with a monitoring class

Here’s an update to a guest post in a Powershell series from Marco Shaw, Powershell MVP and Co-director of the Powershell Community. Marco is also very active in supporting users with questions on the OpsMgr Command Shell, and in fact recently had a TechNet article published on the topic. In this post, Marco tackles some of the finer points of working with the OpsMgr 2007 Command Shell.

I’ll let Marco take it from here…

A few months ago, there was an online discussion on how to use the –criteria parameter with the Get-MonitoringObject cmdlet.
Running this in the Command Shell:

$mc = Get-MonitoringClass -name “Microsoft.Windows.Server.Computer”
Get-MonitoringObject -MonitoringClass $mc

Resulted in a list of “NoteProperty” properties such as this one:

Get-MonitoringObject -MonitoringClass $mc | Get-Member -Type NoteProperty *.DNSName

TypeName: Microsoft.EnterpriseManagement.Monitoring.MonitoringObject

Name MemberType Definition
—- ———- ———-
[Microsoft.Windows.Computer].DNSName NoteProperty System.String [Microsoft.Win..

So the issue was how to properly do something like this:

Get-MonitoringObject -MonitoringClass $mc

-Criteria ‘[Microsoft.Windows.Computer].DNSName=’something’’’

No matter what combinations of quotes or escape characters I used, nothing worked. Then it struck me that this was a “NoteProperty” and that these types of properties aren’t actual members of the original object, but special properties added by the PowerShell Extended Type System (ETS). My first guess that these properties were being added by some custom .ps1xml files, but that wasn’t the case. These properties must be embedded in the C# code of the cmdlets likely.

So a bit of searching through MSDN, and I came up with a method named GetValidPropertyNames. This method allows me to check what are the valid property names that I can use in my criteria expression.

So with a bit of OpsMgr SDK magic from the Command Shell:

# Get a connection to the local management server.
$mg = New-Object Microsoft.EnterpriseManagement.ManagementGroup(”localhost”)

# Get the class we’re looking for.
$computerMonitoringClass = $mg.GetMonitoringClass([Microsoft.EnterpriseManagement.Configuration.SystemMonitoringClass]::WindowsComputer)

# Create a criteria object. We leave the first part of the constructor as $null.
$criteria = New-Object
Microsoft.EnterpriseManagement.Monitoring.MonitoringObjectCriteria($null,$computerMonitoringClass)

# Use the method we discovered.
$critera.GetValidPropertyNames()

The result from the last command:

$criteria.GetValidPropertyNames()
PrincipalName
DNSName
NetbiosComputerName
NetbiosDomainName

So, what we should really be using for our criteria value is:

Get-MonitoringObject -MonitoringClass $mc -Criteria ‘DNSName=’something’’’

Which works great!

 

One thought on “OpsMgr: Command Shell tips and tricks when using a criteria with a monitoring class

Leave a Reply