server groups

is there a way to enumerate through existing server groups Im trying to create children server groups.

Answer this question

server groups

  • Astericks

    Allen White wrote:
    Now, I haven't been able to figure out yet how to establish the "top" group so that the entire tree becomes available, but if I set the sgTop group to a valid group name the groups and servers under that group are presented to me properly.

    any ideas or suggestions where to look to find out how to discover the "top" group


  • StarSS

    Hi Chuck,

    Have a look at the code I posted earlier - it includes the logic to do what you ask and in fact just populates a treeview with all registered servers/groups. Is there something specific you're having a problem with

    Cheers,

    Rob


  • dork

    Hi Allen,

    You'll need to use a recursive function for this as a group can have a sub-group, and those groups may have sub-groups etc. (not to mention servers at any level)

    So, say you wanted to populate a treeview with all servers/groups registered on the machine:

    private void PopulateServerListRCR(Microsoft.SqlServer.Management.Smo.RegisteredServers.ServerGroupCollection Grps, TreeNode ParentNode)
    {
    TreeNode node;
    TreeNode SrvNode;

    for (int i = 0; i < Grps.Count; i++)
    {
    node = new TreeNode();
    node.Name = GrpsIdea.Name;
    node.Text = GrpsIdea.Name;

    if (ParentNode != null)
    {
    ParentNode.Nodes.Add(node);
    }
    else
    {
    tvServers.Nodes.Add(node);
    }

    for (int n = 0; n < GrpsIdea.RegisteredServers.Count; n++)
    {
    SrvNode = new TreeNode();

    SrvNode.Name = "SERV" + GrpsIdea.RegisteredServersNo.Name;
    SrvNode.Text = GrpsIdea.RegisteredServersNo.Name;

    if (ParentNode == null)
    {
    if (GrpsIdea.RegisteredServersNo.Parent != null)
    {
    tvServers.Nodes[GrpsIdea.RegisteredServersNo.Parent.Name].Nodes.Add(SrvNode);
    }
    else
    {
    tvServers.Nodes.Add(SrvNode);
    }
    }
    else
    {
    node.Nodes.Add(SrvNode);
    }
    }

    if (GrpsIdea.ServerGroups.Count > 0)
    {
    PopulateServerListRCR(GrpsIdea.ServerGroups, node);
    }

    }

    }

    and you'd call it as such:

    PopulateServerListRCR(SmoApplication.SqlServerRegistrations.ServerGroups, null);

    Sorry, I'm mostly c# not VB, so hopefully you can "VBify" this :)

    Cheers,

    Rob


  • Sevi

    Yes, you're absolutely correct. I was just showing an example of the objects and how one can get the existing servers using them.

  • teqmem

    yes im trying register groups and servers in a treeview in SSMS within multiple groups and subgroups.
  • Saqib Jahangir

    Imports Microsoft.SqlServer.Management.Smo

    Imports Microsoft.SQLServer.Management.Smo.Server

    Imports Microsoft.SqlServer.Management.Smo.RegisteredServers

    Dim svr As Server = New Server()

    Dim grp As ServerGroup = New ServerGroup("Test")

    grp.Create()

    '--register server

    Dim regSvr As RegisteredServer = New RegisteredServer(grp, "NTSQLDEV02")

    regSvr.ServerInstance = "NTSQLDev02"

    regSvr.LoginSecure = True

    regSvr.Create()

    This creates a group and registers the server.

    Im wanting to create subgroups like Production and Development under the Test group and assign servers accordingly, but like i mentioned before im having problems getting a handle on the existing groups to be able to fill the .parent property for the children groups.

    And yes documentation on this is a bit sparse. :)


  • Ben Santiago

    another question, and i know there's an option that imports sql2000 registered servers but there instances where that's not available to us.

    What do i do if there's no parent server Im wanting to fill the management studio registered server window with my list. If there's no server local or otherwise present where do i find my parent property

    Thanks again!


  • smk_k

    many thanks!
  • Jassim Rahma

    Can you post the code you're using to create new groups I'm trying to find any reference for them and am unable to do so. The object hierarchy in SMO starts at the Server object and works its way down from there, to the best of my knowledge.

  • Konst Kolesnichenko

    OK, this is connecting to your local machine for the registered servers and groups. Thanks for showing me this aspect, as I'd focused strictly on the server-side objects. Here's some code I'm playing with to get the existing registered servers and their groups:

    Dim sgTop As ServerGroup
    Dim sgColl As ServerGroupCollection
    Dim sgGroup As ServerGroup
    Dim rsColl As RegisteredServerCollection
    Dim rsRegSvr As RegisteredServer

    sgTop = New ServerGroup("ExistingGroup")
    rsColl = sgTop.RegisteredServers
    For Each rsRegSvr In rsColl
    Console.WriteLine("SrvGrp: " + sgTop.Name + ", RegSvr: " + rsRegSvr.Name)
    Next

    sgColl = sgTop.ServerGroups
    For Each sgGroup In sgColl
    rsColl = sgGroup.RegisteredServers
    For Each rsRegSvr In rsColl
    Console.WriteLine("SrvGrp: " + sgGroup.Name + ", RegSvr: " + rsRegSvr.Name)
    Next
    Next

    Now, I haven't been able to figure out yet how to establish the "top" group so that the entire tree becomes available, but if I set the sgTop group to a valid group name the groups and servers under that group are presented to me properly.



  • Jangid

    Hi Chuck,

    The only way to do this will be to interrogate the Parent property of the server (and in turn the Parent property of the parent obejct etc etc).

    My previous recursive example shows how to do this (and you will need to use recursion as the top level group can be any number deep).

    Cheers

    Rob


  • Mark Rendle

    I'm able to create new groups with the SMO objects i am having problems accessing the existing ones.
  • John Sobernheim

    Server groups are defined within your Management Studio environment. The SMO objects start at the server and work down from there.

  • Mikepy

    Hi Chuck,

    I'm not sure I knwo what you mean Can you give an example Are you just trying to populate a treeview with all servers registered in SSMS

    Cheers,

    Rob


  • server groups