2013-10-22 51 views
1

我有我的脚本来通过displayname进行搜索并返回userid,它工作正常。VBScript Active Directory通过displayname搜索并收到2的相同

但是当我遇到了在公元2项即

  1. 帕夫莱·斯托亚诺维奇一个显示名称 - 他是从公司1
  2. 帕夫莱·斯托亚诺维奇 - 他是从公司2

用户ID不会显示,因为脚本不知道该怎么办?

我该怎么过来呢?如果我得到2或更多的回报,我想在输出中说嘿,我找到了两次相同的名称等等。这里是两个userids和公司。

如果你想看到剧本的下面......

strFile = objFSO.GetParentFolderName(Wscript.ScriptFullName) & "\users.xls" 

Set objWorkbook = objExcel.Workbooks.Open(strFile) 
objWorkbook.Activate 
objExcel.Visible = False 


intRow = 2 ' starts reading file at line 2 


' this part runs a loop through the excel file reading each userid and getting data requested. 
' --------------------------------------------------------------------------------------------- 

Do Until objExcel.Cells(intRow,1).Value = "" 

ExcelRow = objExcel.Cells(intRow, 1) 
Call GetOU ' calling sub to search 
intRow = intRow + 1 

Loop 



' This section just formats the excel file to widen the columns 
' -------------------------------------------------------------- 

Set objRange = objExcel.Range("A1") 
objRange.Activate 
Set objRange = objExcel.ActiveCell.EntireColumn 
objRange.AutoFit() 

Set objRange = objExcel.Range("B1") 
objRange.Activate 
Set objRange = objExcel.ActiveCell.EntireColumn 
objRange.AutoFit() 

Set objRange = objExcel.Range("C1") 
objRange.Activate 
Set objRange = objExcel.ActiveCell.EntireColumn 
objRange.AutoFit() 

Set objRange = objExcel.Range("D1") 
objRange.Activate 
Set objRange = objExcel.ActiveCell.EntireColumn 
objRange.AutoFit() 

objExcel.ActiveWorkbook.Save 
objExcel.Quit 




' Sub to get Details for user 
' ---------------------------- 

Sub GetOU 

On Error Resume Next 

Set objRootDSE      = GetObject("LDAP://RootDSE") 
strDomain       = objRootDSE.Get("DefaultNamingContext") 
Set objConnection     = CreateObject("ADODB.Connection") 
objConnection.Provider    = "ADsDSOObject" 
objConnection.Open "Active Directory Provider" 
Set objCommand      = CreateObject("ADODB.Command") 
Set objCommand.ActiveConnection  = objConnection 
objCommand.Properties("Size Limit") = 100000 
objCommand.Properties("Searchscope") = 2 
objCommand.CommandText = "SELECT distinguishedName FROM 'LDAP://" & _ 
strDomain & _ 
"' WHERE objectCategory='User' AND DisplayName = '" & _ 
ExcelRow & "'" 
Set objRecordSet      = objCommand.Execute 


If Not objRecordSet.EOF Then 

    strDN = objRecordSet.Fields("distinguishedName").Value 


' ########################################################### 
' ########################################################### 

' This is where the script does 'its thing' ... 
' gets what you want. 
' ------------------------------------------------ 


Set MyUser = GetObject ("LDAP://" & strDN) 


objExcel.Cells(intRow, 3).Value = UCASE(MyUser.SamAccountName) 



' ########################################################### 
' ########################################################### 



Else 

    Wscript.Echo "User Not Found: " & ExcelRow 

End If 

Err.Clear 

End Sub 

回答

0

如果发现了多个账户,该记录集将有多个记录,你会通过它需要循环。您的代码目前仅获取记录集中的第一项。

变化If Not objRecordSet.EOF ThenDo While Not objRecordSet.EOF
然后

strDN = objRecordSet.Fields("distinguishedName").Value 
' ########################################################### 
' ########################################################### 
Set MyUser = GetObject ("LDAP://" & strDN) 

当插入用户到电子表格中,你要动态地控制细胞的位置,以便在每个循环相同的细胞不被写入过。

objExcel.Cells(intRow, 3).Value = UCASE(MyUser.SamAccountName) 

在处理该用户的最后,您将使用它来移动到记录集

objRecordSet.MoveNext 

下一个对象(用户),然后代替End If,您将使用Loop

编辑:

此外,而不是连接到使用Set MyUser = GetObject(etc)对象,可你只需要使用"SELECT sAMAccountName FROM...哟你的查询然后strsAMAccountName = objRecordSet.Fields("sAMAccountName")保存一些内存/时间?

编辑2:
我正在做我的脚本。

If objRecordSet.RecordCount = 0 Then 
    'Things to do if not found 
    Exit Sub 'Then exit before entering loop 
End If 

此外,如果用户没有找到,那么objRecordSet.EOF将等于True

+0

谢谢队友,我可以尝试,但我怎么会得到'其他'的声明incase'用户'不存在,如果我循环它? –

+0

编辑原帖 – langstrom