Querying Active Directory

#import sys
#sys.path.append(r'C:\WINDOWS\system32\windowspowershell\v1.0')

import System,clr
clr.AddReference("System.DirectoryServices")
from System.DirectoryServices import DirectoryEntry

#Script settings. Change this accordingly
myPath = "LDAP://DC=americas,DC=ent,DC=contoso,DC=com"
myFilter = "(SAMAccountName=jlevi)"

root = DirectoryEntry(myPath)
search = System.DirectoryServices.DirectorySearcher(root) 
search.Filter = myFilter
resultCol = search.FindOne();

#get individual AD attributes 
print "Path:\t",resultCol.Properties["distinguishedname"][0]
print "Dir:\t",resultCol.Properties["homedirectory"][0]
print "Email:\t",resultCol.Properties["userprincipalname"][0]

#dump all attributes for the given result node

print str("_ ")*50
for key in resultCol.Properties.PropertyNames:
   for val in resultCol.Properties[key]: 
      #last print arg truncates val to 200 chars 
      print key,":\t\t\t",str(val)[0:200]

Reference: System.DirectoryServices, DirectoryEntry Class

4 comments:

jww said...

Jose, This works great in professional, however, I cannot get it to work in the web player. I am getting this error:

Could not perform action 'SCRIPTNAME'.

at System.DirectoryServices.DirectoryEntry.Bind(Boolean throwIfFail) at System.DirectoryServices.DirectoryEntry.Bind() at System.DirectoryServices.DirectoryEntry.get_AdsObject() at System.DirectoryServices.DirectorySearcher.FindAll(Boolean findMoreThanOne) at System.DirectoryServices.DirectorySearcher.FindOne() at _stub_$350##350(Closure , CallSite , CodeContext , Object ) at Microsoft.Scripting.Actions.MatchCaller.Call2[T0,T1,TRet](Func`4 target, CallSite site, Object[] args) at Microsoft.Scripting.Actions.CallSite`1.UpdateAndExecute(Object[] args) at Microsoft.Scripting.Actions.UpdateDelegates.Update2[T,T0,T1,TRet](CallSite site, T0 arg0, T1 arg1) at $351##351(Closure , Scope , LanguageContext ) at Spotfire.Dxp.Application.ScriptSupport.IronPythonScriptEngine.Execute(ScriptDefinition script, Dictionary`2 scope) at Spotfire.Dxp.Application.Scripting.ScriptService.Execute(ScriptDefinition script, Dictionary`2 scope, LicenseManager licenseManager, InternalLibraryManager internalLibraryManager, NotificationService notificationService) at Spotfire.Dxp.Application.Scripting.ScriptManager.<>c__DisplayClass4.b__3() at Spotfire.Dxp.Framework.Commands.CommandHistory.Transaction(Executor executor, Boolean visible, Boolean sticky, Guid stickyGuid) at Spotfire.Dxp.Framework.Commands.CommandHistory.Transaction(String displayName, Executor executor) at Spotfire.Dxp.Framework.DocumentModel.DocumentNode.Transaction(String displayName, Executor executor) at Spotfire.Dxp.Application.Scripting.ManagedScript.Execute(Dictionary`2 environment) at Spotfire.Dxp.Application.HtmlTextAreaControls.ActionControl.ModifyCore(Object value) at Spotfire.Dxp.Application.Visuals.HtmlTextArea.<>c__DisplayClass14.<>c__DisplayClass16.b__13() at Spotfire.Dxp.Framework.Commands.CommandHistory.Transaction(Executor executor, Boolean visible, Boolean sticky, Guid stickyGuid) at Spotfire.Dxp.Framework.Commands.CommandHistory.Transaction(String displayName, Executor executor) at Spotfire.Dxp.Framework.DocumentModel.DocumentNode.Transaction(String displayName, Executor executor) at Spotfire.Dxp.Application.Visuals.HtmlTextArea.InteractWithControl(String id, Action`1 interaction)

An operations error occurred.


Thanks
Justin

Jose Leviaguirre said...

jww,

Some scripts only work on the client. It has to do with the clr and .NET (AD) references since the webplayer is running in a different environment. Make sure the sSpotfire service account running webplayer services requires permissions to query AD

jww said...

Jose,

I have an update. If you specify the username and password - generally of a service account, it will work via IIS.

Example:
#Add these lines below root = DirectoryEntry(myPath)
root.Username = UserName
root.Password = SecurelyStoredPassword

Justin

Jose Leviaguirre said...

Thank you Justin!