Add a single user or multiple users to AD with a VB Script.

This is a VB Script that I have created to fit a scenario where you need to add a user to Active Directory and set attributes for that user. I have found it to be quite tedious to have to create each user and set the attributes manually. So I have searched the net to find information on how to create a script to do what I need. Here is my scenario…

I need to create a user, set a password, set a location, create a user home folder(not roaming) and set the share permissions. I wanted to be able to have the option to create multiple users at once and to be able to create a single user using dialog boxes.

To start you have to download RMTShare from http://ss64.com/nt/rmtshare.html .  This is used to set the permissions on the share after it is created on the server. Make sure that the script knows how to get to rmtshare.exe.

The script is not too complicated. There is a sub procedure called User and inside it is an “if” that determines whether you will be using a .csv file to create users or if you are creating a single user. The CSV file must be in the correct order: Firstname(given name), lastname(sirname), predetermined username(SamAccountName) and in my situation, grade. If you create a single user, the script will ask you all of the above information using an Input Box.

Now this script can be manipulated to make it fit your situation. For instance you have a AD scheme where you have OU’s for locations or departments. In my situation, there users in a school building and in each building there are different grades. The OU’s represent that hiearchy. There is a function called getSchool and getGradYear. These two functions together determine the OU of a user. Those functions set a global variable at each function return and are used in the sub, User.

The next step is to create the user and set attributes. That is pretty self explanatory and you can google more attributes to sets if need be. The last three functions are for the share and are not too complicated. I always run this script from the server that is sharing the folders.

 

Option Explicit
'***** Global Vars *******
dim objUser, objGroup, objContainer, strDirectory, errReturn
 
Const FILE_SHARE = 0
Const MAXIMUM_CONNECTIONS = 0
'***** USER VARS *********
dim strSamName, strGivenName, strSn, strCn, strDn, strL
'***** LDAP VARS *********
dim ldapPath
'***** VAR FOR SETTING OU *******
dim gradYear, school
 
'***** USE CSV FILE OR TEXT BOX (T OR F) IF FALSE WILL USE TEXT BOX *****
dim source
source = InputBox("Use CSV file?   (Answer T for yes or F to use Text box to add one user)","Source Type",,100,200)
'wscript.echo source
User
 
sub User()
if source = "t" then
    'wscript.echo "Use CSV"
    Const ADS_PROPERTY_APPEND = 3
    dim objExcel, objWorkbook, intRow
    Set objExcel = CreateObject("Excel.Application")
    Set objWorkbook = objExcel.Workbooks.Open("Path to csv File here")
    intRow = 1
    Do Until objExcel.Cells(intRow,1).Value = ""
        'USER BELOW FOR CREATE USER, SET USER ATTRIBUTES AND CREATE A SHARE ON A SERVER FOR USER HOMEFOLDER
        'THESE VARIABLES ARE SET FROM THE CSV FILE THE NUMBER INDICATES THE COLUMN NUMBER IN THE CSV FILE. THE CSV FILE CAN BE IN ALPHA OR NUMERIC COLUMN NAME
        'MODE, BUT IN THIS SCRIPT IT HAS TO BE NUMERIC EX. "A"(IN CSV FILE)= "1"(IN SCRIPT)
        strSn = (objExcel.Cells(intRow,2).Value)
        strGivenName = (objExcel.Cells(intRow,1).Value)
        strSamName = (objExcel.Cells(intRow,3).Value)
        strCn = strGivenName & " " & strSn
        getGradYear(CInt((objExcel.Cells(intRow,4).Value)))
        getSchool(CInt((objExcel.Cells(intRow,4).Value)))
 
        ' SETS VARIABLES TO CREATE THE USER
        ldapPath = "LDAP://CN=" & "" & strCn & "" & ",OU=" & "" & gradYear & "" & ",OU=" & "" & school & "" & ",OU=Students,DC=Domain,DC=local"
        strDn = "CN=" & "" & strCn & "" & ",OU=" & "" & gradYear & "" & ",OU=" & "" & school & "" & ",OU=Students,DC=DOMAin,DC=local"
        strConPath = "LDAP://OU=" & "" & gradYear & "" & ",OU=" & "" & school & "" & ",OU=Students,DC=domain,DC=local"
 
        Set objContainer = GetObject("" & strConPath & "")
        Set objUser = objContainer.Create("User","cn=" & strCn)
        objUser.Put "sAMAccountName", ""&strSamName&""
        objUser.SetInfo
        objUser.Put "givenName", ""&strGivenName&""
        objUser.Put "sn", ""&strSn&""
        objUser.Put "userPrincipalName", ""&strSamName&"" & "@domain.local"
        objUser.Put "scriptPath", "general.bat"
        objUser.AccountDisabled = FALSE
        objUser.Put "pwdLastSet", "0"
        objUser.SetPassword "welcome"
        objUser.Put "wWWHomePage", "www.home.org"
        objUser.Put "l", "city"
        objUser.SetInfo
        Set objGroup = GetObject("LDAP://cn=groupname,dc=domain,dc=local")
        objGroup.PutEx ADS_PROPERTY_APPEND, "member", Array(""&strDn&"")
        objGroup.SetInfo
 
        'calls the functions to create folder for the users home folder and then shares and sets permissions. the share is hidden
        FolderCreate
        CreateShare
        SetPermissions
        'sets user ad attribute for the homefolder this must be done after the folder is created.
        objUser.Put "homeDirectory", "\\servername\" & strSamName & "$"
        objUser.Put "homeDrive", "U"
        objUser.SetInfo
        intRow = intRow + 1
    Loop
    objExcel.Quit
else
    'this part of the script is the same except that the values that were gotten from the excel file are gotten from text box entries.
    dim tGradYear, strConPath
    strSamName = InputBox("Enter Student ID","Enter Student ID",,100,200)
    strGivenName = InputBox("Enter Student First Name","Enter Student First Name",,100,200)
    strSn = InputBox("Enter Student Last Name","Enter Student Last Name",,100,200)
    strCn = strGivenName & " " & strSn
    tGradYear = InputBox("Enter Grade","Enter Grade",,100,200)
    getGradYear(CInt(tGradYear))
    getSchool(CInt(tGradYear))
    ldapPath = "LDAP://CN=" & "" & strCn & "" & ",OU=" & "" & gradYear & "" & ",OU=" & "" & school & "" & ",OU=Students,DC=domain,DC=local"
    strDn = "CN=" & "" & strCn & "" & ",OU=" & "" & gradYear & "" & ",OU=" & "" & school & "" & ",OU=Students,DC=domain,DC=local"
    strConPath = "LDAP://OU=" & "" & gradYear & "" & ",OU=" & "" & school & "" & ",OU=Students,DC=domain,DC=local"
    Set objContainer = GetObject("" & strConPath & "")
    Set objUser = objContainer.Create("User","cn=" & strCn)
    objUser.Put "sAMAccountName", ""&strSamName&""
    objUser.SetInfo
    objUser.Put "givenName", ""&strGivenName&""
    objUser.Put "sn", ""&strSn&""
    objUser.Put "userPrincipalName", ""&strSamName&"" & "@domain.local"
    objUser.Put "scriptPath", "general.bat"
    objUser.AccountDisabled = FALSE
    objUser.Put "pwdLastSet", "0"
    objUser.SetPassword "welcome"
    objUser.Put "wWWHomePage", "www.home.org"
    objUser.Put "l", "LocalCity"
    objUser.SetInfo
    Set objGroup = GetObject("LDAP://cn=groupname,dc=domain,dc=local")
    objGroup.PutEx ADS_PROPERTY_APPEND, "member", Array(""&strDn&"")
    objGroup.SetInfo
 
    FolderCreate
    CreateShare
    SetPermissions
 
    objUser.Put "homeDirectory", "\\servername\" & strSamName & "$"
    objUser.Put "homeDrive", "U"
    objUser.SetInfo
end if
 
wscript.echo "DONE"
 
end sub
 
Sub FolderCreate ()
    ' Get Folder name
    dim objFSO, objFolder
    strDirectory = "f:\StudentShares\"& gradYear & "\" & strSamName
    ' Create the File System Object
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    ' Note If..Exists. Then, Else ... End If construction
    If objFSO.FolderExists(strDirectory) Then
        Set objFolder = objFSO.GetFolder(strDirectory)
    Else
        Set objFolder = objFSO.CreateFolder(strDirectory)
    End If
End Sub
 
Sub CreateShare()
    dim strComputer
    strComputer = "."
    dim objWMIService, objNewShare
    Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
    Set objNewShare = objWMIService.Get("Win32_Share")
    errReturn = objNewShare.Create (strDirectory, strSamName & "$", FILE_SHARE, ,"Student Share" )
End Sub
 
Sub SetPermissions()
    dim WSHShell
    dim domadmin, stuU
    domadmin = """domain\domain admins"""
    stuU = """domain\studentudrive"""
    Set WSHShell = CreateObject("Wscript.Shell")
    WSHShell.Run "c:\sanmove\rmtshare\rmtshare.exe \\servername\" & strSamName & "$ /grant domain\" & strSamName & ":f" & "/grant " & stuU & ":f" & "/grant " & domadmin & ":f", 0, FALSE
    WSHShell.Run "c:\sanmove\rmtshare\rmtshare.exe \\servername\" & strSamName & "$ /remove everyone:f", 0, FALSE
End Sub
 
'this function gets the graduation year of the student based on their current grade
Function getGradYear(cGrade)
    if cGrade=6 then
        if Year(Date) Mod 2 = 0 then '1 means odd, 0 means even
            gradYear = Year(Date)+ 6
        else
            gradYear = Year(Date) + 7
        end if
    end if
    if cGrade=7 then
        if Year(Date) Mod 2 = 0 then '1 means odd, 0 means even
            gradYear = Year(Date)+ 5
        else
            gradYear = Year(Date) + 6
        end if
    end if
    if cGrade=8 then
        if Year(Date) Mod 2 = 0 then '1 means odd, 0 means even
            gradYear = Year(Date)+ 4
        else
            gradYear = Year(Date) + 5
        end if
    end if
    if cGrade=9 then
        if Year(Date) Mod 2 = 0 then '1 means odd, 0 means even
            gradYear = Year(Date)+ 3
        else
            gradYear = Year(Date) + 4
        end if
    end if
    if cGrade=10 then
        if Year(Date) Mod 2 = 0 then '1 means odd, 0 means even
            gradYear = Year(Date)+ 2
        else
            gradYear = Year(Date) + 3
        end if
    end if
    if cGrade=11 then
        if Year(Date) Mod 2 = 0 then '1 means odd, 0 means even
            gradYear = Year(Date)+ 1
        else
            gradYear = Year(Date) + 2
        end if
    end if
    if cGrade=12 then
        if Year(Date) Mod 2 = 0 then '1 means odd, 0 means even
            gradYear = Year(Date)+ 0
        else
            gradYear = Year(Date) + 1
        end if
    end if
end Function
 
'this function returns the OU name that the student will go in based on their grade.
Function getSchool(cGrade)
    if cGrade <= 8 and cGrade >= 6 then
        school = "MSStudents"
    elseif cGrade <= 12 and cGrade > 8 then
        school = "HSStudents"
    else
        school = "Wrong Grade"
    end if
end Function
This entry was posted in VBS and tagged , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *