msadexchman said:
I've just been given an Excel spreadsheet from our HR department with the
request to populate all the fields in the spreadsheet to the respective
users object in Active Directory. There are fields such as:
Title
First Name
Last Name
Street
Department
Manager
Is it possible to update the user accounts in AD from this? I don't see a
column for "username" in the spreadsheet?
Hi,
Assuming the "First Name" is the value of the givenName attribute and "Last
Name" is the value of the sn attribute, you have a problem. None of the
values are mandatory, much less required to be unique. You have no way to
reliably identify the users.
Assuming that the combination of "First Name" and "Last Name" is intended to
uniquely identify the users, the first step is to determine if you are lucky
enough for this to work in your environment. You can use ADO to retrieve the
user(s) with the specified combinations of names. If there is just one
match, retrieve the distinguishedName and put that in a new spreadsheet
(with the other values) that can be used later to update the remaining
attributes. In brief (not tested):
=========================
Set adoConnection = CreateObject("ADODB.Connection")
Set adoCommand = CreateObject("ADODB.Command")
adoConnection.Provider = "ADsDSOObject"
adoConnection.Open "Active Directory Provider"
adoCommand.ActiveConnection = objConnection
objCommand.Properties("Page Size") = 100
objCommand.Properties("Timeout") = 30
objCommand.Properties("Cache Results") = False
strAttributes = "distinguishedName"
strBase = "<LDAP://dc=MyDomain,dc=com>"
intRow = 1
Do While (objSheet1.Cells(intRow, 1).Value <> "")
strFirstName = objSheet1.Cells(intRow, 2).Value
strLastName = objSheet1.Cells(intRow, 3).Value
' Filter on users with specified first and last name.
strFilter = "(&(objectCategory=person)(objectClass=user)" _
& "(givenName=" & strFirstName & ")(sn=" & strLastName & "))"
adoCommand.CommandText = strBase & ";" & strFilter _
& ";" & strAttributes & ";subtree"
Set adoRecordset = objCommand.Execute
If (adoRecordset.EOF = True) Then
' No user found.
objSheet2.Cells(intRow, 7).Value = "User Not Found"
Else
blnFirst = True
Do Until adoRecordset.EOF
strDN = adoRecordset.Fields("distinguishedName").Value
If (blnFirst = True) Then
' This is the first user found with the names.
objSheet.Cells(intRow, 7).Value = strDN
strTitle = objSheet1.Cells(intRow, 1).Value
strStreet = objSheet1.Cells(intRow, 4).Value
strDepartment = objSheet1.Cells(intRow, 5).Value
strManager = objSheet1.Cells(intRow, 6).Value
objSheet2.Cells(intRow, 1).Value = strTitle
objSheet2.Cells(intRow, 4).Value = strStreet
objSheet2.Cells(intRow, 5).Value = strDepartment
objSheet2.Cells(intRow, 6).Value = strManager
blnFirst = False
Else
' This is a duplicate user with the same names.
objSheet2.Cells(intRow, 7).Value = "More than one user
found"
End If
adoRecordset.MoveNext
Loop
End If
intRow = intRow + 1
Loop
============
objSheet1 and objSheet2 must be bound appropriately earlier in the code.
I've assumed column numbers for the values in the spreadsheet. The new
spreadsheet (objSheet2) has a new column (# 7) with the DN of the user if
there is one match. Nothing above tested, but hopefully you get the idea.
Another program would be used to read the new spreadsheet, bind to the users
with the DN, and set the values.
Of course, the above assumes that the givenName and sn attributes are
populated. If instead you must use the displayName or cn attribute of the
user objects, the idea would be the same, but you would construct the
proposed displayName or cn from the specified first and last names. It might
be more difficult to get a match unless your names are standardized. Maybe:
strFilter = "(&(objectCategory=person)(objectClass=user)" _
& "(cn=" & strFirstName & " " & strLastName & "))"