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
Jul 022011
 

Hi All,

I have just worked up a short script to search a SQL server using Javascript.
Using ActiveX and the ADoDB.Connection object this enables me to build a little HTA frontend to do what I need.

Firstly, we create the ADODB object.

var objConnection = new ActiveXObject("adodb.connection");

Then, set the options we want for the connection.
I’m connecting to a SQL server, that is hosted on “server1”, and the database i want is “db1”, and I’m connecting with username and passwords.

var strConn = "driver={sql server};server=server1;database=db1;uid=username;password=password";

Then we open the connection with the options specified.

objConnection.Open(strConn);

Once the connection has been opened, we use the ADODB.Recordset object to execute a query and retrieve results from SQL.


var rs = new ActiveXObject("ADODB.Recordset");
var strQuery = "SELECT * FROM users WHERE username = 'user1'";
rs.Open(strQuery,objConnection);

Once the query has been executed, the results will be stored in the recordset.
You will now be able to view the results using the standard recordset functions.


rs.MoveFirst
while(!rs.eof) {
//Do your thing here
rs.movenext;
}

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
May 192011
 

Hi all,

I covered this in another post, but thought I would make it separate to make it easier to find.

The following code block lets you get the distinguished name of the username that is passed into this function.


function GetDN(strUsername) {

var objUserDN = new ActiveXObject("NameTranslate");
objUserDN.Init(1,"< DOMAIN NAME HERE >");
objUserDN.Set(3,"< DOMAIN NAME HERE >\\"+strUsername);
strUserDN = objUserDN.get(1)
return strUserDN;
}

This codeblock does the following :

  1. Creates a new NameTranslate Object and binds objUserDN to it
  2. Initialises NameTranslate with the Init method with the ‘1’ setting the type of bind used, which is Domain in this case.
  3. Sets the type of translation used and the username to be translated by using the set method, the 3 specifies that the username that is input is in the NT format of “domain\User”
  4. Gets the Distinguished Name by using the Get method with a parameter of 1 which specifies that it should get the RFC 1779 formatted name.
  5. Returns the distinguished name

References :
ADS_NAME_INITTYPE_ENUM Enumeration
ADS_NAME_TYPE_ENUM Enumeration

Share