Jul 222013
 

Just a quick snippet of code to convert seconds to hours, minutes, and seconds, with the hours shown optionally.


function fromSeconds(seconds, showHours = false) {
if(showHours) {
var hours = Math.floor(seconds / 3600);
seconds = seconds - hours * 3600;
}
var minutes = (Math.floor(seconds/60) < 10) ? "0" + Math.floor(seconds/60) : Math.floor(seconds/60); var seconds = (seconds % 60 > 9) ? seconds % 60 : "0" + seconds % 60;
if(showHours) {
var timestring = hours+":"+minutes+":"+seconds;
} else {
var timestring = minutes+":"+seconds;
}
return timestring;
}

Share
Jul 132012
 

Recently, I wanted to make a script to read the subjects from a set of emails that I was receiving that was telling me what jobs I’d been assigned in the internal ticketing system at work.
Each time I got assigned a job, it would send me an email. Using this little scriptlet, I made a display to show me what the subjects were so I could see what jobs I had.

First step is to create the ActiveX object that taps into Outlook, and set that object up.

outlookApp = new ActiveXObject("Outlook.Application");
nameSpace = outlookApp.getNameSpace("MAPI");
nameSpace.logon("","",false,false);

Once the object has been setup, we can select the desired folder.
The getDefaultFolder(6) selects the Inbox as the folder start from.
The Inbox object then has all the folders under the Inbox assigned to it.

mailFolder = nameSpace.getDefaultFolder(6);
var Inbox = mailFolder.Folders;

Next up, I’m selecting the subfolder that I want the email subjects from, and assigning it to the box variable.

var box = Inbox.Item("SRs Assigned To Me").Items;

This bit is where we read the subjects from the emails themselves.
We get the first item in the folder using the GetFirst method, then we use a while loop to keep getting the subjects until there are no more emails. In this example, all the subjects are appended to the list variable, but you can choose to put them into an array, or echo them out directly using WScript.Echo. The choice is up to you.

var boxmail = box.GetFirst;
var list = '';
while(boxmail != null) {
list += boxmail.subject+"\n";
boxmail = box.GetNext;
}

Lastly, we tell the script to tell outlook to close all the connections that it opened to read the emails.

nameSpace.logoff();
olLoggedOn = false;
nameSpace = null;
outlookApp = null;

With this script, you can view other properties of the emails, or count how many emails you have in a specific folder, or even in the Inbox itself. Putting this snippet into an HTA will even allow you to make a little email counter window.

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