Jump to content

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


Recommended Posts

Posted

Hi

 

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

 

thanks

 

J.

Posted
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.

Posted

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
	FindOU(FoundObject)
End If
If FoundObject.class="user" Then
	count=count+1
End If
Next 
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.SetInfo
		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)
		objGroup.Add(strCommon)
		' 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.
		objUser.SetInfo
		WScript.echo objUser.sAMAccountName & " " & objUser.displayName & " " & "created."
		createcount = createcount + 1
	Else
		' 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
Loop
' 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
   objExcel.ActiveWorkbook.Close
   objExcel.Application.Quit
' Close output log file.
filetxt.Close 
' Finally, report how many users are in the new OU. Just so you can match up the script with what's in the spreadsheet.
FindOU(oCurrent)
WScript.echo "Total users created: " & createcount
WScript.echo "Total users skipped: " & skipcount
WScript.echo "Total users in the OU: " & count

 

Cheerie,

Monty

  • Thanks 1

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...