Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Listing Domain Controllers

by fritz1968 (Sexton)
on Sep 23, 2010 at 13:48 UTC ( #861534=perlquestion: print w/replies, xml ) Need Help??
fritz1968 has asked for the wisdom of the Perl Monks concerning the following question:

I want to list all of the domain controllers in a given domain. (This is Microsoft Active Directory). The script would run on a Windows XP or 7 workstation. I found some sample code that uses "use Net::DNS;" but I cannot find a DNS module for the WIN32 environment.

Any help would be greatly appreciated.

Replies are listed 'Best First'.
Re: Listing Domain Controllers
by marto (Archbishop) on Sep 23, 2010 at 13:58 UTC

    "I cannot find a DNS module for the WIN32 environment.

    Add repositories via PPM::Repositories or install via cpan. Regarding the AD question, did you look at any of the modules I suggested in the chatterbox? Win32::NetAdmin, Win32::OLE or the LDAP modules?

      Yes, I did look though the win32::NetAdmin. GetServers looked to be the most promising, but I could not figure out how to list just the Domain Contollers. It seems that it listed everything but the Domain Contollers... including the workstations... and we have 50k+ workstations here.

      I would assume that any of the following SV codes would list the servers:
      SV_TYPE_SERVER
      SV_TYPE_DOMAIN_CTRL

        "Yes, I did look though the win32::NetAdmin. GetServers looked to be the most promising, but I could not figure out how to list just the Domain Contollers."

        Did you miss the GetDomainController and GetAnyDomainController functions? They are the second and third functions listed in the docs.

Re: Listing Domain Controllers
by james2vegas (Chaplain) on Sep 23, 2010 at 13:59 UTC
    You can't install Net::DNS with PPM / CPAN / something else, on Windows?

    Otherwise you can convert something like this, but convert it to use Win32::OLE or DBD::ADO.
Re: Listing Domain Controllers
by bingos (Vicar) on Sep 25, 2010 at 22:39 UTC

    Using Win32::OLE and ADSI you can get this information:

    # This Perl code displays the domain controllers for the specified dom +ain. # --------------------------------------------------------------- # 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 # --------------------------------------------------------------- # ------ SCRIPT CONFIGURATION ------ my $strDomain = ''; # e.g. emea.rallencorp.com # ------ END CONFIGURATION --------- use Win32::OLE 'in'; $Win32::OLE::Warn = 3; my $objRootDSE = Win32::OLE->GetObject("LDAP://$strDomain/RootDSE"); my $objDomain = Win32::OLE->GetObject("LDAP://" . $objRootDSE->Get("de +faultNamingContext")); my $strMasteredBy = $objDomain->GetEx("masteredBy"); foreach my $strNTDSDN (in $strMasteredBy) { my $objNTDS = Win32::OLE->GetObject("LDAP://" . $strNTDSDN); my $objServer = Win32::OLE->GetObject($objNTDS->Parent); print $objServer->Get("dNSHostName"),"\n"; }

    Adapt to taste.

      The above code results in: Can't call method "in" on unblessed reference at... referring to line: foreach my $strNTDSDN (in $strMasteredBy) {

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://861534]
Approved by moritz
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (1)
As of 2018-07-21 18:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?















    Results (450 votes). Check out past polls.

    Notices?