LDAP Query - Javascript

I have to build an error workflow task for a customer, the process should warn the user by email, in case of any errors in the current workflow proces.
I have the users ldap name form the print job.
Have anybody tried to build a javascript in the workflow for ldap/ldaps tasks -to retrieve ldap info like the email address ?

Tried to use a powershell script but it seams like the AD servers does not have that option enabled.

Microsoft ADExplore works fine on the same server with the domain name and a login name.

Most of the javascript I can find on the web, are build for web pages - not exactly the same a script in the workflow.

Thanks in advance

Here’s a script that I used to query our AD for information about users:

// GET USER INFO FROM ACTIVE DIRECTORY
// See http://www.kouti.com/tables/userattributes.htm for list of RecordSet fields
var connString = "<your connection string>";
var requesterName = "<the user to use to query AD>";
var requesterPW = "<its password>";
var userEmail = "<email of the user you want info on>";
var manager = "";

var userObject = {
  manager: "",
  managerMail: ""
};

var managerObject = {
  email : ""
};

//Search for user's manager
var ldapFilter = "(mail=" + userEmail + ");";
var fieldList = "manager";

var ado = new ActiveXObject("ADODB.Connection");
ado.Provider = "ADSDSOObject";
ado.Properties("User ID") = requesterName;
ado.Properties("Password") = requesterPW;
ado.Properties("Encrypt Password") = true;
ado.Properties("ADSI Flag") = 3;
ado.Open("Search For User");

var rs = ado.Execute(connString + ldapFilter + fieldList + ";subtree");
if(!rs.EOF){
   rs.MoveFirst;
   userObject.manager     = /^CN=([a-zA-Z\sÀ-ÿ]+)/i.exec(rs.Fields("manager").Value)[1];
}

Watch.log("manager= "+userObject.manager,2);
ado.Close;

//Search for manager's email
ado.Open("Search For Email");

ldapFilter = "(CN=" + userObject.manager + ");";
fieldList = "mail";

rs = ado.Execute(connString + ldapFilter + fieldList + ";subtree");
if(!rs.EOF){
   rs.MoveFirst;
   managerObject.email = rs.Fields("mail").Value;
}

userObject.managerMail = managerObject.email;

ado.Close;

Watch.SetJobinfo(9, JSON.stringify(userObject));


Hope that helps.

1 Like

I have really been struggling with this the most of the day, without any results.

The code I have now is this, but it does not work, fails in the execute command - and does not tell me why. (Provider: One or more errors occurred during processing of command.)

// Base data
var connString = “ldap://OU=Onesystem,OU=Organisationer,DC=x1,DC=x2,DC=x3,DC=dk”;
var requesterName = “username”;
var requesterPW = “userpwd”;
var userToFind = “ZYGP”;

//Filter
var SrchCriteria = “(&(objectClass=user)(sAMAccountName=” + userToFind + “))”;
var ldapAttribut = “mail”;

//Connection
var ado = new ActiveXObject(“ADODB.Connection”);
ado.Provider = “ADSDSOObject”;
ado.Properties(“User ID”) = requesterName;
ado.Properties(“Password”) = requesterPW;
ado.Properties(“Encrypt Password”) = true;
ado.Properties(“ADSI Flag”) = 3;
ado.Open(“Search For mail”);

//Execute
exeCommand = connString + “;” + SrchCriteria + “;” + ldapAttribut + “;” + “subtree”;
Watch.Log("exeCommand: " + exeCommand, 3);
var result = ado.Execute(exeCommand);
watch.log("User: " + result, 3);
ado.Close;

Finaly figured it out -ended up with this:

// Variables & filters
var connString = “LDAP://OU=Onesystem,OU=Organisationer,DC=xx1,DC=xx2,DC=xx3,DC=dk”;
var user = “ZYGP”;
var SrchCriteria = “(&(objectClass=user)(sAMAccountName=” + user + “))”;
var ldapAttribut = “mail”;

//Connection
var ado = new ActiveXObject(“ADODB.Connection”);
ado.Provider = “ADSDSOObject”;
ado.Open(“Search For mail”);

//Execute
var ldapresult = ado.Execute( connString + “;” + SrchCriteria + “;” + ldapAttribut + “;” + “subtree”);

//Get Result
var email = ldapresult.Fields(“mail”);
Watch.Log("Email: " + email, 3);

//Close connection
ado.Close;

1 Like

Out of curiosity: using a different connection string did the trick?

Looks like this web page removes <> in the connection string

var connString = “<LDAP://Place to serarch>”;

Klaus

Tip: Please embed code samples in three backticks as shown in the example below to avoid this.

code-sample_20241111
Result:

console.log("1 > 2 = " + (1 > 2));
1 Like