<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Indented! &#187; list domains</title>
	<atom:link href="http://www.indented.co.uk/index.php/tag/list-domains/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.indented.co.uk</link>
	<description></description>
	<lastBuildDate>Fri, 02 Jul 2010 10:45:15 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Listing all domains in a forest</title>
		<link>http://www.indented.co.uk/index.php/2008/10/21/listing-all-domains-in-a-forest/</link>
		<comments>http://www.indented.co.uk/index.php/2008/10/21/listing-all-domains-in-a-forest/#comments</comments>
		<pubDate>Tue, 21 Oct 2008 15:00:13 +0000</pubDate>
		<dc:creator>Chris</dc:creator>
				<category><![CDATA[Active Directory]]></category>
		<category><![CDATA[C# .NET]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[VB .NET]]></category>
		<category><![CDATA[VbScript]]></category>
		<category><![CDATA[list domains]]></category>
		<category><![CDATA[vbs]]></category>

		<guid isPermaLink="false">http://www.highorbit.co.uk/?p=310</guid>
		<description><![CDATA[These snippets of code shows how to search Active Directory using LDAP to return all domains in the current Forest (based on current authentication). For VB and C# a reference to System.DirectoryServices is required within the project. C# .NET // Connect to RootDSE DirectoryEntry RootDSE = new DirectoryEntry(&#34;LDAP://rootDSE&#34;); // Retrieve the Configuration Naming Context from [...]


No related posts.

Related posts brought to you by <a href='http://mitcho.com/code/yarpp/'>Yet Another Related Posts Plugin</a>.]]></description>
			<content:encoded><![CDATA[<p>These snippets of code shows how to search Active Directory using LDAP to return all domains in the current Forest (based on current authentication).<br />
<span id="more-310"></span><br />
For VB and C# a reference to System.DirectoryServices is required within the project.</p>
<h3>C# .NET</h3>
<pre class="brush: csharp;">
// Connect to RootDSE
DirectoryEntry RootDSE = new DirectoryEntry(&quot;LDAP://rootDSE&quot;);

// Retrieve the Configuration Naming Context from RootDSE
string configNC =
  RootDSE.Properties[&quot;configurationNamingContext&quot;].Value.ToString();

// Connect to the Configuration Naming Context
DirectoryEntry configSearchRoot = new DirectoryEntry(&quot;LDAP://&quot; + configNC);

// Search for all partitions where the NetBIOSName is set.
DirectorySearcher configSearch = new DirectorySearcher(configSearchRoot);
configSearch.Filter = (&quot;(NETBIOSName=*)&quot;);

// Configure search to return dnsroot and ncname attributes
configSearch.PropertiesToLoad.Add(&quot;dnsroot&quot;);
configSearch.PropertiesToLoad.Add(&quot;ncname&quot;);
SearchResultCollection forestPartitionList = configSearch.FindAll();

// Loop through each returned domain in the result collection
foreach (SearchResult domainPartition in forestPartitionList)
{
  // domainName like &quot;domain.com&quot;. ncName like &quot;DC=domain,DC=com&quot;
  string domainName = domainPartition.Properties[&quot;dnsroot&quot;][0].ToString();
  string ncName = domainPartition.Properties[&quot;ncname&quot;][0].ToString();
}
</pre>
<h3>PowerShell</h3>
<pre class="brush: powershell;">
# Connect to RootDSE
$rootDSE = [ADSI]&quot;LDAP://RootDSE&quot;

# Connect to the Configuration Naming Context
$configSearchRoot = [ADSI](&quot;LDAP://&quot; + `
  $rootDSE.Get(&quot;configurationNamingContext&quot;))

# Configure the filter
$filter = &quot;(NETBIOSName=*)&quot;

# Search for all partitions where the NetBIOSName is set
$configSearch = New-Object `
  DirectoryServices.DirectorySearcher($configSearchRoot, $filter)

# Configure search to return dnsroot and ncname attributes
$retVal = $configSearch.PropertiesToLoad.Add(&quot;dnsroot&quot;)
$retVal = $configSearch.PropertiesToLoad.Add(&quot;ncname&quot;)

$configSearch.FindAll() | Select-Object `
  @{n=&quot;dnsroot&quot;;e={$_.Properties.dnsroot}}, `
  @{n=&quot;ncname&quot;;e={$_.Properties.ncname}}
</pre>
<h3>VB .NET</h3>
<pre class="brush: vb;">
' Connect to RootDSE
Dim RootDSE As New DirectoryEntry(&quot;LDAP://rootDSE&quot;)

' Retrieve the Configuration Naming Context from RootDSE
Dim configNC As String = _
  RootDSE.Properties(&quot;configurationNamingContext&quot;).Value.ToString()

' Connect to the Configuration Naming Context
Dim configSearchRoot As New DirectoryEntry(&quot;LDAP://&quot; &amp; configNC)

' Search for all partitions where the NetBIOSName is set.
Dim configSearch As New DirectorySearcher(configSearchRoot)
configSearch.Filter = (&quot;(NETBIOSName=*)&quot;)

' Configure search to return dnsroot and ncname attributes
configSearch.PropertiesToLoad.Add(&quot;dnsroot&quot;)
configSearch.PropertiesToLoad.Add(&quot;ncname&quot;)

Dim forestPartitionList As SearchResultCollection
forestPartitionList = configSearch.FindAll()

' Loop through each returned domain in the result collection
For Each domainPartition In forestPartitionList
  ' domainName like &quot;domain.com&quot;. ncName like &quot;DC=domain,DC=com&quot;
  Dim domainName As String = _
    domainPartition.Properties(&quot;dnsroot&quot;)(0).ToString()
  Dim ncName As String = _
    domainPartition.Properties(&quot;ncname&quot;)(0).ToString()
Next
</pre>
<h3>VbScript</h3>
<pre class="brush: vb;">
Dim objConnection, objRootDSE, objRecordSet
Dim strFilter

strFilter = &quot;(NETBIOSName=*)&quot;

Set objConnection = CreateObject(&quot;ADODB.Connection&quot;)
objConnection.Provider = &quot;ADsDSOObject&quot;
objConnection.Open &quot;Active Directory Provider&quot;

Set objRootDSE = GetObject(&quot;LDAP://RootDSE&quot;)
Set objRecordSet = objConnection.Execute( _
  &quot;&lt;LDAP://&quot; &amp; objRootDSE.Get(&quot;configurationNamingContext&quot;) &amp; &quot;&gt;;&quot; &amp; _
  strFilter &amp; &quot;;&quot; &amp; &quot;dnsroot,ncname;subtree&quot;)
Set objRootDSE = Nothing

While Not objRecordSet.EOF
  WScript.Echo Join(objRecordSet.Fields(&quot;dnsroot&quot;).Value)
  WScript.Echo objRecordSet.Fields(&quot;ncname&quot;).Value

  objRecordSet.MoveNext
WEnd
</pre>


<p>No related posts.</p>
<p>Related posts brought to you by <a href='http://mitcho.com/code/yarpp/'>Yet Another Related Posts Plugin</a>.</p>]]></content:encoded>
			<wfw:commentRss>http://www.indented.co.uk/index.php/2008/10/21/listing-all-domains-in-a-forest/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>
