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 Thenに変更Do 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 に答える