VBScript LDAP: is there a request method for physicalDeliveryOfficeName using an email address in Active Directory?

I am trying to use VBScript to connect the physicalDeliveryOfficeName attribute in Active Directory with an email address.

I know how to do this with a common name, as shown below:

 Set MyUser = GetObject ("LDAP://cn=" & uname & ",ou=" & strname & ",DC=bobdom,DC=net") 

However, only an email address is available. How to do it? I even tried

 Set MyUser = GetObject ("LDAP://mail=" & uname & ",ou=" & strname & ",DC=bobdom,DC=net") 

and it won’t work.

+4
source share
2 answers

I ended up writing the following:

 Function getOffice (strname, uname) strEmail = uname WScript.Echo "email: " & strEmail Dim objRoot : Set objRoot = GetObject("LDAP://RootDSE") Dim objDomain : Set objDomain = GetObject("LDAP://" & objRoot.Get("defaultNamingContext")) Dim cn : Set cn = CreateObject("ADODB.Connection") Dim cmd : Set cmd = CreateObject("ADODB.Command") cn.Provider = "ADsDSOObject" cn.Open "Active Directory Provider" Set cmd.ActiveConnection = cn cmd.CommandText = "SELECT physicalDeliveryOfficeName FROM '" & objDomain.ADsPath & "' WHERE mail='" & strEmail & "'" cmd.Properties("Page Size") = 1 cmd.Properties("Timeout") = 300 cmd.Properties("Searchscope") = ADS_SCOPE_SUBTREE Dim objRS : Set objRS = cmd.Execute If IsNull(objRS.Fields(0)) = TRUE Then getOffice = "BLANK" Else getOffice = objRS.Fields(0) WScript.Echo getOffice End If Set objRS = Nothing Set cmd = Nothing Set cn = Nothing Set objDomain = Nothing Set objRoot = Nothing End Function 
+4
source

If you are using an LDAP request (not sure if you need the server name there in your case):

 <LDAP://SERVERNAME/DC=bobdom,DC=net>;(&(objectClass=user)( mail=mike.spencer@kenblanchard.com )); 

Trying this in my own environment, it looks like this (with a couple of things generalized):

 <LDAP://SERVERNAME/DC=bobdom,DC=net>;(&( mail=email@company.com ));name,mail,member,description,memberOf,userParameters,userAccountControl,whenCreated,CN;subTreeCount=1 

And the whole batch looks like this (in ASP, if in the .vbs file you only need to change Server.CreateObject to CreateObject ... I think).

 Set oCon = Server.CreateObject("ADODB.Connection") oCon.Provider = "ADsDSOObject" oCon.Open "ADProvider", "ADUsername", "ADPassword" Set oCmd = Server.CreateObject("ADODB.Command") Set oCmd.ActiveConnection = oCon sQuery = "<LDAP://SERVERNAME/DC=bobdom,DC=net>;(&( mail=email@company.com ));name,distinguishedName,physicalDeliveryOfficeName;subTreeCount=1>" oCmd.CommandText = sQuery Set ADRecordSet = oCmd.Execute 

You may need to play with subTreeCount .

+2
source

All Articles