List domains (Perl)

This code can be found in Chapter 2 of Active Directory Cookbook, 2nd edition

Purchase XP Cookbook or Networking Recipes for only $25 plus shipping! While supplies last.

Find out how to download all of the Perl code from this site.

# This Perl code gets the list of the domains contained in the 
# forest that the user running the script is logged into

# ---------------------------------------------------------------
# Adapted from VBScript code contained in the book:
#      "Active Directory Cookbook" by Robbie Allen
# Publisher: O'Reilly and Associates
# ISBN: 0-596-00466-4
# Book web site: http://rallenhome.com/books/adcookbook/code.html
# ---------------------------------------------------------------

use Win32::OLE;
$Win32::OLE::Warn = 3;

my $objRootDSE = Win32::OLE->GetObject("LDAP://RootDSE");
my $strADsPath =  "<GC://" . $objRootDSE->Get("rootDomainNamingContext") . ">;";
my $strFilter  = "(objectcategory=domainDNS);";
my $strAttrs   = "name;";
my $strScope   = "SubTree";

my $objConn = Win32::OLE->CreateObject("ADODB.Connection");
$objConn->{Provider} = "ADsDSOObject";
$objConn->Open;
my $objRS = $objConn->Execute($strADsPath . $strFilter . $strAttrs . $strScope);
$objRS->MoveFirst;
while (not $objRS->EOF) {
    print $objRS->Fields(0)->Value,"\n";
    $objRS->MoveNext;
}

This code has been viewed 2357 times.

New from the creators of TechTasks.com: StatSheet.com