Blog
By Andreas Zuckerhut on 11/23/2009 5:37:42 AM • Rank (2563) • Views 2898
0

0

Preface

At our company we are in the need of creating Management Packs including a Group using discovered Properties. Therefore we have a class Windows.Computer.Extended and an attribute called ServerRole, of course the Base Class is Windows Computer.

So, the first thing we had to figure out was how to get the environment of the Operations Manager Console as well as the assemblies into a standalone Script.

 

Loading the OpsMgr Snapin

As you can see in the code below, we check at first if the Snapin is already loaded, if it's not, we load it.

# Check if the OpsMgr.Client PSSnapin is already loaded, if not, load it.
    if ((Get-PSSnapin | Where-Object {$_.Name -eq 'Microsoft.EnterpriseManagement.OperationsManager.Client'}) -eq $null)
    {
        Write-Host("Loading Operations Manager Client PS Snapin")
        Add-PSSnapin Microsoft.EnterpriseManagement.OperationsManager.Client   #  <- that's the magical line
    }
    else
    {
        Write-Host("Operations Manager Client PS Snapin is already loaded")
    }

 

Loading the Assemblies

Additionally we need to load the Assemblies using the PartialName. However, make sure first that these assemblies are available on the client by checking the folder: %windir%\assembly

You should have these Assemblies in there: Microsoft.EnterpriseManagement.OperationsManager and Microsoft.EnterpriseManagement.OperationsManager.Common

Once verified, all you need are the lines below:

# Load the Assemblies
    [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.EnterpriseManagement.OperationsManager")
    [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.EnterpriseManagement.OperationsManager.Common")

 

Setting the Location and loading the ManagementGroup

# Set Location to the RMS. Afterwards you can use any Powershell Console like the Operations Manager Console. And of course, you can use this in Powershell Modules
    $rms = "localhost" # Insert the FQDN of your RMS here
    Write-Host("Setting Location $rms.")
    Set-Location "OperationsManagerMonitoring::" -ErrorVariable errSnapin;
    $MG = New-ManagementGroupConnection -ConnectionString:$rms -ErrorVariable errSnapin;   
    Set-Location $rms -ErrorVariable errSnapin;

 

How to use the Assemblies

Something that is a must-have here is Visual Studio or some other .net coding application to know the full names of the Objects and we also need to know how to use them. Something that you will always need is the ManagementGroup object, therefore in Visual Studio start a New Project -> Visual C# -> Console Application -> OK

image

Additionally, go to Project -> Add Reference -> Browse -> %ProgramFiles%\System Center Operations Manager 2007\SDK Binaries -> select Microsoft.EnterpriseManagement.OperationsManager.dll and Microsoft.EnterpriseManagement.OperationsManager.Common.dll -> OK

image

Next thing, go to View -> Object Browser

image

You will get a Tree View of all the Assemblies and their Classes as well as "documentation" on the Overloads. In our case, we want the ManagementGroup Object which is located in: Microsoft.EnterpriseManagement.OperationsManager\Microsoft.EnterpriseManagement\ManagementGroup.

In the mid-pane select ManagementGroup and you will see that it takes a string for the ServerName as an Overload. So, to load a new Object using that Class we insert the following line into our Powershell Script:

$Script:MG = New-Object Microsoft.EnterpriseManagement.ManagementGroup($rms) # We can now access that Object using $Script:MG ($Script Vars are available throughout the entire Script

Now, just to see what we can do with it we look for the function GetManagementPacks which takes a few different Overloads. To make things not more complicated we use GetManagementPacks() which takes 0 Overloads and gets all ManagementPacks of the current ManagementGroup which looks like this:

$Script:MG.GetManagementPacks()

 

Please find the Example Script attached in this Blog.

Latest Media - View All Media (1)
   
Comments (1) - Comment RSS
Marco Shaw wrote: on Apr 25, 2010 07:11 AM
And that "$script:MG" syntax... For people new to PowerShell, that's a way to define the scope of the variable (see PS> help about_scopes). I would consider it an intermediate topic, but still one that is good to understand.


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

Top Contributors
Featured Members
Pete Zerger
Points: 65442
Level: System Center Expert
Tommy Gunn
Points: 42712
Level: System Center Expert
Simon Skinner
Points: 40744
Level: System Center Expert
Stefan Koell
Points: 28999
Level: System Center Expert
Andreas Zuckerhut
Points: 27434
Level: System Center Expert