1

I have my script to search by displayname and return the userid, which works fine.

but when I encounter a displayname that has 2 entries in AD i.e.

  1. pavle stojanovic - he is from company 1
  2. pavle stojanovic - he is from company 2

the userid doesnt get displayed because the script doesnt know what to do ?

how do i over come this ? if I get a return of 2 or more I'd like to say in the output hey i found the same name twice etc.. here are the userids and companies for both.

If you want to see the script its below...

 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
4

1 回答 1

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")节省一些内存/时间吗?

Edit2:
我在我的脚本中这样做。

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

此外,如果未找到用户,objRecordSet.EOF则将等于True.

于 2013-10-22T22:03:01.683 回答