djcomplex
06-11-2002, 12:19 PM
i am trying to write a script to add users to active directory, i _think_ it works ok but it just isnt connecting to the domain atm so that isnt a script issue, what i want to be able to do is import a .txt file with a list of users in so it makes the accounts automatically because creating 1200 users accounts one at a time is a no no
this is what i have so far
On Error Resume Next
dim objDS, objUser, wshNetwork
dim WshShell
dim objRoot,objDom
set wshShell=CreateObject("Wscript.Shell")
set wshNetwork=CreateObject("Wscript.Network")
set objRoot=GetObject("LDAP://RootDSE")
set objDom=GetObject("LDAP://" & objRoot.Get("defaultNamingContext"))
shortADSPath=MID(objDom.ADSPath,8)
strDomain=wshNetwork.UserDomain
'Where will the user's home directory be created?
strUsersDir="\\MCS\users$"
strHomeSrv="MCS"
'what is the local path on the server where the share will be created?
strLocalPath="C:\users"
strUser=Trim(InputBox("Enter in the new username","Create User Account","firstname lastname"))
ChkInput err.Number,"You entered a blank user name or cancelled."
numSpace=InStr(struser," ")
strFirst=Left(strUser,Numspace-1)
strLast=Mid(strUser,NumSpace+1,20)
strLogon=Left(strFirst,1)&strLast
strUsername=LCASE(Left(strFirst,1)&strlast)
strDomain="magnacarta.surrey.sch.uk"
strPass=InputBox("Enter in a password","Create User Account",LCase(strLogon))
ChkInput strPass,"You entered a blank password or cancelled."
strYearGroup=InputBox("Enter the Year Group")
strOU=InputBox("Where do you want to create the new user account?","Create User Account","c:/users/"&strYearGroup&"/"&strLogon&"")
set objDS=GetObject(strOU)
ChkErr err.number,"Error connecting to " & strDomain
'uncomment next line for debugging
wscript.echo "Creating account for " & strFirst & " " & strLast & " (" & Left(strFirst,1)&strlast&"@"&strDomain&")"
'Create user object
set objUser=objDS.Create ("User","cn="&strUser)
objuser.Put "samAccountName",strUserName
objuser.Put "UserPrincipalName",strUserName & "@" & strDomain & ""
objUser.SetInfo
ChkErr err.number,"Error setting info for " & strUserName & " on " & strDomain
'Now that user object is created, let's set some properties
'There are plenty of other AD properties you can set. These are the more
'likely ones.
objUser.FirstName=strFirst
objUser.LastName=strLast
objUser.FullName=strFirst & " " & strLast
objUser.Description="Created " & NOW & " by " & wshNetwork.UserName
objUser.EmailAddress=Left(strFirst,1)&strlast&"@"&strDomain&".com"
objUser.AccountDisabled=True
objUser.SetPassword(strPass)
objUser.SetInfo
objUser.HomeDrive="C:"
objUser.HomeDirectory="\\" & strHomeSrv &"\" & strUserName & "$"
'comment out the next line if you don't want to specify a network profile
'location
objUser.Profile="\\mcs\profiles$\" & strUserName
objUser.SetInfo
'Create User Directory
'dim oFileSys
set oFileSys=CreateObject("Scripting.FileSystemObject")
oFileSys.CreateFolder(strUsersDir & "\" & strUserName)
'share the new home directory
dim objNewShare, objSrv
set objSrv=GetObject("WinNT://" & strHomeSrv & "/LanManServer")
set objNewShare=objSrv.Create("fileshare",strUserName & "$")
objNewShare.Path=strLocalPath & "\" & strUserName
objNewShare.MaxUserCount=2
objNewShare.SetInfo
'you may need to give the service a moment or two to finish this process
'before continuing
wscript.Sleep 5000
'we need to temporarily map a drive to the user's new directory so we
'can set permissions. Script will error and fail if the X: drive is
'already in use. You may want to add code to check if X: exists and
'disconnect it if it does.
wshNetwork.MapNetworkDrive "x:","\\"&strHomeSrv & "\" & strUserName & "$"
'set permissions
'windows are hidden. Change 0 to 1 to see windows
wshShell.Run "cmd /c echo y|cacls X:\ /g Administrators:F",0,True
wshShell.Run "cacls x:\ /e /g " & strDomain & "\" & strUserName & ":C",0,True
'drop network connection
wshNetwork.RemoveNetworkDrive "x:",True
'Force user to change password the first time they logon
set user=GetObject("WinNT://" & strDomain & "/" & strUserName & ",User")
User.Put "PasswordExpired",1
User.SetInfo
'Add user to a group or groups
'Repeat as needed.
dim objGroup
strGroup="Student"
set objGroup=GetObject("WinNT://" & strDomain & "/" & strGroup & ",Group")
objGroup.Add(user.ADSPath)
objGroup.SetInfo
set objGroup=Nothing
wscript.Echo "User account created for " & strUserName
'Empty our objects
set User=Nothing
set objUser=Nothing
set wshNetwork=Nothing
set objDS=Nothing
set objRoot=Nothing
set objDom=Nothing
set wshShell=Nothing
wscript.quit
'///////////////////////////
'/ Error Handling /
'///////////////////////////
Function ChkErr(errReturn,strMsg)
On Error Resume Next
If errReturn<>0 then
wscript.echo strMsg
set oDomain=Nothing
set oNewGroup=Nothing
set ochkGrp=Nothing
set oGrp=Nothing
wscript.quit
End If
End Function
Function ChkInput(strReturn,strMsg)
On Error Resume Next
If strReturn="" then
wscript.echo strMsg
set oDomain=Nothing
set oNewGroup=Nothing
set ochkGrp=Nothing
set oGrp=Nothing
wscript.quit
End If
End Function
'EOF
this is what i have so far
On Error Resume Next
dim objDS, objUser, wshNetwork
dim WshShell
dim objRoot,objDom
set wshShell=CreateObject("Wscript.Shell")
set wshNetwork=CreateObject("Wscript.Network")
set objRoot=GetObject("LDAP://RootDSE")
set objDom=GetObject("LDAP://" & objRoot.Get("defaultNamingContext"))
shortADSPath=MID(objDom.ADSPath,8)
strDomain=wshNetwork.UserDomain
'Where will the user's home directory be created?
strUsersDir="\\MCS\users$"
strHomeSrv="MCS"
'what is the local path on the server where the share will be created?
strLocalPath="C:\users"
strUser=Trim(InputBox("Enter in the new username","Create User Account","firstname lastname"))
ChkInput err.Number,"You entered a blank user name or cancelled."
numSpace=InStr(struser," ")
strFirst=Left(strUser,Numspace-1)
strLast=Mid(strUser,NumSpace+1,20)
strLogon=Left(strFirst,1)&strLast
strUsername=LCASE(Left(strFirst,1)&strlast)
strDomain="magnacarta.surrey.sch.uk"
strPass=InputBox("Enter in a password","Create User Account",LCase(strLogon))
ChkInput strPass,"You entered a blank password or cancelled."
strYearGroup=InputBox("Enter the Year Group")
strOU=InputBox("Where do you want to create the new user account?","Create User Account","c:/users/"&strYearGroup&"/"&strLogon&"")
set objDS=GetObject(strOU)
ChkErr err.number,"Error connecting to " & strDomain
'uncomment next line for debugging
wscript.echo "Creating account for " & strFirst & " " & strLast & " (" & Left(strFirst,1)&strlast&"@"&strDomain&")"
'Create user object
set objUser=objDS.Create ("User","cn="&strUser)
objuser.Put "samAccountName",strUserName
objuser.Put "UserPrincipalName",strUserName & "@" & strDomain & ""
objUser.SetInfo
ChkErr err.number,"Error setting info for " & strUserName & " on " & strDomain
'Now that user object is created, let's set some properties
'There are plenty of other AD properties you can set. These are the more
'likely ones.
objUser.FirstName=strFirst
objUser.LastName=strLast
objUser.FullName=strFirst & " " & strLast
objUser.Description="Created " & NOW & " by " & wshNetwork.UserName
objUser.EmailAddress=Left(strFirst,1)&strlast&"@"&strDomain&".com"
objUser.AccountDisabled=True
objUser.SetPassword(strPass)
objUser.SetInfo
objUser.HomeDrive="C:"
objUser.HomeDirectory="\\" & strHomeSrv &"\" & strUserName & "$"
'comment out the next line if you don't want to specify a network profile
'location
objUser.Profile="\\mcs\profiles$\" & strUserName
objUser.SetInfo
'Create User Directory
'dim oFileSys
set oFileSys=CreateObject("Scripting.FileSystemObject")
oFileSys.CreateFolder(strUsersDir & "\" & strUserName)
'share the new home directory
dim objNewShare, objSrv
set objSrv=GetObject("WinNT://" & strHomeSrv & "/LanManServer")
set objNewShare=objSrv.Create("fileshare",strUserName & "$")
objNewShare.Path=strLocalPath & "\" & strUserName
objNewShare.MaxUserCount=2
objNewShare.SetInfo
'you may need to give the service a moment or two to finish this process
'before continuing
wscript.Sleep 5000
'we need to temporarily map a drive to the user's new directory so we
'can set permissions. Script will error and fail if the X: drive is
'already in use. You may want to add code to check if X: exists and
'disconnect it if it does.
wshNetwork.MapNetworkDrive "x:","\\"&strHomeSrv & "\" & strUserName & "$"
'set permissions
'windows are hidden. Change 0 to 1 to see windows
wshShell.Run "cmd /c echo y|cacls X:\ /g Administrators:F",0,True
wshShell.Run "cacls x:\ /e /g " & strDomain & "\" & strUserName & ":C",0,True
'drop network connection
wshNetwork.RemoveNetworkDrive "x:",True
'Force user to change password the first time they logon
set user=GetObject("WinNT://" & strDomain & "/" & strUserName & ",User")
User.Put "PasswordExpired",1
User.SetInfo
'Add user to a group or groups
'Repeat as needed.
dim objGroup
strGroup="Student"
set objGroup=GetObject("WinNT://" & strDomain & "/" & strGroup & ",Group")
objGroup.Add(user.ADSPath)
objGroup.SetInfo
set objGroup=Nothing
wscript.Echo "User account created for " & strUserName
'Empty our objects
set User=Nothing
set objUser=Nothing
set wshNetwork=Nothing
set objDS=Nothing
set objRoot=Nothing
set objDom=Nothing
set wshShell=Nothing
wscript.quit
'///////////////////////////
'/ Error Handling /
'///////////////////////////
Function ChkErr(errReturn,strMsg)
On Error Resume Next
If errReturn<>0 then
wscript.echo strMsg
set oDomain=Nothing
set oNewGroup=Nothing
set ochkGrp=Nothing
set oGrp=Nothing
wscript.quit
End If
End Function
Function ChkInput(strReturn,strMsg)
On Error Resume Next
If strReturn="" then
wscript.echo strMsg
set oDomain=Nothing
set oNewGroup=Nothing
set ochkGrp=Nothing
set oGrp=Nothing
wscript.quit
End If
End Function
'EOF