Manipulating SCOM 2012 agent multihoming options by script

Last week I got a question from a friend in the System Center community, Adrian Chirtoc, about how to remove a management group entry from a multi-homed SCOM 2012 agent by using a script.
The case at hand was a parallel side-by-side installation of SCOM 2012 next to a SCOM 2007 R2 management group. By this process all SCOM agents got upgraded to SCOM 2012 version. Next step in this case would be to remove the management group entry of the old SCOM management group from the new agent.
Now one would at first think this is as easy as using the method previously used for SCOM 2007 agents. We have discussed a few of those over here in SCOM Trick 19 – Remove SCOM agent command.
Another much heard remark would be to use the control panel, where in the SCOM 2012 version agent you will find this place where you can add and remove and edit management group entries in an existing SCOM 2012 agent.
SCOM Agent Management
But in this case the requirement is to do it by using a script. This will also be applicable if you are in a multi-homing scenario where you are doing agent management by some other means, such as Configuration Manager or any other deployment tool. This is where the scripting comes into play.
So, as far as I could find out in short time I could only find an uninstall command from the momagent.msi which in this version should completely remove the agent and where you will not name a management group to remove (which you could do in SCOM 2007). I could not find any way to interface with the agent management applet as shown above. SO next step would be to check out either PowerShell or scripting against the SDK.
So, we end up talking to the Operations Manager Agent Configuration library, which is available on each SCOM 2012 agent and management server as AgentConfigManager.dll . From there we find a method to use to remove a management group entry from an agent.
I uploaded a simple vbscript to remove a management group entry from a SCOM 2012 agent in the TechNet Gallery you can find by clicking this sentence.
From there I figured we might want to be able to also add management group entries to an existing agent in order to multi-home it by using a script. So that is simply another method which we can use to manipulate the agent. The example script to add a management group to an existing SCOM 2012 agent by script is also uploaded to the TechNet Gallery. There is also a possibility to add an action account to the agent in the same command, but I left it at three entries (management group + management server + agent port).
To get to these I have used information from the Operations Manager 2012 SDK reference.
Have fun scripting the existing SCOM 2012 agents!
Bob Cornelissen