VB.NET - Updating users in AD based on Excel Sheet
Ok, so I have this code that retreives attributes of AD users from a certain OU and loads the info into an Excel sheet. This is working as it should. However, I would like to know how I can alter my code to be able to edit the Excel sheet and use that to update the users from which it came from.
I know this is VB.NET and I do know there are ways of connecting to AD using .NET framework. I plan on using this wrapper http://www.codeproject.com/KB/system/active_directory_in_vbnet.aspx at a later dat开发者_StackOverflow社区e. But for now, I just want this to work.
This is my code right now. I know it's probably sloppy and could have been done better. I will use the wrapper mentioned above as soon as this first version of the tool is completed.
Public Sub ExportADUsers()
Dim i As Integer
Dim strfilter, strAttributes, strScope
strfilter = "(&(objectCategory=Person)(objectClass=User))"
strAttributes = "givenName, sn, displayName, mail," & _
"initials, description, company, title, department," & _
"location, telephoneNumber, mobile," & _
"physicalDeliveryOfficeName, streetAddress," & _
"l, st, postalCode, c, info"
strScope = "subtree"
Dim cn, cmd, rs
cn = CreateObject("ADODB.Connection")
cmd = CreateObject("ADODB.Command")
cn.open("Provider=ADsDSOObject;")
cmd.ActiveConnection = cn
cmd.commandtext = "LDAP://OU=MyOU,OU=Groups,DC=myDomain,DC=local>;" & strfilter & ";" & strAttributes & ";" & strScope
rs = cmd.EXECUTE
Dim objExcel, objWB, objSheet
objExcel = CreateObject("Excel.Application")
objWB = objExcel.Workbooks.Add
objSheet = objWB.Worksheets(1)
For i = 0 To rs.Fields.Count - 1
objSheet.Cells(1, i + 1).Value = rs.Fields(i).Name
objSheet.Cells(1, i + 1).Font.Bold = True
Next
objSheet.Range("A2").CopyFromRecordset(rs)
objSheet.Cells.EntireColumn.AutoFit()
objSheet.Cells.EntireRow.AutoFit()
objSheet.SaveAs("C:\Sheet.xlsx")
'Clean up
rs.Close()
cn.Close()
objSheet = Nothing
objExcel.Quit()
objExcel = Nothing
End Sub
So, can I use this code as a basis for doing it the other way around? Actually updating the same set of users with the same Excel sheet that was generated?
Edit: Edited the code and removed unecessary bits
精彩评论