Greetings:
I want to rename users in bulk using a CSV. I found some code that works for one user.
===START===strParentDN = "ou=Sales,dc=2zero,dc=local" ' e.g.
strUserOldName = "BWagner" ' e.g. jsmithstrUserNewName = "Beatriz.Wagner" ' e.g. jimstrEmailAddress = "Beatriz.Wagner@2zero.net"
Set objCont = GetObject("LDAP://" & strParentDN)WScript.Echo strUserOldName & "," & strParentDN, "cn=" & strUserNewNameobjCont.MoveHere "LDAP://cn=" & strUserOldName & "," & strParentDN, "cn=" & strUserNewNameset objUser = GetObject("LDAP://cn=" & strUserNewName & "," & strParentDN)objUser.Put "sAMAccountName", strUserNewNameobjUser.Put "userPrincipalName", strUserNewName & "@2zero.local"objUser.Put "mail", strEmailAddressobjUser.SetInfoWScript.Echo "Rename successful"===END===
So I put this into a routine that should work and, for the most part, almost does. The problem is that it does not change the sam account name or the prefix of the UPN.
I am using a CSV with three fields. The old account, the new account and the email address. My code looks like this:
===START===
Option Explicit
Dim sCSVFileLocationDim sCSVFileDim oConnectionDim oRecordSetDim objCont
' Variables needed for LDAP connectionDim oRootLDAP, oParentDN, oDomain
' Holding variables for information import from CSV fileDim strEmailAddrDim strUserOldNameDim strUserNewName
' Modify this to match your ADoDomain="2zerotest.local"
' Input file locationsCSVFileLocation = "C:FNameLName"
' Input file namesCSVFile = sCSVFileLocation&"fname_lname_modesto.csv"
' Commands used to open the CSV file and select all of the recordsset oConnection = createobject("adodb.connection")set oRecordSet = createobject("adodb.recordset")oConnection.open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source= " & sCSVFileLocation & ";Extended Properties=""text;HDR=NO;FMT=Delimited"""oRecordSet.open "SELECT * FROM " & sCSVFile ,oConnection
' Create a connection to the Active Directory Set oRootLDAP = GetObject("LDAP://rootDSE")oParentDN = "ou=Sales,dc=2zerotest,dc=local" Set objCont = GetObject("LDAP://" & oParentDN)
on error resume next
Do Until oRecordSet.EOF ' Reads the values (cells) in the sInputFile file.
strUserOldName = oRecordSet.Fields.Item(0).Value strUserNewName = oRecordSet.Fields.Item(1).Value strEmailAddr = oRecordSet.Fields.Item(2).Value
' WScript.Echo strUserOldName & "," & oParentDN, "cn=" & strUserNewName objCont.MoveHere "LDAP://cn=" & strUserOldName & "," & oParentDN, "cn=" & strUserNewName' objUser.SetInfo set objUser = GetObject("LDAP://cn=" & strUserNewName & "," & oParentDN) objUser.Put "sAMAccountName",strUserNewName objUser.Put "userPrincipalName",strUserNewName & "@2zero.local" objUser.Put "name",strUserNewName' objUser.Put "name",lcase(sLogon) objUser.Put "mail",strEmailAddr objUser.SetInfo' WScript.Echo "Rename successful"
oRecordSet.MoveNext()Loop
===END===
Bulk User Add
Forum rules
Do not post any licensing information in this forum.
Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
Do not post any licensing information in this forum.
Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
- mmacdonald
- Posts: 44
- Last visit: Thu Apr 07, 2016 7:53 am
Bulk User Add
Greetings:
I want to rename users in bulk using a CSV. I found some code that works for one user.
===START===strParentDN = "ou=Sales,dc=2zero,dc=local" ' e.g.
strUserOldName = "BWagner" ' e.g. jsmithstrUserNewName = "Beatriz.Wagner" ' e.g. jimstrEmailAddress = "Beatriz.Wagner@2zero.net"
Set objCont = GetObject("LDAP://" & strParentDN)WScript.Echo strUserOldName & "," & strParentDN, "cn=" & strUserNewNameobjCont.MoveHere "LDAP://cn=" & strUserOldName & "," & strParentDN, "cn=" & strUserNewNameset objUser = GetObject("LDAP://cn=" & strUserNewName & "," & strParentDN)objUser.Put "sAMAccountName", strUserNewNameobjUser.Put "userPrincipalName", strUserNewName & "@2zero.local"objUser.Put "mail", strEmailAddressobjUser.SetInfoWScript.Echo "Rename successful"===END===
So I put this into a routine that should work and, for the most part, almost does. The problem is that it does not change the sam account name or the prefix of the UPN.
I am using a CSV with three fields. The old account, the new account and the email address. My code looks like this:
===START===
Option Explicit
Dim sCSVFileLocationDim sCSVFileDim oConnectionDim oRecordSetDim objCont
' Variables needed for LDAP connectionDim oRootLDAP, oParentDN, oDomain
' Holding variables for information import from CSV fileDim strEmailAddrDim strUserOldNameDim strUserNewName
' Modify this to match your ADoDomain="2zerotest.local"
' Input file locationsCSVFileLocation = "C:FNameLName"
' Input file namesCSVFile = sCSVFileLocation&"fname_lname_modesto.csv"
' Commands used to open the CSV file and select all of the recordsset oConnection = createobject("adodb.connection")set oRecordSet = createobject("adodb.recordset")oConnection.open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source= " & sCSVFileLocation & ";Extended Properties=""text;HDR=NO;FMT=Delimited"""oRecordSet.open "SELECT * FROM " & sCSVFile ,oConnection
' Create a connection to the Active Directory Set oRootLDAP = GetObject("LDAP://rootDSE")oParentDN = "ou=Sales,dc=2zerotest,dc=local" Set objCont = GetObject("LDAP://" & oParentDN)
on error resume next
Do Until oRecordSet.EOF ' Reads the values (cells) in the sInputFile file.
strUserOldName = oRecordSet.Fields.Item(0).Value strUserNewName = oRecordSet.Fields.Item(1).Value strEmailAddr = oRecordSet.Fields.Item(2).Value
' WScript.Echo strUserOldName & "," & oParentDN, "cn=" & strUserNewName objCont.MoveHere "LDAP://cn=" & strUserOldName & "," & oParentDN, "cn=" & strUserNewName' objUser.SetInfo set objUser = GetObject("LDAP://cn=" & strUserNewName & "," & oParentDN) objUser.Put "sAMAccountName",strUserNewName objUser.Put "userPrincipalName",strUserNewName & "@2zero.local" objUser.Put "name",strUserNewName' objUser.Put "name",lcase(sLogon) objUser.Put "mail",strEmailAddr objUser.SetInfo' WScript.Echo "Rename successful"
oRecordSet.MoveNext()Loop
===END===
I want to rename users in bulk using a CSV. I found some code that works for one user.
===START===strParentDN = "ou=Sales,dc=2zero,dc=local" ' e.g.
strUserOldName = "BWagner" ' e.g. jsmithstrUserNewName = "Beatriz.Wagner" ' e.g. jimstrEmailAddress = "Beatriz.Wagner@2zero.net"
Set objCont = GetObject("LDAP://" & strParentDN)WScript.Echo strUserOldName & "," & strParentDN, "cn=" & strUserNewNameobjCont.MoveHere "LDAP://cn=" & strUserOldName & "," & strParentDN, "cn=" & strUserNewNameset objUser = GetObject("LDAP://cn=" & strUserNewName & "," & strParentDN)objUser.Put "sAMAccountName", strUserNewNameobjUser.Put "userPrincipalName", strUserNewName & "@2zero.local"objUser.Put "mail", strEmailAddressobjUser.SetInfoWScript.Echo "Rename successful"===END===
So I put this into a routine that should work and, for the most part, almost does. The problem is that it does not change the sam account name or the prefix of the UPN.
I am using a CSV with three fields. The old account, the new account and the email address. My code looks like this:
===START===
Option Explicit
Dim sCSVFileLocationDim sCSVFileDim oConnectionDim oRecordSetDim objCont
' Variables needed for LDAP connectionDim oRootLDAP, oParentDN, oDomain
' Holding variables for information import from CSV fileDim strEmailAddrDim strUserOldNameDim strUserNewName
' Modify this to match your ADoDomain="2zerotest.local"
' Input file locationsCSVFileLocation = "C:FNameLName"
' Input file namesCSVFile = sCSVFileLocation&"fname_lname_modesto.csv"
' Commands used to open the CSV file and select all of the recordsset oConnection = createobject("adodb.connection")set oRecordSet = createobject("adodb.recordset")oConnection.open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source= " & sCSVFileLocation & ";Extended Properties=""text;HDR=NO;FMT=Delimited"""oRecordSet.open "SELECT * FROM " & sCSVFile ,oConnection
' Create a connection to the Active Directory Set oRootLDAP = GetObject("LDAP://rootDSE")oParentDN = "ou=Sales,dc=2zerotest,dc=local" Set objCont = GetObject("LDAP://" & oParentDN)
on error resume next
Do Until oRecordSet.EOF ' Reads the values (cells) in the sInputFile file.
strUserOldName = oRecordSet.Fields.Item(0).Value strUserNewName = oRecordSet.Fields.Item(1).Value strEmailAddr = oRecordSet.Fields.Item(2).Value
' WScript.Echo strUserOldName & "," & oParentDN, "cn=" & strUserNewName objCont.MoveHere "LDAP://cn=" & strUserOldName & "," & oParentDN, "cn=" & strUserNewName' objUser.SetInfo set objUser = GetObject("LDAP://cn=" & strUserNewName & "," & oParentDN) objUser.Put "sAMAccountName",strUserNewName objUser.Put "userPrincipalName",strUserNewName & "@2zero.local" objUser.Put "name",strUserNewName' objUser.Put "name",lcase(sLogon) objUser.Put "mail",strEmailAddr objUser.SetInfo' WScript.Echo "Rename successful"
oRecordSet.MoveNext()Loop
===END===
Bulk User Add
Let's focus on the first block of code. If that doesn't do what you want for a single user, there's no reason to worry about the bulk changes yet. I don't see why that code shouldn't change the SAMAccountname. I've used code like it with no problems. Do you have permissions to change these properties? Do you get any errors? If you are using On Error Resume Next, comment it out.
Bulk User Add
I tried your code and it changed everything without error. I think we have to know if you are getting any errors.
- mmacdonald
- Posts: 44
- Last visit: Thu Apr 07, 2016 7:53 am
Bulk User Add
I get an error saying 'the server is unwilling to process your request' when I try to update the user object - objUser.SetInfo. If I remove the line to update the UPN and the name, the script completes but does not update the SAM account name.
Bulk User Add
Usually the "server is unwilling to process your request" is because your passing the wrong format (i.e. your passing cn=Jeff Smith,dc=dom,dc=com and don't have the "LDAP://" in the string) or your trying to write to a "read-only" field, such as the NAME. If your trying to rename a existing user account, you have to use a method like:
Set cont = GetObject("LDAP://dc=dom,dc=com")Set newobj = cont.MoveHere("LDAP://cn=Jeff Smith,dc=dom,dc=com", "cn=Denise Smith")
http://msdn.microsoft.com/en-us/library ... S.85).aspx
Set cont = GetObject("LDAP://dc=dom,dc=com")Set newobj = cont.MoveHere("LDAP://cn=Jeff Smith,dc=dom,dc=com", "cn=Denise Smith")
http://msdn.microsoft.com/en-us/library ... S.85).aspx
- mmacdonald
- Posts: 44
- Last visit: Thu Apr 07, 2016 7:53 am
Bulk User Add
I am trying to rename the account. I found that if I did not specify the SAM account name in my code I did not get the results I wanted. Things seem to be working now with a few small changes. I always find one small sucess leads to new failures. Still working on things.