code_junkie

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

Nov 12th, 2011
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.82 KB | None | 0 0
  1. Set MyUser = GetObject ("LDAP://cn=" & uname & ",ou=" & strname & ",DC=bobdom,DC=net")
  2.  
  3. Set MyUser = GetObject ("LDAP://mail=" & uname & ",ou=" & strname & ",DC=bobdom,DC=net")
  4.  
  5. Function getOffice (strname, uname)
  6.  
  7. strEmail = uname
  8. WScript.Echo "email: " & strEmail
  9. Dim objRoot : Set objRoot = GetObject("LDAP://RootDSE")
  10. Dim objDomain : Set objDomain = GetObject("LDAP://" & objRoot.Get("defaultNamingContext"))
  11. Dim cn : Set cn = CreateObject("ADODB.Connection")
  12. Dim cmd : Set cmd = CreateObject("ADODB.Command")
  13. cn.Provider = "ADsDSOObject"
  14. cn.Open "Active Directory Provider"
  15. Set cmd.ActiveConnection = cn
  16.  
  17. cmd.CommandText = "SELECT physicalDeliveryOfficeName FROM '" & objDomain.ADsPath & "' WHERE mail='" & strEmail & "'"
  18. cmd.Properties("Page Size") = 1
  19. cmd.Properties("Timeout") = 300
  20. cmd.Properties("Searchscope") = ADS_SCOPE_SUBTREE
  21.  
  22. Dim objRS : Set objRS = cmd.Execute
  23. If IsNull(objRS.Fields(0)) = TRUE Then
  24. getOffice = "BLANK"
  25. Else
  26. getOffice = objRS.Fields(0)
  27. WScript.Echo getOffice
  28. End If
  29.  
  30.  
  31. Set objRS = Nothing
  32. Set cmd = Nothing
  33. Set cn = Nothing
  34. Set objDomain = Nothing
  35. Set objRoot = Nothing
  36. End Function
  37.  
  38. <LDAP://SERVERNAME/DC=bobdom,DC=net>;(&(objectClass=user)([email protected]));
  39.  
  40. <LDAP://SERVERNAME/DC=bobdom,DC=net>;(&([email protected]));name,mail,member,description,memberOf,userParameters,userAccountControl,whenCreated,CN;subTreeCount=1
  41.  
  42. Set oCon = Server.CreateObject("ADODB.Connection")
  43. oCon.Provider = "ADsDSOObject"
  44. oCon.Open "ADProvider", "ADUsername", "ADPassword"
  45.  
  46. Set oCmd = Server.CreateObject("ADODB.Command")
  47. Set oCmd.ActiveConnection = oCon
  48.  
  49. sQuery = "<LDAP://SERVERNAME/DC=bobdom,DC=net>;(&([email protected]));name,distinguishedName,physicalDeliveryOfficeName;subTreeCount=1>"
  50.  
  51. oCmd.CommandText = sQuery
  52. Set ADRecordSet = oCmd.Execute
Add Comment
Please, Sign In to add comment