VBScript LDAP: Is there a way to query for physicalDeliveryOfficeName using the email address in Active Directory

active-directoryldapvbscript

I'm attempting to utilize VBScript to connect pull the physicalDeliveryOfficeName attribute in Active Directory by providing the email address.

I know how to do it with a common name like the following:

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

However only the email address is available. How to do this? I've even tried

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

and that doesn't work.

Best Solution

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