<?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; VB .NET</title>
	<atom:link href="http://www.indented.co.uk/index.php/category/vb-net/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>Administering Microsoft DNS in PowerShell</title>
		<link>http://www.indented.co.uk/index.php/2008/12/30/administering-microsoft-dns-in-powershell/</link>
		<comments>http://www.indented.co.uk/index.php/2008/12/30/administering-microsoft-dns-in-powershell/#comments</comments>
		<pubDate>Tue, 30 Dec 2008 12:35:50 +0000</pubDate>
		<dc:creator>Chris</dc:creator>
				<category><![CDATA[Microsoft DNS]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[VB .NET]]></category>
		<category><![CDATA[wmi]]></category>

		<guid isPermaLink="false">http://www.highorbit.co.uk/?p=636</guid>
		<description><![CDATA[DNS administration in PowerShell, including tasks like creating zones and adding Host (A) records, can be performed using the WMI interface. Full documentation for the interface is available from Microsoft in the DNS WMI Provider Reference. I have released a PowerShell 2.0 module using the WMI provider here. There are a few limitations of the [...]


Related posts:<ol><li><a href='http://www.indented.co.uk/index.php/2010/01/12/powershell-iis-and-log-settings/' rel='bookmark' title='Permanent Link: PowerShell, IIS and log settings'>PowerShell, IIS and log settings</a> <small>A function to retrieve IIS log settings from a local...</small></li>
</ol>

Related posts brought to you by <a href='http://mitcho.com/code/yarpp/'>Yet Another Related Posts Plugin</a>.]]></description>
			<content:encoded><![CDATA[<p>DNS administration in PowerShell, including tasks like creating zones and adding Host (A) records, can be performed using the WMI interface. Full documentation for the interface is available from Microsoft in the <a href="http://msdn.microsoft.com/en-us/library/ms682128(VS.85).aspx">DNS WMI Provider Reference</a>.</p>
<p>I have released a PowerShell 2.0 module using the WMI provider <a href='http://www.indented.co.uk/index.php/dnsshell/'>here</a>.<br />
<span id="more-636"></span><br />
There are a few limitations of the interface. The properties associated with Aging are read-only and cannot be set. Several of the configuration options are not available including the option to enable GlobalNames with Windows Server 2008.</p>
<h3>Common variables</h3>
<p>The examples below use two common variables. Both should be updated to reflect the environment used to execute any command.</p>
<pre class="brush: powershell;">
$ServerName = &quot;dns01&quot;
$ContainerName = &quot;somedomain.example&quot;
</pre>
<h3>Management Class vs Management Object</h3>
<p>Two different classes from the .NET Framework are used below. The ManagementObject, created using Get-WMIObject, and the ManagementClass created using [WMIClass].</p>
<p>[WMIClass] creates the same object as the following example.</p>
<pre class="brush: powershell;">
$Scope = New-Object Management.ManagementScope(&quot;\\$ServerName\root\MicrosoftDNS&quot;)
$Path = New-Object Management.ManagementPath(&quot;MicrosoftDNS_Zone&quot;)
$Options = New-Object Management.ObjectGetOptions($Null, `
  [System.TimeSpan]::MaxValue, $True)

$ZoneClass = New-Object Management.ManagementClass($Scope, $Path, $Options)
</pre>
<p>In general terms, to change something that already exists use the properties and methods of a ManagementObject via Get-WMIObject. To add something new use the methods associated with a ManagementClass.</p>
<h4>Creating an instance of a Management Object</h4>
<pre class="brush: powershell;">
$Zones = Get-WMIObject -Computer $ServerName `
  -Namespace &quot;root\MicrosoftDNS&quot; -Class &quot;MicrosoftDNS_Zone&quot;
</pre>
<h4>Exploring the Management Object</h4>
<p>Showing the object type:</p>
<pre class="brush: plain;">
PS C:\Stuff\Scripts\PowerShell&gt; $Zones.GetType()

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     Object[]                                 System.Array
</pre>
<p>Showing the properties and methods:</p>
<pre class="brush: plain;">
PS C:\Stuff\Scripts\PowerShell&gt; $Zones | Get-Member

   TypeName: System.Management.ManagementObject#rootMicrosoftDNSMicrosoftDNS_Zone

Name                      MemberType   Definition
----                      ----------   ----------
AgeAllRecords             Method       Management.ManagementBaseObject AgeAllRec...
ChangeZoneType            Method       Management.ManagementBaseObject ChangeZon...
ForceRefresh              Method       Management.ManagementBaseObject ForceRefr...
GetDistinguishedName      Method       Management.ManagementBaseObject GetDistin...
PauseZone                 Method       Management.ManagementBaseObject PauseZone()
ReloadZone                Method       Management.ManagementBaseObject ReloadZone()
ResetSecondaries          Method       Management.ManagementBaseObject ResetSeco...
ResumeZone                Method       Management.ManagementBaseObject ResumeZone()
UpdateFromDS              Method       Management.ManagementBaseObject UpdateFro...
WriteBackZone              Method       Management.ManagementBaseObject WriteBack...
Aging                     Property     Boolean Aging {get;set;}
AllowUpdate               Property     UInt32 AllowUpdate {get;set;}
AutoCreated               Property     Boolean AutoCreated {get;set;}
AvailForScavengeTime       Property     UInt32 AvailForScavengeTime {get;set;}
Caption                   Property     String Caption {get;set;}
ContainerName             Property     String ContainerName {get;set;}
DataFile                  Property     String DataFile {get;set;}
Description               Property     String Description {get;set;}
DisableWINSRecordReplicat Property     Boolean DisableWINSRecordReplication {ge...
DnsServerName             Property     String DnsServerName {get;set;}
DsIntegrated              Property     Boolean DsIntegrated {get;set;}
ForwarderSlave            Property     Boolean ForwarderSlave {get;set;}
ForwarderTimeout          Property     UInt32 ForwarderTimeout {get;set;}
InstallDate               Property     String InstallDate {get;set;}
LastSuccessfulSoaCheck    Property     UInt32 LastSuccessfulSoaCheck {get;set;}
LastSuccessfulXfr         Property     UInt32 LastSuccessfulXfr {get;set;}
LocalMasterServers        Property     String[] LocalMasterServers {get;set;}
MasterServers             Property     String[] MasterServers {get;set;}
Name                      Property     String Name {get;set;}
NoRefreshInterval         Property     UInt32 NoRefreshInterval {get;set;}
Notify                    Property     UInt32 Notify {get;set;}
NotifyServers             Property     String[] NotifyServers {get;set;}
Paused                    Property     Boolean Paused {get;set;}
RefreshInterval           Property     UInt32 RefreshInterval {get;set;}
Reverse                   Property     Boolean Reverse {get;set;}
ScavengeServers           Property     String[] ScavengeServers {get;set;}
SecondaryServers          Property     String[] SecondaryServers {get;set;}
SecureSecondaries         Property     UInt32 SecureSecondaries {get;set;}
Shutdown                  Property     Boolean Shutdown {get;set;}
Status                    Property     String Status {get;set;}
UseNBStat                 Property     Boolean UseNBStat {get;set;}
UseWins                   Property     Boolean UseWins {get;set;}
ZoneType                  Property     UInt32 ZoneType {get;set;}
...
</pre>
<p>Showing a subset of the properties within the object:</p>
<pre class="brush: plain;">
PS C:\Stuff\Scripts\PowerShell&gt; $Zones | Select-Object Name,DsIntegrated,ZoneType,Reverse

Name                            DsIntegrated            ZoneType             Reverse
----                            ------------            --------             -------
1.2.3.in-addr.arpa                     False                   1                True
1.2.4.in-addr.arpa                     False                   1                True
1.2.5.in-addr.arpa                     False                   1                True
</pre>
<h4>Creating an instance of a Management Class</h4>
<pre class="brush: powershell;">
$ZoneClass = [WMIClass]&quot;\\$ServerName\root\MicrosoftDNS:MicrosoftDNS_Zone&quot;
</pre>
<h4>Exploring the Management Class</h4>
<p>Showing the object type:</p>
<pre class="brush: plain;">
PS C:\Stuff\Scripts\PowerShell&gt; $ZoneClass.GetType()

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     ManagementClass                          System.Management.ManagementObject
</pre>
<p>Showing the properties and methods:</p>
<pre class="brush: plain;">
PS C:\Stuff\Scripts\PowerShell&gt; $ZoneClass | Get-Member

   TypeName: System.Management.ManagementClass#ROOTMicrosoftDNSMicrosoftDNS_Zone

Name                   MemberType    Definition
----                   ----------    ----------
Name                   AliasProperty Name = __Class
CreateZone             Method        System.Management.ManagementBaseObject CreateZone(Sys...
...
</pre>
<h4>Listing the parameters required for a method</h4>
<p>The output above truncates the strings detailing the parameters used for each method. The following shows how the full list can be displayed.</p>
<pre class="brush: powershell;">
([WMIClass]&quot;\\$ServerName\root\MicrosoftDNS:MicrosoftDNS_AType&quot;) | `
  Get-Member -Name CreateInstanceFromPropertyData | Format-List
</pre>
<p>Note that while this shows the parameter type it does not show whether or not the parameter is required or optional. For full details refer to the <a href="http://msdn.microsoft.com/en-us/library/ms682128(VS.85).aspx">DNS WMI Provider Reference</a>.</p>
<h3>Creating Zones</h3>
<p>The following values represent the Zone Types available with Microsoft DNS.</p>
<pre class="brush: plain;">
0 Primary zone
1 Secondary zone
2 Stub zone
 * Windows Server 2003:  This zone type is introduced in Windows Server 2003.
3 Zone forwarder
 * Windows Server 2003:  This zone type is introduced in Windows Server 2003.
</pre>
<h4>Create a Forward  or Reverse Lookup Zone</h4>
<p>This example shows all of the possible parameters, this can be reduced to a single line by dropping the comments and use of variables. Note that any optional variable can be set to $Null or &#8220;&#8221;.</p>
<pre class="brush: powershell;">
# Forward Lookup zone name (example)
$Name = &quot;somedomain.example&quot;
# Reverse Lookup zone name (example for 1.2.3.x Subnet)
$Name = &quot;3.2.1.in-addr.arpa&quot;
# See above
$Type = 0
# AD Integration (only valid on Active Directory Domain Controllers)
$IsDSIntegrated = $False
# FileName (Optional and only valid for zones with AD integrated set to $False)
# File must exist if specified and have size greater than 0b.
$Filename = $Null
# Master IP (Optional and only valid for Secondary, Stub and Forwarder zones)
$MasterIP = $Null
# AdminEmail (Optional and only valid for Primary zones, writes into SOA record)
$AdminEmail = $Null

$NewZone = ([WMIClass]&quot;\\$ServerName\root\MicrosoftDNS:MicrosoftDNS_Zone&quot;).CreateZone( `
  $Name, $Type, $IsDSIntegrated, $FileName, $MasterIP, $AdminEmail)
</pre>
<h4>Examples</h4>
<pre class="brush: powershell;">
# New Standard Primary Forward Lookup Zone
$NewZone = ([WMIClass]&quot;\\$ServerName\root\MicrosoftDNS:MicrosoftDNS_Zone&quot;).CreateZone( `
  &quot;standardprimary.example&quot;, 0, $False)
# New Standard Primary Forward Lookup Zone using existing zone file
$NewZone = ([WMIClass]&quot;\\$ServerName\root\MicrosoftDNS:MicrosoftDNS_Zone&quot;).CreateZone( `
  &quot;standardprimary-existing.example&quot;, 0, $False, &quot;standardprimary-existing.example.dns&quot;)
# New Active Directory Integrated Forward Lookup Zone
$NewZone = ([WMIClass]&quot;\\$ServerName\root\MicrosoftDNS:MicrosoftDNS_Zone&quot;).CreateZone( `
  &quot;adprimary.example&quot;, 0, $True)
# New Standard Primary Reverse Lookup Zone
$NewZone = ([WMIClass]&quot;\\$ServerName\root\MicrosoftDNS:MicrosoftDNS_Zone&quot;).CreateZone( `
  &quot;1.168.192.in-addr.arpa&quot;, 0)
# New Active Directory Integrated Reverse Lookup Zone
$NewZone = ([WMIClass]&quot;\\$ServerName\root\MicrosoftDNS:MicrosoftDNS_Zone&quot;).CreateZone( `
  &quot;2.168.192.in-addr.arpa&quot;, 0, $True)
# New Secondary Forward Lookup Zone
$NewZone = ([WMIClass]&quot;\\$ServerName\root\MicrosoftDNS:MicrosoftDNS_Zone&quot;).CreateZone( `
  &quot;standardsecondary.example&quot;, 1, $False, &quot;&quot;, `
  @(&quot;192.168.0.1&quot;, &quot;192.168.0.2&quot;))
# New Stub Zone
$NewZone = ([WMIClass]&quot;\\$ServerName\root\MicrosoftDNS:MicrosoftDNS_Zone&quot;).CreateZone( `
  &quot;stub.example&quot;, 2, $False, &quot;&quot;, @(&quot;192.168.0.1&quot;, &quot;192.168.0.2&quot;))
# New Conditional Forwarder
$NewZone = ([WMIClass]&quot;\\$ServerName\root\MicrosoftDNS:MicrosoftDNS_Zone&quot;).CreateZone( `
  &quot;conditionalforwarder.example&quot;, 3, $False, &quot;&quot;, @(&quot;192.168.0.1&quot;, &quot;192.168.0.2&quot;))
# New AD Integrated Conditional Forwarder
$NewZone = ([WMIClass]&quot;\\$ServerName\root\MicrosoftDNS:MicrosoftDNS_Zone&quot;).CreateZone( `
  &quot;adconditionalforwarder.example&quot;, 3, $True, &quot;&quot;, @(&quot;192.168.0.1&quot;, &quot;192.168.0.2&quot;))
</pre>
<h3>Creating resource records with CreateInstanceFromPropertyData</h3>
<p>CreateInstanceFromPropertyData is available on each individual record class. For example, the method can be invoked from MicrosoftDNS_AType, or MicrosoftDNS_MXType, and so on. Note that the syntax for the method varies slightly depending on the record type.</p>
<h4>Create an A record</h4>
<pre class="brush: powershell;">
# Record Name (Owner Name). Should include full suffix to prevent the method
# throwing an error.
$OwnerName = &quot;www.$ContainerName&quot;
# Class, as in IN, CS, CH or HS. Normally only care about IN (Internet: 1) which
# is the default. (Optional)
$RecordClass = $Null
# Time To Live in seconds (Optional)
$TTL = $Null
# IP Address is required
$IPAddress = &quot;1.2.3.4&quot;

$NewATypeClass = [WMIClass]&quot;\\$ServerName\root\MicrosoftDNS:MicrosoftDNS_AType&quot;
$NewARecord = $NewATypeClass.CreateInstanceFromPropertyData( `
  $ServerName, $ContainerName, $OwnerName, $RecordClass, $TTL, $IPAddress)
</pre>
<h4>Create an MX record</h4>
<pre class="brush: powershell;">
# Record Name (Owner Name). Normally the SMTP domain, in this case it matches
# ContainerName.
$OwnerName = $ContainerName
$RecordClass = $Null
$TTL = $Null
# Preference, numeric value used to determine the preferred server(s)
$Preference = 10
# The server used to handle the mail
$MailExchange = &quot;mail.somedomain.example&quot;

$NewMXTypeClass = [WMIClass]&quot;\\$ServerName\root\MicrosoftDNS:MicrosoftDNS_MXType&quot;
$NewMXRecord = $NewMXTypeClass.CreateInstanceFromPropertyData( `
  $ServerName, $ContainerName, $OwnerName, $RecordClass, $TTL, $Preference, $MailExchange)
</pre>
<h3>Creating resource records with CreateInstanceFromTextRepresentation</h3>
<p>CreateInstanceFromTextRepresentation is available from the MicrosoftDNS_ResourceRecord class. It takes fewer parameters than the previous method but ultimately requires exactly the same information.</p>
<h4>Create an NS record</h4>
<pre class="brush: powershell;">
# The text version of the record. Must include Class (IN) or this will fail.
# @ represents the origin, or zone / domain name.
$TextRepresentation = &quot;@ IN NS ns1.somedomain.net&quot;

$NewRRClass = [WMIClass]&quot;\\$ServerName\root\MicrosoftDNS:MicrosoftDNS_ResourceRecord&quot;
$NewRR = $NewRRClass.CreateInstanceFromTextRepresentation( `
  $ServerName, $ContainerName, $TextRepresentation)
</pre>
<h4>Create a TXT record</h4>
<pre class="brush: powershell;">
# Using @ with the TXT record here will cause an error when executing the method.
# Instead, this uses the $ContainerName variable.
# Unlike the zone file itself the method does not require a terminating
# period following each name.
$TextRepresentation = &quot;$ContainerName IN TXT `&quot;hello world`&quot;&quot;

$NewRRClass = [WMIClass]&quot;\\$ServerName\root\MicrosoftDNS:MicrosoftDNS_ResourceRecord&quot;
$NewRR = $NewRRClass.CreateInstanceFromTextRepresentation( `
  $ServerName, $ContainerName, $TextRepresentation)
</pre>
<h3>Update server data file</h3>
<p>DNS zones are held in memory, any change to the zone is performed in memory rather than as a direct alteration of the zone file. The following method can be used to force the updated zone to write back to the file.</p>
<pre class="brush: powershell;">
(Get-WMIObject -Computer $ServerName `
  -Namespace &quot;root\MicrosoftDNS&quot; -Class &quot;MicrosoftDNS_Zone&quot; `
  -Filter &quot;ContainerName='$ContainerName'&quot;).WriteBackZone()
</pre>
<h3>Reload a zone</h3>
<p>Changes made to the zone file can be loaded into memory immediately using the ReloadZone method.</p>
<pre class="brush: powershell;">
(Get-WMIObject -Computer $ServerName `
  -Namespace &quot;root\MicrosoftDNS&quot; -Class &quot;MicrosoftDNS_Zone&quot; `
  -Filter &quot;ContainerName='$ContainerName'&quot;).ReloadZone()
</pre>
<h3>Enabling and starting scavenging on a server</h3>
<p>Enabling scavenging requires setting the ScavengingInterval property to a non-zero value. The value representing the interval uses Hours.</p>
<pre class="brush: powershell;">
# Connecting to the Server Management Object
$Server = Get-WMIObject -Computer $ServerName `
  -Namespace &quot;root\MicrosoftDNS&quot; -Class &quot;MicrosoftDNS_Server&quot;
# Setting a new Scavenging Interval
$Server.ScavengingInterval = 24
$Server.Put()
# Start Scavenging
$Server.StartScavenging()
</pre>
<h3>Clear the cache</h3>
<p>Any cached entries on a server can be cleared using the ClearCache method of the MicrosoftDNS_Cache class.</p>
<pre class="brush: powershell;">
(Get-WMIObject -Computer $ServerName `
  -Namespace &quot;root\MicrosoftDNS&quot; -Class &quot;MicrosoftDNS_Cache&quot;).ClearCache()
</pre>
<h3>Displaying the DNS server statistics</h3>
<p>Each DNS server holds a variety of statistics that can help to evaluate server performance.</p>
<pre class="brush: powershell;">
Get-WMIObject -Computer $ServerName `
  -Namespace &quot;root\MicrosoftDNS&quot; -Class &quot;MicrosoftDNS_Statistic&quot; | `
    Select-Object Name, StringValue, Value
</pre>


<p>Related posts:<ol><li><a href='http://www.indented.co.uk/index.php/2010/01/12/powershell-iis-and-log-settings/' rel='bookmark' title='Permanent Link: PowerShell, IIS and log settings'>PowerShell, IIS and log settings</a> <small>A function to retrieve IIS log settings from a local...</small></li>
</ol></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/12/30/administering-microsoft-dns-in-powershell/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<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>
