locked
How to get AD sites & subnets RRS feed

  • Question

  • User570416984 posted
    Hi there

    New to using Visual Basic in Visual Studio 2013...

    How do I get the Sites & Subnets from Active Directory?

    It's a part of a greater project and I'm kind of stuck here...


    ...much appreciated...

    BR
    Mogens
    Sunday, April 27, 2014 3:01 AM

Answers

  • User1508394307 posted

    There are ActiveDirectorySiteCollection collection and ActiveDirectorySite.Subnets properties which could help

    http://msdn.microsoft.com/en-us/library/vstudio/system.directoryservices.activedirectory.activedirectorysitecollection(v=vs.90).aspx 
    http://msdn.microsoft.com/en-us/library/vstudio/system.directoryservices.activedirectory.activedirectorysite.subnets(v=vs.90).aspx 

    Example

    Dim targetForestName As String = "test.domain.local"
    
    Dim context As New DirectoryContext(DirectoryContextType.Forest, targetForestName, "administrator", "adminpasswort")
    
    Dim forest__1 As Forest = Forest.GetForest(context)
    
    For Each site As ActiveDirectorySite In forest__1.Sites
    	If site.Name = "Sitename" Then
    		add2List(2, "Site", site.Name)
    
    		Console.WriteLine("It contains domain:")
    		For Each d As Domain In site.Domains
    			add2List(2, "Domain", d.Name)
    		Next
    		For Each d As DirectoryServer In site.Servers
    			add2List(2, "server", d.Name)
    			add2List(2, "server-ip", d.IPAddress)
    
    			Dim myEnumerator1 As System.Collections.IEnumerator = d.Partitions.GetEnumerator()
    			While myEnumerator1.MoveNext()
    				add2List(2, "partitions", "" & Convert.ToString(myEnumerator1.Current))
    			End While
    			Dim myEnumerator As System.Collections.IEnumerator = d.InboundConnections.GetEnumerator()
    			While myEnumerator.MoveNext()
    				add2List(2, "inbound", "" & Convert.ToString(myEnumerator.Current))
    			End While
    		Next
    
    
    
    		For Each sl As ActiveDirectorySiteLink In site.SiteLinks
    			add2List(2, "Sitelink", sl.Name)
    			Dim myEnumerator3 As System.Collections.IEnumerator = sl.Sites.GetEnumerator()
    			While myEnumerator3.MoveNext()
    				add2List(2, "site-sites", "" & Convert.ToString(myEnumerator3.Current))
    			End While
    		Next
    
    		For Each subnet As ActiveDirectorySubnet In site.Subnets
    			add2List(2, "Subnet, location, site", (subnet.Name + "; " + subnet.Location & "; ") + subnet.Site)
    		Next
    
    		add2List(2, "InterSiteTopologyGenerator is", "" + site.InterSiteTopologyGenerator)
    	End If
    Next
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, April 27, 2014 4:49 AM

All replies

  • User1508394307 posted

    There are ActiveDirectorySiteCollection collection and ActiveDirectorySite.Subnets properties which could help

    http://msdn.microsoft.com/en-us/library/vstudio/system.directoryservices.activedirectory.activedirectorysitecollection(v=vs.90).aspx 
    http://msdn.microsoft.com/en-us/library/vstudio/system.directoryservices.activedirectory.activedirectorysite.subnets(v=vs.90).aspx 

    Example

    Dim targetForestName As String = "test.domain.local"
    
    Dim context As New DirectoryContext(DirectoryContextType.Forest, targetForestName, "administrator", "adminpasswort")
    
    Dim forest__1 As Forest = Forest.GetForest(context)
    
    For Each site As ActiveDirectorySite In forest__1.Sites
    	If site.Name = "Sitename" Then
    		add2List(2, "Site", site.Name)
    
    		Console.WriteLine("It contains domain:")
    		For Each d As Domain In site.Domains
    			add2List(2, "Domain", d.Name)
    		Next
    		For Each d As DirectoryServer In site.Servers
    			add2List(2, "server", d.Name)
    			add2List(2, "server-ip", d.IPAddress)
    
    			Dim myEnumerator1 As System.Collections.IEnumerator = d.Partitions.GetEnumerator()
    			While myEnumerator1.MoveNext()
    				add2List(2, "partitions", "" & Convert.ToString(myEnumerator1.Current))
    			End While
    			Dim myEnumerator As System.Collections.IEnumerator = d.InboundConnections.GetEnumerator()
    			While myEnumerator.MoveNext()
    				add2List(2, "inbound", "" & Convert.ToString(myEnumerator.Current))
    			End While
    		Next
    
    
    
    		For Each sl As ActiveDirectorySiteLink In site.SiteLinks
    			add2List(2, "Sitelink", sl.Name)
    			Dim myEnumerator3 As System.Collections.IEnumerator = sl.Sites.GetEnumerator()
    			While myEnumerator3.MoveNext()
    				add2List(2, "site-sites", "" & Convert.ToString(myEnumerator3.Current))
    			End While
    		Next
    
    		For Each subnet As ActiveDirectorySubnet In site.Subnets
    			add2List(2, "Subnet, location, site", (subnet.Name + "; " + subnet.Location & "; ") + subnet.Site)
    		Next
    
    		add2List(2, "InterSiteTopologyGenerator is", "" + site.InterSiteTopologyGenerator)
    	End If
    Next
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, April 27, 2014 4:49 AM
  • User570416984 posted

    Hello there

    This is really great - it answered all my questions! Thanks!

    Just one quick question - Add2List - how is that defined?

    Best Regards

    /Mogens

    Sunday, April 27, 2014 11:20 AM
  • User570416984 posted

    Hey again

    Forget about the Add2List - never mind.

    When I run this on the Domain controller I get the desired result - fantastic!

    When I try to run this from a remote machine (not in the domain) I get the following error:

    A first chance exception of type 'System.DirectoryServices.ActiveDirectory.ActiveDirectoryObjectNotFoundException' occurred in System.DirectoryServices.dll

    Dim TargetForestName As String = "DOMAIN.NET"
    
    Dim Context As New DirectoryContext(DirectoryContextType.Forest, TargetForestName, "domain.net\administrator", "pass")
    
    Dim Forest__1 As Forest = Forest.GetForest(Context)   <--- Error occurres here
    

    Sure I have tried different syntaxes on the TargetForestName and I have made sure that the server knows what server is the Domain Controller (hosts file) - same result.

    Tried also to disable the Firewall on the Domain Controller - same result.

    The intention is that a non-domain server will ask for this information - and I'm happy that the user/pass is included in the code.

    Best Regards

    /Mogens

    Sunday, April 27, 2014 1:34 PM
  • User1508394307 posted

    This is more related to network setup than to asp.net. Ask your sysadmin, he should know how it might need be configured.

    Are these domains are trusted domains in same forest? You can easily check it if you run Active Directory Users and Computers from the second server, Connect to Domain and try to connect to DOMAIN.NET from there. 

    http://technet.microsoft.com/en-us/library/cc775736(v=ws.10).aspx 
    http://technet.microsoft.com/en-us/library/bb742437.aspx 

    Also see the following thread 

    http://forums.asp.net/t/1926618.aspx?Cross+Domain+AD+query 

    Monday, April 28, 2014 11:46 AM