Compartilhar via


Old Blog post about Compiling commandline application for Creating and Updating Groups

Marco Shaw published a blogpost about and Advanced example using PowerShell and the OpsMgr SDK: Creating and Updating Groups and mentioned an old blog post from me which he could not find anymore.

Here is that blog post from me (without the pictures)

Compiling commandline application for Creating and Updating Groups

Mon, 22/10/2007 - 21:32 — Stefan

Today I saw a question from Marco Shaw in the MyITForum MOM maillist about how to compile the code from Jakub Oleksy where he explains how you can use the OpsMgr SDK to create and update groups in OpsMgr.

In this article is demonstrate how I was able to compile that code to create a commandline tool. Keep in mind I'm not a programmer, just trying to get some knowledge about creating tools using the OpsMgr SDK. If you are looking for real great tools for OpsMgr visit Boris Yanushpolsky website ;-)

Install Compiler
First I downloaded Snippet Compiler (why download and install Visual Studio Express if you only need to compile the code?). After the download just unzip the files and you are ready to run the Snippet Compiler.

Install OpsMgr SDK
Check if you have installed the OpsMgr SDK. If not you can download the SDK from the Microsoft Connect site on the System Center Operations and Service Management downloads after you registered.

Add OpsMgr SDK references
After starting the Snippet Compiler you first have to add the OpsMgr SDK References.

Go to Tools->References and select the Microsoft.EnterpriseManagement.OperationsManager.Common.dll and Microsoft.EnterpriseManagement.OperationsManager.dll from the SDK as refrerence in Snippet Compiler.
Click on OK.

Change the code from Jakub Olesky
The code Jakub has provided will not work on it's own, but you can use it to create a commandline tool. I changed the code (bold and larger fonts) slightly to create a console app. But you can add more arguments if you want, it's just for the learning.

 namespace Jakub_WorkSamples
{
  using System;
   using Microsoft.EnterpriseManagement;
   using Microsoft.EnterpriseManagement.Configuration;
 using Microsoft.EnterpriseManagement.ConnectorFramework;
    using Microsoft.EnterpriseManagement.Monitoring;
    
    internal class Program
    {
 private static void Main(string[] args)<br>   {<br>     Console.WriteLine("CreateGroups - Version 1.0 - Compiled October 22, 2007");<br>            Console.WriteLine("https://weblog.stranger.nl");<br>            if (args.Length < 1)<br>            {<br>                Console.WriteLine("\n");<br>                Console.WriteLine("Provide name Root Management Server");<br>                Console.WriteLine(@"ex: CreateGroups.exe RMSServer01");<br>            }<br>            else<br>            {<br>             string strRMS = args[0];<br>              Console.WriteLine("Connect to Root Management Server:" + strRMS); 

               // Connect to the sdk service on the local machine
   
                ManagementGroup localManagementGroup = new ManagementGroup(strRMS);
 
    
                string formula =    
                    @"<MembershipRule>   
                    <MonitoringClass>$MPElement[Name=""Windows!Microsoft.Windows.Computer""]$</MonitoringClass> 
                    <RelationshipClass>$MPElement[Name=""InstanceGroup!Microsoft.SystemCenter.InstanceGroupContainsEntities""]$</RelationshipClass> 
                    </MembershipRule>";
  
    
    
                // Create the custom monitoring object group    
                CustomMonitoringObjectGroup allComputersGroup = 
                    new CustomMonitoringObjectGroup("Jakub.At.Work.Namespace",    
                    "AllComputerGroup",   
                    "Jakub@Work Sample All Computers Group",  
                    formula);
   
    
    
                // Get the default management pack. 
                ManagementPack defaultManagementPack =  
                    localManagementGroup.GetManagementPacks(    
                    "Microsoft.SystemCenter.OperationsManager.DefaultUser")[0];   
    
                // Get the management packs for references  
                ManagementPack windowsManagementPack = localManagementGroup.    
                    GetManagementPack(SystemManagementPack.Windows);
    
                ManagementPack instanceGroupManagementPack = localManagementGroup.  
                    GetManagementPack(SystemManagementPack.Group);
  
                ManagementPackReferenceCollection newReferences =   
                    new ManagementPackReferenceCollection();
    
                newReferences.Add("Windows", windowsManagementPack);  
                newReferences.Add("InstanceGroup", instanceGroupManagementPack);
  
    
    
                defaultManagementPack.InsertCustomMonitoringObjectGroup(allComputersGroup,  
                    newReferences);
 
    
    
                // Get the class that represents my new group   
                MonitoringClass myNewGroup = localManagementGroup.  
                    GetMonitoringClasses("Jakub.At.Work.Namespace.AllComputerGroup")[0];  
    
                // Get the discovery rule that populates this group 
                // For the purposes of this sample, I know there is only 1 in the template  
                MonitoringDiscovery groupPopulateDiscovery = myNewGroup.    
                    GetMonitoringDiscoveries()[0];  
    
                // This is the full configuration of the discovery of which the     
                // membership rule is one part that you can configure and update    
                Console.WriteLine("The discovery configuration: {0}", 
                    groupPopulateDiscovery.DataSource.Configuration);
   
    
    
                // Update the configuration in some fasion  
                string newConfiguration =   
                    @"<RuleId>$MPElement$</RuleId> 
                    <GroupInstanceId>$MPElement[Name=""Jakub.At.Work.Namespace.AllComputerGroup""]$</GroupInstanceId>   
                    <MembershipRules> 
                        <MembershipRule>  
                    <MonitoringClass>$MPElement[Name=""Windows!Microsoft.Windows.Computer""]$</MonitoringClass> 
                    <RelationshipClass>$MPElement[Name=""InstanceGroup!Microsoft.SystemCenter.InstanceGroupContainsEntities""]$</RelationshipClass> 
                    <ExcludeList> 
                        <MonitoringObjectId>f5E5F15E-3D7D-1839-F4C6-13E36BCD982a</MonitoringObjectId>   
                    </ExcludeList>    
                    </MembershipRule> 
                    </MembershipRules>"; 
    
    
                // Now we want to update the group membership for this group    
                groupPopulateDiscovery.Status = ManagementPackElementStatus.PendingUpdate;  
                groupPopulateDiscovery.DataSource.Configuration = newConfiguration; 
                groupPopulateDiscovery.GetManagementPack().AcceptChanges();
         }   

        }

    }

}
 
    
    

Copy to code into Snippet Compiler

Now you have to copy the code from above to the Snippet Compiler and Select Build Current to file. Give it a nice name (CreateGroups.exe) and save it as Console EXE. Or you can download the code from here and import it in the Snippet Compiler.

Result

After you compiled the CreateGroups.exe commandline tool and copied it to the RMS you can run it with the next command: CreateGroups.exe localhost. When the console app is finished a new Group Jakub@Work Sample All Computers Group is created with "Windows Computer" as Members.

Just change the code if you want to change the name of the Group or the formula to populate the group.