Wednesday, April 21, 2010

How to enumerate a User OU in JavaScript

This simple script finds a user in AD and then display it's Common Name and Phone Number via Command Line. Then it dictates the name and phone number of the given user by using SAPI. Be sure to save as .js format. To reference from command line:

cscript name_of_file.js "user to find"

var username, rootDSE, domainNC, connection, command, recordset;
var voice = new ActiveXObject("SAPI.SpVoice");

username = WScript.Arguments.Item(0);

rootDSE = GetObject("LDAP://rootDSE");
domainNC = rootDSE.Get("defaultNamingContext");

connection = new ActiveXObject("ADODB.Connection");
connection.Open("Provider=ADsDSOObject;");
command = new ActiveXObject("ADODB.Command");

command.ActiveConnection = connection;
command.CommandText = ";" +
"(sAMAccountName=" + username + ");" +
"CN,telephoneNumber;subtree";
command.Properties("Cache Results") = false;
command.Properties("Page Size") = 100;

recordset = command.Execute();

while (! recordset.EOF) {
voice.Speak(recordset.Fields.Item("CN").Value);
WScript.Echo(recordset.Fields.Item("CN").Value + "\t" +
recordset.Fields.Item("telephoneNumber").Value);

recordset.MoveNext();
}

connection.Close();

No comments:

Post a Comment