简体   繁体   English

VBScript \\ Active Directory通过显示名称搜索并收到相同的2个

[英]VBScript \ Active Directory Searched by displayname and received 2 of the same

I have my script to search by displayname and return the userid, which works fine. 我的脚本可以按显示名称搜索并返回用户ID,效果很好。

but when I encounter a displayname that has 2 entries in AD ie 但是当我遇到一个在AD中有2个条目的显示名称时

  1. pavle stojanovic - he is from company 1 pavle stojanovic-他来自公司1
  2. pavle stojanovic - he is from company 2 pavle stojanovic-他来自公司2

the userid doesnt get displayed because the script doesnt know what to do ? 由于脚本不知道该怎么办,所以不会显示userid。

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. 如果我得到2或更大的回报,我想在输出中说,嘿,我两次找到相同的名字,等等。这是两者的用户名和公司。

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

If multiple accounts are found, the Record Set will have multiple records and you'll need to loop through it. 如果找到多个帐户,则记录集将具有多个记录,您需要遍历它。 Your code currently only gets the first item in the Record Set. 您的代码当前仅获得记录集中的第一项。

Change If Not objRecordSet.EOF Then to Do While Not objRecordSet.EOF If Not objRecordSet.EOF Then更改为Do While Not objRecordSet.EOF
Then 然后

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

When inserting the users into the spreadsheet, you'll want to control the placement of the cell dynamically so the same cell isn't written over at each loop. 将用户插入电子表格时,您将需要动态控制单元格的位置,以免在每次循环时都覆盖同一单元格。

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

At the end of processing this user, you'll use this to move to the next object (user) in the Record Set 处理该用户结束时,将使用它移动到记录集中的下一个对象(用户)

objRecordSet.MoveNext

Then instead of End If , you'll use Loop 然后,您将使用Loop而不是End If

EDIT: 编辑:

Also, instead of connecting to the object using Set MyUser = GetObject(etc) , could you just use "SELECT sAMAccountName FROM... in your query then strsAMAccountName = objRecordSet.Fields("sAMAccountName") to save some memory/time? 另外,不是使用Set MyUser = GetObject(etc)连接到对象,您是否可以在查询中使用"SELECT sAMAccountName FROM... ,然后使用strsAMAccountName = objRecordSet.Fields("sAMAccountName")节省一些内存/时间?

Edit2: EDIT2:
I am doing this in my script. 我正在我的脚本中执行此操作。

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

Also, if the user isn't found then objRecordSet.EOF will equal True . 另外,如果找不到用户,则objRecordSet.EOF将等于True

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM