Blog
By Pete Zerger on 8/18/2010 11:33:45 AM • Rank (1153) • Views 1239
0

0

In our initial foray into the OpsMgr SDK in Powershell, we examined the .GetDerivedMonitoringClasses() method to explore the lineage of classes in your OpsMgr management group.

Part 1 - OpsMgr 2007: Exploring Class Lineage in Powershell

In the second installment, we examined how easy it was to create a function to enumerate the lineage of every class in OpsMgr recursively.

Part 2 - OpsMgr 2007 - Exploring Class Lineage in Powershell: Recursive Enumeration

In the third installment, we examined the the .GetMonitoringRelationshipClasses() method to retrieve all the relationships for which a relationship is a source or target.

 Part 3 OpsMgr 2007 - Exploring Class Lineage in Powershell: Relationships

In this installment, we put the two together in a quick script that outputs the derived classes and the relationships for which the class is a source or target. Since the output has grown increasingly verbose, this brief example sends the output to a text file.

Sample Script (save with .ps1 extension run from OpsMgr Command Shell prompt

#Check if the output file exists...if so, delete it
if(test-path c:\lineage.txt){Remove-Item c:\lineage.txt}

#Create the output file so later iterations can append
Write-Host " " | out-file c:\lineage.txt

function DerivedClasses ($TargetClass){

#This is the base class you will pass as a
#parameter when calling the function

$Class = $TargetClass

foreach ($baseclass in $baseclasses) {

$DerivedClasses = (get-monitoringclass | where {$_.Name -eq "$Class"}).GetDerivedMonitoringClasses()

foreach ($DerivedClass in $DerivedClasses) {

#Retrieve the derived classes and output to the text file
" " | out-file c:\lineage.txt -append
"The following classes are derived from $DerivedClass" | out-file c:\lineage.txt -append
(get-monitoringclass -Name $DerivedClass ).GetDerivedMonitoringClasses() | select DisplayName, Name | out-file c:\lineage.txt -append


#Retrieve the relationships for which the class is a source or target and output to the text file
" " | out-file c:\lineage.txt -append
"$DerivedClass is the source or target of the following relationships " | out-file c:\lineage.txt -append
(get-monitoringclass -Name $DerivedClass ).GetMonitoringRelationshipClasses() | select DisplayName | out-file c:\lineage.txt -append

#Here, we call the DerivedClasses function for the each of the child classes recursively
DerivedClasses $DerivedClass
}
}
}

#Here, we call the function and pass in the Name (NOT the Display Name) of the desired base class.
#Change the class name to whatever you like to control the output to the c:\lineage.txt file
DerivedClasses 'system.entity'
 

Sample Output

And there it is. We see the hosting relationships and derived classes of the System.ComputerRole abstract class. While we see both derived classes and relationships for System.ComputerRole, some classes will have no derived classes and some will have no relationships defined by default.

 image

 

Other Useful Methods Beneath Get-MonitoringClass

This example could be embellished to make a very useful report, and perhaps allow you to expand your Powershell horizons at the same time. For example, you could play with many examples on the Internet to export this info to an Excel spreadsheet. If you wanted to differentiate between relationships where the class is a source and those where it is a target of the relationship, you could use these methods instead of .GetDerivedMonitoringClasses() as used in the sample above.

 

 

.GetMonitoringRelationshipClassesWhereSource()
.GetMonitoringRelationshipClassesWhereTarget()

Viewing All Available Methods

Though a method known as reflection, you can see all the methods available to you when working with get-monitoringclass. In Powershell, this is done with the get-member cmdlet.

get-monitoringclass | get-member

Below are a few which can easily be used incorporated into an endless array of simple one-liners.

GetMonitorHierarchy()
GetMonitoringConsoleTasks()
GetMonitoringDiagnostics()
GetMonitoringDiscoveries()
GetMonitoringImageReferences()
GetMonitoringLinkedReports()
GetMonitoringProperties()
GetMonitoringProperty()
GetMonitoringRecoveries()
GetMonitoringRelationshipClasses()
GetMonitoringRelationshipClassesWhereSource()
GetMonitoringRelationshipClassesWhereTarget()
GetMonitoringReports()
GetMonitoringRules()
GetMonitoringTasks()
GetMonitoringViews()
GetParentMonitoringClasses()
GetRelatedMonitoringClasses()
GetResultantCategoryOverrides()
GetResultantOverrides()

Feedback

If you have anything useful that comes to mind in the line of a report as a result of reading these posts, ping me at pete.zerger [AT] gmail.com

Comments - Comment RSS


Who Viewed
Who Reviewed
Categories
Related Pages
Shortened URL
http://tinyurl.com/ybc98rs

Top Contributors
Featured Members
Pete Zerger
Points: 65622
Level: System Center Expert
Tommy Gunn
Points: 42748
Level: System Center Expert
Simon Skinner
Points: 40804
Level: System Center Expert
Stefan Koell
Points: 28999
Level: System Center Expert
Andreas Zuckerhut
Points: 27734
Level: System Center Expert