Wednesday, July 13, 2011

Adding First and Last names to Active Directory

I know that my first blog being about VB scripting will shock some of you as I am a diehard SharePoint fan, but none the less this was cool. I wrote my first VB script the other day that import over 430 first and last names into Active Directory from a .csv file. It was awesome to see it work after hours or research and testing. I know that most of you are saying, big whoop I do this in my sleep, but for me it was a real eye opener. I hope that this helps someone else in the future. Anyways, here is what I did and what I learned....

I needed a quick way to import 430 first and last names into Active Directory. I knew that DSMOD would be the command that I would use to add them, but how to automate this so I didn’t have to do it by hand? VB Script.

Step #1: I need to declare all the variables in the top of the script. I have been told to put them up here is no necessary, just considered good “coding.” Here are the variables that I declared:

dim swnum

dim swUser

dim swFirst

dim swLast

dim i

Set oShell = CreateObject(“”)

Set oNet = CreateObject(“Wscript.Network”)

Set oFSO = CreateObject(“Scripting.FileSystemObject”)

f = 500

g = 0

i = 0

Step 1(b): In addition to declaring the variable, we need to define the path to the .csv file. This is how I did it:

BSConfig = “\\ServerUNC\Share\FileName.csv”

Step 1(c): Now that we have defined the configuration file, we have to declare the variable that will call the configuration file. (confused yet?)

BSinfo = oFSO.OpenTextFile(BSConfig,ForReading,True)

Step #2: I need to write a sub-routine that would open the text file and query the .csv columns and push the values to the variables that I could use in the DSMOD command line. Here is the sub-routine:

Sub getinfo()

Do While BSinfo.AtEndOfStream <> True

Line = Bsinfo.ReadLine

LineArray = Split(Line,“,”,-1,1)

i = i + 1 <- This was added to up the count each time to read different lines on the .csv file ->

swnum = LineArray(0) <- The LineArray(0) are the columns in the .csv files ->

swUser = LineArray(1)

swFirst = LineArray(2)

swLast = LineArray(3)

Exit Do

Wscript.sleep “5000”


If i=500 then <- This will stop the script at the end. Change the 500 to a number after ->

Wscript.exit <- the last row in your .csv file ->

end if

Step #3: I need to write another sub-routine that would execute the DSMOD command. This was the tricky one.

Sub Process ()

Do While g"less than"f

getinfo <- this calls the sub-routine “getinfo” ->

if swUser = “END” then

exit do

end if “cmd /c start dsmod user “”CN=” & swUser & “,OU=Users,DC=Microsoft,DC=com”” –fn “ & swFirst & “ –ln “ & swLast

if g = 500 then


end if

g = g + 1


Wscript.sleep “1000”

End Sub

· First, I had to get the EXACT command line with all the right switches for the DSMOD.

· Second, I had to use the “CMD start” line to call the CMD Prompt to execute the DSMOD command. This is the “” line.

· Third, I had to insert the variables from the sub-routine “getinfo” into the command line (this is where the rubber meets the road). Look for the variables in the DSMOD command line.

Step #4: I had to call the sub-routine that I wanted to run as the script. This is the part of the whole script that makes it work.

Process <- This will call the sub-routine “Process” ->

Let’s review; I had to declare all my variables (at the top for “good coding”), then I wrote my sub-routines, after that I had the VB script run “process” which kicks off the whole script. Sit back and watch the fireworks. I have posted the entire script in the “Downloads” link below.

This opened a whole new world for me. I am going to attempt to script everything now.


No comments:

Post a Comment