MOM Management Server Class Library Samples

System Center

Examples


/// <summary>
/// Adds the specified comment to the history of the alert.
/// </summary>
/// <param name="alert">The alert to update.</param>
/// <param name="comment">The comment text to add.</param>
public void AddToHistory(Microsoft.EnterpriseManagement.Mom.Alert alert,
                                 string comment)
{
    if (comment != String.Empty)
    {
        alert.AddAlertHistoryComment(comment);
    }
}


/// <summary>
/// Creates a new MOM computer group.
/// </summary>
/// <param name="groupName">The name for the new computer group.</param>
/// <param name="groupDescription">The description for the new computer group.</param>
public void CreateComputerGroup(string groupName, string groupDescription)
{
    Administration mom;

    if (groupName != null && groupName != String.Empty)
    {
        mom = Administration.GetAdministrationObject();
        mom.InsertComputerGroup(groupName, groupDescription);
    }
}


/// <summary>
/// Displays the company and vendor knowledge for an alert. The
/// knowledge is inherited from the rule that created the alert.
/// </summary>
/// <param name="alert">The knowledge for this alert is displayed.</param>
public void GetKnowledge(Microsoft.EnterpriseManagement.Mom.Alert alert)
{
    Knowledge knowledge;
    string companyKnowledge = "";
    string vendorKnowledge = "";

    knowledge = alert.GetKnowledge();

    companyKnowledge = knowledge.Company;
    vendorKnowledge = knowledge.Vendor;

    Trace.WriteLine("Knowledge for " + alert.Name);
    Trace.WriteLine("  Company: " + companyKnowledge);
    Trace.WriteLine("  Vendor: " + vendorKnowledge);
}


/// <summary>
/// Places a computer in maintenance mode, using the specified
/// reason and time duration.
/// </summary>
/// <param name="computer">The computer to place in maintenance mode.</param>
/// <param name="reason">The reason or description of the maintanence work.</param>
/// <param name="durationHours">The number of hours to place the computer into maintanence mode.</param>
/// <remarks>Ignores computers which are already in maintenance mode, regardless of when
/// the maintenance mode will end.</remarks>
public void PlaceComputerInMaintenanceMode(Computer computer,
                                                    string reason,
                                                    double durationHours)
{
    Administration mom;
    DateTime endMaintenanceAt;
    ComputerMaintenance cm;

    if (!computer.IsInMaintenanceMode)
    {
        mom = Administration.GetAdministrationObject();

        endMaintenanceAt = DateTime.Now.AddHours(durationHours);
        cm = mom.GetComputerMaintenanceObject();
        cm.SetMaintenanceMode(computer, endMaintenanceAt, reason);
    }
}


/// <summary>
/// Places the computers belonging to a computer group in maintenance
/// mode, using the specified reason and time duration.
/// </summary>
/// <param name="computerGroup">The computer group to place in maintenance mode.</param>
/// <param name="reason">The reason or description of the maintanence work.</param>
/// <param name="durationMinutes">The number of minutes to place the computers into maintanence mode.</param>
/// <remarks>The method does not recurse into the subgroups. The method ignores
/// computers which are already in maintenance mode, regardless of when the
/// maintenance mode will end.</remarks>
public void PlaceComputerGroupInMaintenanceMode(ComputerGroup computerGroup,
                                                        string reason, double durationMinutes)
{
    Administration mom;
    ComputersCollection computers;
    ComputerMaintenance cm;
    DateTime endMaintenanceAt;

    mom = Administration.GetAdministrationObject();
    endMaintenanceAt = DateTime.Now.AddMinutes(durationMinutes);

    cm = mom.GetComputerMaintenanceObject();
    computers = computerGroup.GetAssociatedComputers(); // returns members of subgroups

    // loop through the paged collection
    do
    {
        foreach (Computer computer in computers)
        {
            if (!computer.IsInMaintenanceMode)
            {
                cm.SetMaintenanceMode(computer, endMaintenanceAt, reason);
            }
        }
        // Get next page...
        computers.Next();
    }
    while(computers.CurrentCount > 0);
}


/// <summary>
/// Assigns the alert to the specified person.
/// </summary>
/// <param name="alert">The alert to reassign.</param>
/// <param name="newOwner">The name of the new alert owner.</param>
public void ReassignAlert(Microsoft.EnterpriseManagement.Mom.Alert alert,
                                     string newOwner)
{
    if (newOwner != String.Empty && alert.OwnerName != newOwner)
    {
        alert.OwnerName = newOwner;
        alert.Update();
    }
}


/// <summary>
/// Removes the computers in the specified computer group from
/// maintenance mode.
/// </summary>
/// <param name="computerGroup">The computer group to remove from maintenance mode.</param>
/// <remarks>The method does not recurse into the subgroups.</remarks>
public void RemoveComputerGroupFromMaintenanceMode(ComputerGroup computerGroup)
{
    Administration mom;
    ComputerMaintenance cm;
    ComputersCollection computers;

    mom = Administration.GetAdministrationObject();
    cm = mom.GetComputerMaintenanceObject();
    computers = computerGroup.GetAssociatedComputers(); // returns members of subgroups

    // loop through the paged collection
    do
    {
        foreach (Computer computer in computers)
        {
            if (computer.IsInMaintenanceMode)
            {
                cm.RemoveMaintenanceMode(computer);
            }
        }
        // Get next page...
        computers.Next();
    }
    while(computers.CurrentCount > 0);
}


/// <summary>
/// Starts a scan on the local management server.
/// </summary>
public void RunManagementGroupScan()
{
    Administration mom;
    ManagementServer server;

    mom = Administration.GetAdministrationObject();
    server = mom.GetManagementServerObject();
    server.PerformScan();
}


/// <summary>
/// Displays information about the MOM to MOM connectors
/// configured to use the local management server.
/// </summary>
public void ShowMomToMomConnectorInfo()
{
    Administration mom;
    ProductConnectorsCollection connectors;

    mom = Administration.GetAdministrationObject();
    connectors = mom.GetMomToMomProductConnectors();

    foreach (ProductConnector pc in connectors)
    {
        Trace.WriteLine("MOM to MOM Connector");
        Trace.WriteLine("  Name: " + pc.Name);
        Trace.WriteLine("  Resolution State: " + pc.ResolutionState);
    }
}


public void InstallAgent()
{
      string userName = null, userPassword = null;
      string machineName = null, machineDomain = null;
      Administration admin = null;
      ManagementServer mgmtServer = null;

      // Figure out the machine to use...
      ProcessMachine(ref machineName, ref machineDomain);

      // Figure out the credentials to use...
      ProcessCredential(ref userName, ref userPassword);

      try
      {
            // Getting access to the administration and management server objects.
            admin = Administration.GetAdministrationObject();
            mgmtServer = admin.GetManagementServerObject();

            // Make the InstallAgent method call....
            // Using the MOMAction account to install it..
            mgmtServer.InstallAgent(machineName, machineDomain);

            // Or if you pass in credentials.
            // mgmtServer.InstallAgent(machineName, machineDomain, userName, userPassword);
      }
      catch (Exception e)
      {
            // Agent install task submission failed.
      }
}


public class SetToUnk
{
    [STAThread]
    public static void Main(string[] args)
    {
        Console.WriteLine("Settings all Computer Groups (except those that include DCs) to include Unknown Type of Computers....");

        Administration sdkAdministration = Administration.GetAdministrationObject();

        // Get the Top Level Computer Groups
        ComputerGroupsCollection sdkCompGrps = sdkAdministration.GetComputerGroups();
        foreach (ComputerGroup sdkCG in sdkCompGrps)
        {
            SetTypeToUnknown(sdkCG);
        }
    }

    private static void SetTypeToUnknown(ComputerGroup sdkCG)
    {
        Console.WriteLine("Found Computer Group " + sdkCG.Name);

        // Get the computer group ID
        Guid sdkCGGuid = sdkCG.Id;
        Console.WriteLine("\tComputerGroup Type is: " + sdkCG.Type);

        // If the Computer Group's SearchForComputers settings meet our criteria,
        // Set the type to Unknown (NonWindows)
        // Criteria: SearchForComputers property should be true; Type should not
        // include DCs nor should it already be set to include Unknown

        if (sdkCG.SearchForComputers &&
           ((sdkCG.Type & ComputerTypes.DomainControllers) != ComputerTypes.DomainControllers) &&
           ((sdkCG.Type & ComputerTypes.NonWindows) != ComputerTypes.NonWindows))
        {
            sdkCG.Type |= ComputerTypes.NonWindows; // Set the type
            sdkCG.Update();    // Update the Group
            // Get the ComputerGroup afresh from the Admin object and dump the type for visual verification
            ComputerGroup sdkCGUpdated = Administration.GetAdministrationObject().GetComputerGroup(sdkCGGuid);
            Console.WriteLine("\tComputerGroup New Type is: " + sdkCGUpdated.Type);
        }

        // Check if the computer group has any subgroups
        ComputerGroupsCollection sdkSubCompGrps = sdkCG.GetSubComputerGroups();
        if (sdkSubCompGrps.Count > 0)
        {
            // And if so, set the type of the subgroups also to Unknown
            foreach (ComputerGroup sdkSubCG in sdkSubCompGrps)
            {
                SetTypeToUnknown(sdkSubCG);
            }
        }
    }
}

Show: