In need of a translator!! Listing Subnets in AD

J

Jerry Negrelli

Can anyone help translate this into C#??? It's supposed
to list all subnets in all AD sites (see URL below). I
placed it in a VB class library (one class with
one "Function", WScript.Echo replaced with a string
concat & return statement) and tried to reference it that
way, but I kept getting "object not an instance" errors.
Rather than try to debug that approach in that pea soup
language, I'd rather just rewrite it in C# so I can see
what the H is happening. Is this VB using
DirectoryServices or Management classes?


Set objRootDSE = GetObject("LDAP://RootDSE")
strConfigurationNC = objRootDSE.Get
("configurationNamingContext")

strSubnetsContainer = "LDAP://cn=Subnets,cn=Sites," &
strConfigurationNC

Set objSubnetsContainer = GetObject(strSubnetsContainer)

objSubnetsContainer.Filter = Array("subnet")

Set objHash = CreateObject("Scripting.Dictionary")

For Each objSubnet In objSubnetsContainer

objSubnet.GetInfoEx Array("siteObject"), 0
strSiteObjectDN = objSubnet.Get("siteObject")
strSiteObjectName = Split(Split(strSiteObjectDN, ",")
(0), "=")(1)

If objHash.Exists(strSiteObjectName) Then
objHash(strSiteObjectName) = objHash
(strSiteObjectName) & "," & _
Split
(objSubnet.Name, "=")(1)
Else
objHash.Add strSiteObjectName, Split
(objSubnet.Name, "=")(1)
End If

Next

For Each strKey In objHash.Keys
WScript.Echo strKey & "," & objHash(strKey)
Next





The VB script was taken from this page:

http://www.microsoft.com/technet/treeview/default.asp?
url=/technet/ScriptCenter/network/scrnet24.asp
 
J

Jerry Negrelli

The AD wasn't configured correctly for one thing, so
there WERE no subnets. However, once that was remedied,
the following C# worked

StringBuilder subnets = new StringBuilder();

DirectoryEntry servers = new DirectoryEntry
(@"LDAP://RootDSE");
DirectoryEntry servers2 = new DirectoryEntry
(@"LDAP://cn=Subnets,cn=Sites," + servers.Properties
["configurationNamingContext"].Value );

foreach(DirectoryEntry child in servers2.Children){
subnets.Append(child.Name.Replace("CN=","").Trim()
+ ",");
}

if(subnets.Length > 0)
subnets.Remove(subnets.Length-1, 1);

return subnets.ToString().Split(',');
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Where i made an error? 2
Adding subnets to AD 1

Top