Bulk User Add

Anything VBScript-related, including Windows Script Host, WMI, ADSI, and more.
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.
This topic is 15 years and 1 month old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
mmacdonald
Posts: 44
Last visit: Thu Apr 07, 2016 7:53 am

Bulk User Add

Post by mmacdonald »

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===


User avatar
mmacdonald
Posts: 44
Last visit: Thu Apr 07, 2016 7:53 am

Bulk User Add

Post by mmacdonald »

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===


User avatar
jhicks
Posts: 1789
Last visit: Mon Oct 19, 2015 9:21 am

Bulk User Add

Post by jhicks »

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.
User avatar
jhicks
Posts: 1789
Last visit: Mon Oct 19, 2015 9:21 am

Bulk User Add

Post by jhicks »

I tried your code and it changed everything without error. I think we have to know if you are getting any errors.
User avatar
mmacdonald
Posts: 44
Last visit: Thu Apr 07, 2016 7:53 am

Bulk User Add

Post by mmacdonald »

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.
User avatar
rasimmer
Posts: 182
Last visit: Fri Apr 25, 2014 7:00 am

Bulk User Add

Post by rasimmer »

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
User avatar
mmacdonald
Posts: 44
Last visit: Thu Apr 07, 2016 7:53 am

Bulk User Add

Post by mmacdonald »

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.
This topic is 15 years and 1 month old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked