Code:
'This script imporTs users from a CSV file into the OU specified
'Chris Hindmarch 11/07/2005 Fishermore High School
'************* USE AT YOUR OWN RISK **************
'THE AUTHOR TAKES NO RESPONSIBILITY FOR ANY DAMAGE THAT COULD BE CAUSED BY THIS SCRIPT
'********* Begin Object Paths **********
'This is the path to the OU. For a top Level OU just put the name of the OU
'If the OU is nested then the path must look like this
'studenTs,OU=limitedusers
Const StudentOU = "year7,ou=studenTs,ou=fmlimitedusers"
Const GroupName = "year7"
Const GroupPath = "year7,ou=studenTs,ou=fmlimitedusers"
Const UPNName = "fishermore.lancs.sch.uk"
Const Dom_Short_Name = "fishermore" 'Needed for the xcacls doesnt seem to like UPNs
'********* End Object Paths **********
Const Temp_Password = "fmpassword"
Const ForReading = 1
Const ForAppending = 8
Const ADS_PROPERTY_APPEND = 3
Const server="\\filesrv\fmstudent$\year7"
Const Home_Local_Path="d:\Home\FMStudent\Year7"
RecordsRead = 0
UsersCreated = 0
FoldersChanged = 0
inputfile="Year72006fullnames2.csv"
outputfile="userdone.txt"
Set ObjShell = Wscript.CreateObject("Wscript.Shell")
Set objRootDSE = GetObject("LDAP://RootDSE")
Set ObjFSO = CreateObject("Scripting.FileSystemObject")
Set UserFile = ObjFSO.opentextfile(inputfile,forreading)
Set logFso = CreateObject("Scripting.FileSystemObject")
Set Tslog = logFso.OpenTextFile(outputfile, ForAppending)
UserADPath = ""
'while not at end of file
while UserFile.atendofstream <> true
sstring=UserFile.readline
RecordsRead = RecordsRead + 1
UserName = sstring
'UserName = CreateUserName(sstring)
tslog.writeline "Begin New Record"
tslog.writeline "Name read = " & UserName
'*************** Begin UserName Generation
On Error Resume Next
i = 1
UserExists = True
'Takes a full name and coverts it to a username
'in the format of smith.b
'***************************************************
TheSpace = InStr(1,Username, " ",1) 'Find the space in the name
'Wscript.echo TheSpace
'Get the first name
FirstName = Left(Username,(TheSpace -1))
tslog.writeline "First name generated = " & FirstName
'Wscript.echo "First Name = " & Firstname
'Get Surname
Surname = Right(UserName,Len(UserName) - TheSpace )
tslog.writeline "Surname Generated = " & SurName
'Wscript.echo "Surname = " & Surname
'****************************************************
'generate username. Check if it exists already
While UserExists
'Old style username
'UserName = Surname & "." & Left(Firstname,i)
'begin new style username
UserName = "06" & Left(Firstname,3) & Left(Surname,(i + 1))
tslog.writeline "UserName Generated = " & Username
'Wscript.echo "UserName= " & UserName
Set ObjUser = GetObject("WinNT://" & Dom_Short_Name & "/" & UserName & ",user")
If Err.Number <> 0 Then
UserExists = False
tslog.writeline "No accounts matching the UserName " & UserName _
& " Could be found"
tslog.writeline "Creating User"
'Wscript.Echo "User Does not exist creating account"
Else
'Wscript.echo "User Exists"
tslog.writeline "The UserName already exists... Restarting UserName generation"
i = i + 1
Err.Clear
End If 'Error handling
Wend
'*************** End UserName Generation
'Wscript.Echo "First Name Passed = " & FirstName
'Wscript.Echo "Surname Passed = " & SurName
'Wscript.Echo "The Username Generated is " & Username
UserADPath = "ou=" & StudentOU & "," & objRootDSE.Get("DefaultNamingContext")
Set ObjParent = Getobject ("LDAP://" & UserADPath)
Set ObjUser = ObjParent.Create("user", "cn=" & UserName )
ObjUser.put "name", firstname & " " & surname
ObjUser.Put "sAMAccountName", UserName
ObjUser.put "userPrincipalName", UserName & "@" & UPNName
ObjUser.Put "givenName", firstname
ObjUser.Put "sn", surname
ObjUser.put "displayName", firstname & " " & surname
ObjUser.put "homeDirectory", server & "\" & UserName
ObjUser.put "homeDrive", "z:"
ObjUser.put "description", "Year 7"
ObjUser.Setinfo
ObjUser.Setpassword(Temp_Password)
ObjUser.accountdisabled=FALSE
ObjUser.Put "pwdLasTSet", CLng(0)
ObjUser.Setinfo
UsersCreated = UsersCreated + 1
tslog.writeline "User Account Created " & "no:" & UsersCreated
'Put user in the selected group
Set ObjGroup = GetObject _
("LDAP://cn="& GroupName & "," & "ou=" & GroupPath _
& "," & objRootDSE.Get("DefaultNamingContext"))
ObjGroup.PutEx ADS_PROPERTY_APPEND, _
"member", Array("cn=" & UserName & "," & UserADPath)
ObjGroup.SetInfo
tslog.writeline "Added User to group " & GroupName
Set objFolder = ObjFSO.CreateFolder(Home_Local_Path & "/" & UserName)
'Set NTFS Permissions on the folder
CMDLine = "cscript xcacls.vbs " & Home_Local_Path & "\" _
& UserName & " /T /G teachers:r ""Domain admins:f"" " & Dom_Short_Name & "\" & UserName & ":m"
wscript.echo CMDline
ObjShell.Run CMDLine
tslog.writeline "Done Setting permissions on folder " & Dom_Short_Name & "\" & UserName
'The Method below does not seem to work
'CMDLine = "%comspec% /k cscript xcacls.vbs " & Home_Local_Path & "\" _
'& UserName & " " & "/o" & " " & "fishermore\" & UserName
'ObjShell.Run CMDLine, 1
'Use the Win32 Version of CHOWN to set ownership
CMDLine = "chown " & UserName & " " & Home_Local_Path & "\" _
& UserName
wscript.echo CMDLine
ObjShell.Run CMDLine
tslog.writeline "Made the user the owner of their home directory"
FoldersChanged = FoldersChanged + 1
tslog.writeline VbCrLf
Wend
tslog.writeline "Records read " & RecordsRead
tslog.writeline "users created " & UsersCreated
tslog.writeline "Folders Changed = " & FoldersChanged
Function CreateUserName (UserName)
On Error Resume Next
i = 1
UserExists = True
'Takes a full name and coverts it to a username
'in the format of smith.b
TheSpace = InStr(1,Username, " ",1) 'Find the space in the name
'Wscript.echo TheSpace
'Get the first name
FirstName = Left(Username,(TheSpace -1))
Wscript.echo "First Name = " & Firstname
'Get Surname
Surname = Right(UserName,Len(UserName) - TheSpace )
Wscript.echo "Surname = " & Surname
'generate username. Check if it exists already
While UserExists
UserName = Surname & "." & Left(Firstname,i)
Wscript.echo "UserName= " & UserName
Set ObjUser = GetObject("WinNT://" & Dom_Short_Name & "/" & UserName & ",user")
If Err.Number <> 0 Then
UserExists = False
Wscript.Echo "User Does not exist creating account"
Else
Wscript.echo "User Exists"
i = i + 1
Err.Clear
End If 'Error handling
Wend
CreateUser = UserName
End Function
Function CheckUser (UserName)
End Function