1

Powershell Function: Get-AllDomains (in a forest)

Posted by Tao Yang on 05/08/2011 in Active Directory, PowerShell |

I wrote this Powershell function today as part of a script I’m working on. It is to get a list of Active Directory domains within an Active Directory forest using ADSI:

Function Get-AllDomains
{
$Root = [ADSI]"LDAP://RootDSE"
$oForestConfig = $Root.Get("configurationNamingContext")
$oSearchRoot = [ADSI]("LDAP://CN=Partitions," + $oForestConfig)
$AdSearcher = [adsisearcher]"(&(objectcategory=crossref)(netbiosname=*))"
$AdSearcher.SearchRoot = $oSearchRoot
$domains = $AdSearcher.FindAll()
return $domains
}

image

I don’t have any child domains in my test environment, but if you run this on a domain member computer, it will list all child domains as well as the parent forest domain (I’ve tested in the production environment).

Tags: , ,

1 Comment

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Copyright © 2010-2012 Tao Yang's System Management Blog All rights reserved.
This site is using the Desk Mess Mirrored theme, v2.0.2, from BuyNowShop.com.