VBScript LDAP:有没有办法使用Active Directory中的电子邮件地址查询physicalDeliveryOfficeName?

时间:2021-09-13 02:56:43

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

我正在尝试通过提供电子邮件地址来利用VBScript连接来拉取Active Directory中的physicalDeliveryOfficeName属性。

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.

这不起作用。

2 个解决方案

#1


4  

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 

#2


2  

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

如果使用LDAP查询(不确定在您的情况下是否需要服务器名称):

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

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

在我自己的环境中尝试它,它看起来像这样(有几个通用的东西):

<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 done in a .vbs file you'll need to change the Server.CreateObject to just CreateObject... I think).

整个批处理看起来像这样(在ASP中;如果在.vbs文件中完成,则需要将Server.CreateObject更改为CreateObject ...我认为)。

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 fiddle with subTreeCount.

您可能需要摆弄subTreeCount。

#1


4  

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 

#2


2  

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

如果使用LDAP查询(不确定在您的情况下是否需要服务器名称):

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

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

在我自己的环境中尝试它,它看起来像这样(有几个通用的东西):

<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 done in a .vbs file you'll need to change the Server.CreateObject to just CreateObject... I think).

整个批处理看起来像这样(在ASP中;如果在.vbs文件中完成,则需要将Server.CreateObject更改为CreateObject ...我认为)。

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 fiddle with subTreeCount.

您可能需要摆弄subTreeCount。