+ Post New Thread
Results 1 to 5 of 5
Windows Thread, Scripting Creation of User Accounts, Shares and permissions... in Technical; Hi In the past I've run networks which have had all sorts of software from say Viglen which have created ...
  1. #1
    jgcracknell's Avatar
    Join Date
    Mar 2009
    Thank Post
    Thanked 1 Time in 1 Post
    Rep Power

    Scripting Creation of User Accounts, Shares and permissions...


    In the past I've run networks which have had all sorts of software from say Viglen which have created accounts and permissions/setup automatically.

    What I need is a script (or free util) that creates user accounts (say from an Excel spreadsheet) and creates home folders, shares them out and sets everything up with the correct permissions etc.

    I am using Win 2008 R2



  2. #2

    teejay's Avatar
    Join Date
    Apr 2008
    Thank Post
    Thanked 773 Times in 583 Posts
    Rep Power
    Create a User Account from Information in an Excel Spreadsheet
    If you redirect the documents folder through AD it will create the folder automatically when they login. Never had a need to use Home folders since NT4, redirected documents works fine.

  3. #3
    jgcracknell's Avatar
    Join Date
    Mar 2009
    Thank Post
    Thanked 1 Time in 1 Post
    Rep Power
    Quote Originally Posted by teejay View Post
    Create a User Account from Information in an Excel Spreadsheet
    If you redirect the documents folder through AD it will create the folder automatically when they login. Never had a need to use Home folders since NT4, redirected documents works fine.
    Thanks Teejay - thing is shares have always been used e.g. \\server1\username$ since the inception of this network and isn't something I want to change at the moment. Really want to get next Years 7 in without hassle and then experiment on changing things for the following academic year.

  4. #4

    tmcd35's Avatar
    Join Date
    Jul 2005
    Thank Post
    Thanked 893 Times in 738 Posts
    Blog Entries
    Rep Power
    As ever Wisesoft answers the sysadmins AD prayers - http://www.wisesoft.co.uk/software/a...t/default.aspx

    It's what I use now after years of writing Pascal code to do the job.

  5. #5
    Monty1157's Avatar
    Join Date
    Jun 2010
    Thank Post
    Thanked 3 Times in 2 Posts
    Rep Power
    Hi All,
    From the initial script link above, I managed to create the following VB Script to create a user and home folder from a spreadsheet, fill in all the user details into the account, set the password (as well as setting it to change at first login), and point it to the correct location of the home drive.
    I'm no coder, so please forgive the utterly horrible coding discipline and spelling mistakes. I'm sure someone could probably do a much better job, but it works with very little changes required when switching year/OU/domain and I'm happy with it.
    All variables to be changed are at the top of the code, and are commented to describe what they are. Hopefully you'll find some use out of it. Spreadsheet setup is also in the comments section at the top of the code.

    ' Name: Createuser.vbs																																				'
    ' Author: Monty																																						'		
    ' Original script coded by Fox / The Scripting Community (http://gallery.technet.microsoft.com/ScriptCenter/en-us/bc330a9b-929d-49fd-8c03-ab69b6fc4fd6)				'
    ' Several hundred hacks nicked from all over the interwebs.																											'
    ' Build date: 29/07/2010																																			'
    ' Last ammened by: Monty																																			'
    ' Last ammendment date: 03/08/2010																																	'
    ' Excel Spreadsheet Layout Format: Column headers and Year on first row. Year is a special header in 6th column for description field(e.g. 2010 Student)			'
    ' Headers in column order:																																			'
    ' 1 SAM																																								'
    ' 2 Given Name																																						'
    ' 3 Surname											   																												'
    ' 4 Display Name																																					'
    ' 5 Password																																						'
    Dim oRoot 
    Dim oDomain
    Dim oCurrent
    Dim strExcelFileName 
    Dim oOU 
    Dim OUFind
    Dim count
    Dim createcount
    Dim skipcount
    Dim strCompleteOU
    Dim strStaticDir
    Dim strHomeDir
    Dim strHomeDrive
    Dim strStudentGroup
    Dim strDirectory
    Dim strCommon
    Dim strXcacls
    Dim strRun
    Dim objFolder
    Dim objShell
    Dim filesys
    Dim filetxt
    Dim fileLoc
    Const ForWriting = 2 
    count = 0
    createcount = 0
    skipcount = 0
    OUFind = 0
    ' Variable to hold completed folder path.
    strDirectory = ""
    ' Partially omplete OU string for where the users are being placed. Keep the setup as it currently is. There's no LDAP:// at the front.
    strCompleteOU = "OU=2010 Students,OU=school Students,OU=School Users,OU=School Name,OU=Schools,DC=edu,DC=lan"
    ' Complete OU string for the group that the users are being placed into.
    strStudentGroup = "LDAP://CN=G SchoolStudents,OU=School Groups,OU=School Name,OU=Schools,DC=edu,DC=lan"
    ' Drive letter for the home folder that's entered into the user account on creation.
    strHomeDrive = "W:"' Initial part of the folder structure where the user folders are being placed into.
    ' Actual folder location on the server this is being run from.
    strStaticDir = "D:\Studenthome\2010\"
    ' Initial part of the UNC pathname to the user folder. This is placed into the user account on creation.
    strHomeDir = "\\school-server\studenthome\2010\"
    ' Location of the xcacls.vbs script needed for this script to work. 
    ' Yes it needs three sets of double quotes to work, so that the full path works if there's one or more spaces in there. (e.g "C:\Program Files\XCacls\xcacls.vbs")
    strXcacls = """D:\temp\xcacls.vbs"""
    ' Excel Spreadsheet name (e.g. d:\temp\import.xls)
    strExcelFileName = "d:\temp\matched.xlsx"
    ' Location for output log file. No triple quotes needed here for whatever reason.
    fileLoc = "D:\temp\output.txt"
    ' Used to count the amount of users in the OU we're adding to.
    Sub FindOU(ObjOU)
    	For Each FoundObject in ObjOU
    	If FoundObject.class="organizationalUnit" Then
    	End If
    	If FoundObject.class="user" Then
    	End If
    End Sub
    ' Start. Prepare variables and get the domain name.
    Set objArgs = WScript.Arguments
    Set oRoot=GetObject("LDAP://rootDSE")
    Set oDomain=GetObject("LDAP://" & oRoot.Get("defaultNamingContext"))
    Set oCurrent = GetObject("LDAP://" & strCompleteOU)
    ' Opens log file for writing. If log file is already present, it will be overwritten, if not it will be created.
    	Set filesys = CreateObject("Scripting.FileSystemObject")
    	Set filetxt = filesys.OpenTextFile(fileLoc, ForWriting, True)
    	' Write a little header on the file to give some idea of what it contains.
    	filetxt.WriteLine("SAM    Common Name")
    ' Opens Excel and loads the workbook given in the var strExcelFileName.
    	Set objExcel = CreateObject("Excel.Application")
    	Set objWorkbook = objExcel.Workbooks.Open(strExcelFileName)
    	' Start at row two in the spreadsheet. Row 1 is left for column headers.
    	intRow = 2
    	' Do statement until the first column is read as blank. Which means no breaks in the data. Please.
    	Do Until objExcel.Cells(intRow,1).Value=""
    		On Error Resume Next
    		' Set initial full OU layout. Adjust the strCompleteOU variable manually above.
    		Set objOU = GetObject("LDAP://"& strCompleteOU)
    		strCommon = "LDAP://cn="& objExcel.Cells(intRow,4).Value &"," & strCompleteOU
    		Set objUser = GetObject(strCommon)
    		If not objUser.sAMAccountName = objExcel.Cells(intRow, 1).Value Then
    			' Create user from common name in fifth column.
    			Set objUser = objOU.Create("User", "cn=" & objExcel.Cells(intRow,4).Value)
    			' SAM account name from first column. Rest is kind of obvious.
    			objUser.sAMAccountName = objExcel.Cells(intRow, 1).Value
    			objUser.GivenName = objExcel.Cells(intRow, 2).Value
    			objUser.SN = objExcel.Cells(intRow, 3).Value
    			objUser.Mail = objExcel.Cells(intRow, 1).Value & "@school.sch.uk"
    			objUser.userPrincipalName = objExcel.Cells(intRow, 1).Value & "@edu.lan"
    			' Special setting next. First row, sixth column is the description (e.g. 2010 Student).
    			objUser.description = objExcel.Cells(1,6).Value
    			objUser.displayName = objExcel.Cells(intRow,4).Value
    			' Need to do a set info here, otherwise the account won't have been created and will not take some of the following settings correctly.
    			objUser.SetPassword objExcel.Cells(intRow, 5).Value
    			' Force user to change password on the next login.
    			objUser.Put "pwdLastSet", 0
    			objUser.AccountDisabled = FALSE
    			' Add user to the global student group.
    			set objGroup = GetObject(strStudentGroup)
    			' Then lets create the home drive.
    			strDirectory = strStaticDir & objExcel.Cells(intRow, 1).Value
    			Set objFSO = CreateObject("Scripting.FileSystemObject")
    			' Create the folder from the above concantinated string.
    			Set objFolder = objFSO.CreateFolder(strDirectory)
    			Set objShell = CreateObject("WScript.Shell")
    			' Run the cscript.exe session invisible, and let the calling VBScript wait for the called VBScript to finish.
    			' Change the 0 to a 1 for the window to momentarily pop up when this command is being run.
    			' We only add the user to the account here as we've set the parent dir to not inherit anything from further up the folder tree.
    			' This means that when the folder's created it should initially only have the Administrators group with full control and the following just adds in the user.
    			runCommand = "cscript.exe " & strXcacls & " " & strDirectory & " /T /E /G EDU\" & objExcel.Cells(intRow, 1).Value & ":F"
    			objShell.Run runCommand, 0, True
    			objUser.homeDirectory = strHomeDir & objUser.sAMAccountName
    			objUser.homeDrive = strHomeDrive
    			' Again use set info to put it all into the account.
    			WScript.echo objUser.sAMAccountName & " " & objUser.displayName & " " & "created."
    			createcount = createcount + 1
    			' Write SAM and display name to the text file if this object already exists elsewhere in the domain.
    			filetxt.WriteLine(objUser.sAMAccountName & "    " & objUser.displayName)
    			skipcount = skipcount + 1
    		End If
    	' Finally, increment the row by one, and do the loop again
    	intRow = intRow + 1
    	' Close workbook and quit Excel. First line forces a confirmation that the file has been saved even although no changes were made to it.
    	' This is for scripting compatibility with Excel 2007.
    	objExcel.ActiveWorkbook.Saved = True
    	' Close output log file.
    ' Finally, report how many users are in the new OU. Just so you can match up the script with what's in the spreadsheet.
    WScript.echo "Total users created: " & createcount
    WScript.echo "Total users skipped: " & skipcount
    WScript.echo "Total users in the OU: " & count

  6. Thanks to Monty1157 from:

    tosh74 (4th August 2010)

+ Post New Thread

Similar Threads

  1. Export list of shares and permissions?
    By cookie_monster in forum Windows Server 2008
    Replies: 4
    Last Post: 13th July 2010, 08:16 AM
  2. Shares and folder permissions in VB.NET
    By GoldenWonder in forum Coding
    Replies: 5
    Last Post: 18th June 2010, 11:55 AM
  3. Replies: 3
    Last Post: 22nd April 2010, 10:13 AM
  4. Clever scripting for Webdav Link Creation
    By westy in forum Scripts
    Replies: 0
    Last Post: 24th November 2009, 05:00 PM
  5. AD User Accounts Not Inheriting Permissions
    By OutLawTorn in forum Windows Server 2000/2003
    Replies: 0
    Last Post: 19th October 2009, 10:56 PM

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts