Register Server in Enterprise Manager Using SQLDMO

A question I was recently asked is how the SQLDMO object can be used to register Servers and Server Groups in SQL Server Enterprise Manager.  The following VBScript snippet demonstrates adding a new Server Group and adding a Server to the newly created Server Group:

~~~

Dim oApplication
Dim oServerGroup

Set oApplication = CreateObject(“SQLDMO.Application”)
Set oServerGroup = CreateObject(“SQLDMO.ServerGroup”)
Set oRegisteredServer = CreateObject(“SQLDMO.RegisteredServer”)

‘ Add a new Server Group in Enterprise Manager called
‘ NewGroup
oServerGroup.Name = “NewGroup”
oApplication.ServerGroups.Add oServerGroup

‘ Register the SQL Server instance so that it uses
‘ Windows Authentication
oRegisteredServer.Name = “WARDYIT01”
oRegisteredServer.UseTrustedConnection = 1 ‘ 1 = True

‘ Add the registered server to the Serve Group NewGroup
Set DefaultGroup = oApplication.ServerGroups(“NewGroup”)
DefaultGroup.RegisteredServers.Add oRegisteredServer

Set oRegisteredServer = Nothing
Set oServerGroup = Nothing
Set oApplication = Nothing

~~~