Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

ServiceNow LDAP “Browse” equivalent via script: lookup by Distinguished Name and return sAMAccountNa

Chandra18
Mega Sage

Hi all,

In LDAP Server we can use Browse to enter a DN (example: uid=einstein,dc=example,dc=com) and see the attribute names/values. I need to use the same functionality from a script—specifically, a Background Script—to search a user by DN and return either "sAMAccountName" OR uid .


Chandra18_0-1765188198119.png

I written a script but it is not working for DN 

var ldapServerName = 'LDAP Demo Integration';
var userDn = "uid=einstein,dc=example,dc=com"; 
var gr = new GlideRecord('ldap_server_config');
if (gr.get('name', ldapServerName)) {
    var ldap = new GlideLDAP();
    ldap.setConfigID(gr.sys_id);
    ldap.setup();
    var dn = userDn;
    gs.print("LDAP Filter: " + userDn);
    var results = ldap.getMatching("", userDn, true, 10);
    while (results.next()) {
        gs.print("uid: " + results.getAttribute("uid"));
        gs.print("sAMAccountName: " + results.getAttribute("sAMAccountName")); // AD only
    }
}


Please correct my script where needed but I want to search a single user by Distinguished Name (DN) only

 

Thanks in advance!

2 REPLIES 2

Ankur Bawiskar
Tera Patron
Tera Patron

@Chandra18 

try this

-> getMatching method requires a search filter and not DN as 2nd parameter

var ldapServerName = 'LDAP Demo Integration';
var userDn = "uid=einstein,dc=example,dc=com";
var gr = new GlideRecord('ldap_server_config');
if (gr.get('name', ldapServerName)) {
    var ldap = new GlideLDAP();
    ldap.setConfigID(gr.sys_id);
    ldap.setup();
    // Use the DN as the base DN, and filter to match the object
    var baseDn = userDn;
    var filter = "(objectClass=*)"; // or "(objectClass=person)" for users
    gs.print("LDAP Base DN: " + baseDn);
    gs.print("LDAP Filter: " + filter);
    var results = ldap.getMatching(baseDn, filter, true, 1);
    while (results.next()) {
        gs.print("uid: " + results.getAttribute("uid"));
        gs.print("sAMAccountName: " + results.getAttribute("sAMAccountName")); // AD only
    }
}

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Hi @Ankur Bawiskar 

it is showing error:

Script execution error: Script Identifier: null.null.script, Error Description: Cannot convert null to an object., Script ES Level: 0
Evaluator: com.glide.script.RhinoEcmaError: Cannot convert null to an object. script : Line(14) column(0) 11: gs.print("LDAP Base DN: " + baseDn); 12: gs.print("LDAP Filter: " + filter); 13: var results = ldap.getMatching(baseDn, filter, true, 1); ==> 14: while (results.next()) { 15: gs.print("uid: " + results.getAttribute("uid")); 16: gs.print("sAMAccountName: " + results.getAttribute("sAMAccountName")); // AD only 17: } Stack trace: at null.null.script:14


Full error MSG:

*** Script: LDAP Base DN: uid=einstein,dc=example,dc=com
*** Script: LDAP Filter: (objectClass=*)
LDAP API - LDAPLogger : LDAP Error : javax.naming.NameNotFoundException: [LDAP: error code 32 - No Such Object]; remaining name 'uid=einstein,dc=example,dc=com,dc=example,dc=com': java.naming/com.sun.jndi.ldap.LdapCtx.mapErrorCode(LdapCtx.java:3285) java.naming/com.sun.jndi.ldap.LdapCtx.processReturnCode(LdapCtx.java:3206) java.naming/com.sun.jndi.ldap.LdapCtx.processReturnCode(LdapCtx.java:2997) java.naming/com.sun.jndi.ldap.LdapCtx.searchAux(LdapCtx.java:1875) java.naming/com.sun.jndi.ldap.LdapCtx.c_search(LdapCtx.java:1798) java.naming/com.sun.jndi.toolkit.ctx.ComponentDirContext.p_search(ComponentDirContext.java:392) java.naming/com.sun.jndi.toolkit.ctx.PartialCompositeDirContext.search(PartialCompositeDirContext.java:358) java.naming/com.sun.jndi.toolkit.ctx.PartialCompositeDirContext.search(PartialCompositeDirContext.java:341) java.naming/javax.naming.directory.InitialDirContext.search(InitialDirContext.java:296) com.glide.sys.ldap.AbstractLDAP.getMatching(AbstractLDAP.java:270) com.glide.sys.ldap.LDAP.getMatching(LDAP.java:148) java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77) java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) java.base/java.lang.reflect.Method.invoke(Method.java:569) org.mozilla.javascript.MemberBox.invoke(MemberBox.java:229) org.mozilla.javascript.NativeJavaMethod.call(NativeJavaMethod.java:295) org.mozilla.javascript.ScriptRuntime.doCall(ScriptRuntime.java:3197) org.mozilla.javascript.Interpreter.interpretLoop(Interpreter.java:1968) org.mozilla.javascript.Interpreter.interpret(Interpreter.java:940) org.mozilla.javascript.InterpretedFunction.lambda$call$0(InterpretedFunction.java:127) com.glide.caller.gen.null_null_script.call(Unknown Source) com.glide.script.ScriptCaller.call(ScriptCaller.java:22) org.mozilla.javascript.InterpretedFunction.call(InterpretedFunction.java:125) org.mozilla.javascript.ContextFactory.doTopCall(ContextFactory.java:722) org.mozilla.javascript.ScriptRuntime.doTopCall(ScriptRuntime.java:4812) org.mozilla.javascript.InterpretedFunction.exec(InterpretedFunction.java:141) com.glide.script.ScriptCompiler.executeAndPublishMetric(ScriptCompiler.java:83) com.glide.script.ScriptEvaluator.execute(ScriptEvaluator.java:517) com.glide.script.ScriptEvaluator.evaluate(ScriptEvaluator.java:242) com.glide.script.fencing.GlideScopedEvaluator.evaluateScript(GlideScopedEvaluator.java:384) com.glide.script.fencing.GlideScopedEvaluator.evaluateScript(GlideScopedEvaluator.java:268) com.glide.script.fencing.GlideScopedEvaluator.evaluateScript(GlideScopedEvaluator.java:245) com.glide.processors.ScriptProcessor.evaluateScript0(ScriptProcessor.java:411) com.glide.processors.ScriptProcessor.lambda$evaluateScriptWithRecordingOption$0(ScriptProcessor.java:394) com.glide.rollback.recording.RollbackRecorder.execute(RollbackRecorder.java:67) com.glide.processors.ScriptProcessor.evaluateScriptWithRecordingOption(ScriptProcessor.java:394) com.glide.processors.ScriptProcessor.evaluateScript(ScriptProcessor.java:375) com.glide.processors.ScriptProcessor.runScript(ScriptProcessor.java:272) com.glide.processors.ScriptProcessor.process(ScriptProcessor.java:230) com.glide.processors.AProcessor.runProcessor(AProcessor.java:915) com.glide.processors.AProcessor.processTransaction(AProcessor.java:341) com.glide.processors.ProcessorRegistry.process0(ProcessorRegistry.java:200) com.glide.processors.ProcessorRegistry.process(ProcessorRegistry.java:188) com.glide.ui.GlideServletTransaction.process(GlideServletTransaction.java:62) com.glide.sys.Transaction.run(Transaction.java:3134) com.glide.ui.HTTPTransaction.run(HTTPTransaction.java:44) com.glide.sys.util.sema.SemaphoreQueueThreadPool$Semaphore.runTransaction(SemaphoreQueueThreadPool.java:315) com.glide.sys.util.sema.SemaphoreQueueThreadPool$Semaphore.runThreadImpl(SemaphoreQueueThreadPool.java:289) com.glide.sys.util.sema.SemaphoreQueueThreadPool$Semaphore.runThread(SemaphoreQueueThreadPool.java:136) java.base/java.lang.Thread.run(Thread.java:841)
Script execution error: Script Identifier: null.null.script, Error Description: Cannot convert null to an object., Script ES Level: 0
Evaluator: com.glide.script.RhinoEcmaError: Cannot convert null to an object. script : Line(14) column(0) 11: gs.print("LDAP Base DN: " + baseDn); 12: gs.print("LDAP Filter: " + filter); 13: var results = ldap.getMatching(baseDn, filter, true, 1); ==> 14: while (results.next()) { 15: gs.print("uid: " + results.getAttribute("uid")); 16: gs.print("sAMAccountName: " + results.getAttribute("sAMAccountName")); // AD only 17: } Stack trace: at null.null.script:14