List Servers in Active Directory

A Script to List Servers in Your Active Directory:  There are two ways to maintain a list of servers in your data center. Grab a clipboard and walk the aisles of your data center, or get the list using Active Directory scripts!  Here's a Perl script that searches your domain for any computers that have the word "server" in the operating system.  Just another ADO search;  we're searching for computer objects (objectCategory=computer) whose operatingSystem attribute has the string "server" in it (operatingSystem=*server*). Like I've said before, learning how to build the query is the key to getting information out of AD. It's a piece of cake. 

The script returns the server name, operating system version, and the distinguished name (the full path to the computer account in Active Directory), and prints it out to a text file (servers.txt).

I saw someone searching for this recently, so whoever you are, here it is!

use Win32::OLE;
open OUT,">servers.txt\n";
$dse=Win32::OLE->GetObject("LDAP://RootDSE");
$root=$dse->Get("DefaultNamingContext");
$adpath="LDAP://$root";
$base="<".$adpath.">";
$connection = Win32::OLE->new("ADODB.Connection");
$connection->{Provider} = "ADsDSOObject";
$connection->Open("ADSI Provider");
$command=Win32::OLE->new("ADODB.Command");
$command->{ActiveConnection}=$connection;
$command->{Properties}->{'Page Size'}=1000;
$rs = Win32::OLE->new("ADODB.RecordSet");
$command->{CommandText}="$base;(&(objectCategory=Computer)(operatingSystem=*server*));cn,distinguishedName,operatingSystem;subtree";
$rs=$command->Execute;
until ($rs->EOF){
 $cn=$rs->Fields(0)->{Value};
 $dn=$rs->Fields(1)->{Value};
 $os=$rs->fields(2)->{Value};
 print "$cn\t$os\n";
 print OUT "$cn\t$os\t$dn\n";
 $rs->MoveNext;
}
close OUT;

0 comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...