Jul 122012
 

So I just found out about Fiverr. Pay $5 for someone to do something for you !
Sounds interesting, so I put up my idea up there.
If anyone wants to have a look at my Fiverr Idea, the link is here

Share
Oct 132011
 

Recently I’ve had to pull out sites and subnets from AD in order to match an IP Address against the subnet that I pulled out to determine whether a workstation is in that site.
I ran into some issues trying to do it in JScript because for some strange reason, the Description attribute for all the subnets is an array, so I had to convert it from a VB array to a Javascript array.

Now, onto the code I used –

First up, is setting up the ADO Connection.

var objRootDSE = GetObject("LDAP://RootDSE");
var strDomain = objRootDSE.Get("configurationNamingContext");
var objConnection = new ActiveXObject("ADODB.Connection");
objConnection.Provider="ADsDSOObject";
objConnection.Open("ADs Provider");
var objCommand = new ActiveXObject("ADODB.Command");
objCommand.ActiveConnection = objConnection;

All that does is setup the connection to AD and open up the connection using the ADs Provider.

Next up, is to run the query which will return all the Subnets within AD.

var Dom = "LDAP://CN=subnets,CN=Sites,"+strDomain;
objCommand.CommandText = "select cn,Name,Location,Description from '"+Dom+"'";
objSiteRecordSet = objCommand.Execute();

This will return a recordset with all subnets and their descriptions and locations.

After that, a simple case of enumerating the recordset to get what we need out of it.
In this example, I have returned just Name and Description, which means that in the earlier query, I needn’t have query all 4 attributes.

while(!(objSiteRecordSet.EoF)) {
var Desc = '';
var IPRange = objSiteRecordSet.Fields('Name').Value;
var Desc = objSiteRecordSet.Fields('Description').Value;
if(Desc == null) {
Desc = IPRange;
} else {
Desc = VBArray(Desc).toArray();
}
var result = [IPRange,Desc];
arrSites.push(result);
objSiteRecordSet.MoveNext;
}

In that code, the results are pushed into the arrSites array to be used elsewhere or to be returned depending on what you’re doing with the data.

Share
May 232011
 

Hi All,

The last few posts I’ve posted used the SQL syntax method to query Active Directory.
This is different to my previous posts where I was using the IADs interface.
With the IADs interface, we would bind to an object and query it that way, with ActiveX Data Objects, we use SQL-like syntax to query Active Directory

This codeblock below will select all users from the domain.


objRootDSE = GetObject("LDAP://RootDSE");
strDomain = objRootDSE.Get("DefaultNamingContext");
/*This gets the Default Naming context, i.e. the domain root*/

objConnection = new ActiveXObject("ADODB.Connection");
objConnection.Provider="ADsDSOObject";
objConnection.Open("ADs Provider");
objCommand = new ActiveXObject("ADODB.Command");
objCommand.ActiveConnection = objConnection;

/*This sets up the Connection to AD, sets the provider to ADsDSOObject, and creates an object that we can use to execute a command*/

var strDom = "LDAP://"+strDomain;
/* The strDom string will later become part of the SQL command that we will execute. If you want to specify a OU or CN to search, here would be the place to put it. */

objCommand.CommandText = "select name from '"+strDom+" WHERE objectCategory = 'user'';

/* Next up is the command itself.*/

objRecordSet = objCommand.Execute();

/* Then we execute the command */
/* Once executed, the command will return an enumeration of the results.*/
/* We will use the standard JS enumeration methods here to get what we need*/

objRecordSet.Movefirst;
while(!(objRecordSet.EoF)) {
objRecordSet.Fields("name").value; // This will be the fields that you Selected in the command's value, if you've selected more than one then you will need a loop or multiple statements to get it all.
objRecordSet.MoveNext;
}

The codeblock will need modification to suit your needs but it well let you search Active Directory with ease from JScript.


References
SQL Dialect
Searching with ActiveX Data Objects (ADO)
Active Directory Services Data Services Objects

Share
May 202011
 

Hi All,

I’ve created a function to search for users in Active Directory.
It’s fairly similar to the previous query to get the Domain Controllers.
The function accepts 2 parameters, the first being the username that’s being searched for, and the second is the type of search, e.g. begins with, or contains.

This code can also easily be modified to show all users in Active Directory by removing the samaccountname section in the query.


function Search(search,SearchType) {
var arrSearchResult = [];
var strSearch = '';
switch(SearchType) {
case "contains":
strSearch = "*"+search+"*";
break;
case "begins":
strSearch = search+"*";
break;
case "ends":
strSearch = "*"+search;
break;
case "exact":
strSearch = search;
break;
default:
strSearch = "*"+search+"*";
break;
}
objRootDSE = GetObject("LDAP://RootDSE");
strDomain = objRootDSE.Get("DefaultNamingContext");

strOU = "OU=Users"; // Set the OU to search here.
strAttrib = "name,samaccountname"; // Set the attributes to retrieve here.

objConnection = new ActiveXObject("ADODB.Connection");
objConnection.Provider="ADsDSOObject";
objConnection.Open("ADs Provider");
objCommand = new ActiveXObject("ADODB.Command");
objCommand.ActiveConnection = objConnection;
var Dom = "LDAP://"+strOU+","+strDomain;
var arrAttrib = strAttrib.split(",");
objCommand.CommandText = "select '"+strAttrib+"' from '"+Dom+"' WHERE objectCategory = 'user' AND objectClass='user' AND samaccountname='"+search+"' ORDER BY samaccountname ASC";

try {

objRecordSet = objCommand.Execute();

objRecordSet.Movefirst;
while(!(objRecordSet.EoF)) {
var locarray = new Array();
for(var y = 0; y < arrAttrib.length; y++) { locarray.push(objRecordSet.Fields(y).value); } arrSearchResult.push(locarray); objRecordSet.MoveNext; } return arrSearchResult; } catch(e) { alert(e.message); } }

This function will return an array within an array with the strAttrib defining which fields are retrieved.
The output can be parsed with 2 for loops like so :


var arrResults = Search('user');
for(a in arrResults) {
for(b in arrResults[a]) {
alert(arrResults[a][b]);
}
}

That code snippet will alert all the attributes of the user queried.

Share
May 192011
 

Hi All,

Another quick one while I’m working on these.

The following will give you the names of all your Domain Controllers in the Domain Controllers OU or at least the computers in the Domain Controllers OU.


function GetDC() {

var arrDCs = [];

objRootDSE = GetObject("LDAP://RootDSE");
strDomain = objRootDSE.Get("DefaultNamingContext");
objConnection = new ActiveXObject("ADODB.Connection");
objConnection.Provider="ADsDSOObject";
objConnection.Open("ADs Provider");
objCommand = new ActiveXObject("ADODB.Command");
objCommand.ActiveConnection = objConnection;
var Dom = "LDAP://OU=Domain Controllers,"+strDomain;
objCommand.CommandText = "select name from '"+Dom+"' WHERE objectCategory = 'computer' ORDER BY name ASC";

objRecordSet = objCommand.Execute();

objRecordSet.Movefirst;

while(!(objRecordSet.EoF)) {
arrDCs.push(objRecordSet.Fields('Name').Value);
objRecordSet.MoveNext;
}
return arrDCs;
}

This function will return an array containing a list of the members names in the Domain Controllers OU.
The list that is returned can be modified by changing the search query.

Share