Automating OpsMgr Part 5: Adding Computers to Computer Groups


This is the 5th instalment of the Automating OpsMgr series. Previously on this series:

In the previous post (part 4), I have demonstrated a runbook creating new empty instance groups and computer groups using the OpsMgrExtended module. As I also mentioned in Part 4, I will dedicate few posts on creating and managing OpsMgr groups. So, this post is the 2nd one on this topic.

In OpsMgr, groups can be populated via Explicit memberships (static) or Dynamic Memberships (query based), or combination of both:


In this post, I will demonstrate how to use a runbook to add a Windows computer object to a computer group via Explicit membership.

Runbook Add-ComputerToComputerGroup

When using this runbook, you will need to update line 9 of the runbook, and replace the SMA connection name with the one you’ve used in your SMA environment:


This runbook requires 2 mandatory parameters:

  • Windows computer principal name (FQDN) – which is the key property of the Windows Computer object


  • The group name – it’s the internal name, not the display name. I did not use the display name because it is not unique. i.e.



If the group was created using the New-OMComputerGroup and New-OMInstanceGroup functions from the OpsMgrExtended module, these 2 functions would automatically prepend the management pack name in front of the group name specified by the user (if the management pack name is not already the prefix of the specified names). I forgot to mention this behaviour in my previous post (Part 4).

Since the OpsMgrExtended module does not (yet) have a function to add a computer to a computer group, I wrote this runbook to perform this task directly via OpsMgr SDK (therefore all within the inlinescript). The high level steps for this runbook is listed below:

  1. Establish OpsMgr management group connection (and the SDK assemblies will be loaded automatically).
  2. Get the Windows computer monitoring object
  3. Get the computer group monitoring class (singleton class)
  4. Check if the group object specified is indeed a computer group
  5. Get the computer group instance
  6. Get the computer group discovery
  7. Make sure the discovery is defined in an unsealed management pack
  8. Detect if any “MembershipRule” segments in discovery data source module contains existing static members
  9. If there are existing static members in one of the membership rule, add the Windows computer as a static member in the membership rule.
  10.   If none of the membership rules contain static members, define a static member section (“<IncludeList>”) in the first Membership Rule.
  11. Update the unsealed management pack where the discovery is defined.

Executing Runbook

Group membership before execution:


Executing runbook:



Group membership after execution:



In this post, I have demonstrated how to use a runbook and OpsMgrExtended module to add a Windows computer object as a static member of a computer group.

I’ve also demonstrated even when an activity is not pre-defined in the OpsMgrExnteded module, we can still leverage OpsMgrExnteded module to perform the task because we can directly interact with OpsMgr management groups and SDKs via this module – by using the Connect-OMManagementGroup function, the SDK will be loaded automatically.

When I was writing this runbook few days ago, I have realised this is something I should have included in the OpsMgrExtended module because it could be very common and useful. Although I’ve published this as a rather complex runbook at this stage, I will probably included this as an additional function in the future release of OpsMgrExtended module.

I will demonstrate how to add a explicit member to an instance group in the Part 6 of this series.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.