To use this script, type it into
Notepad
(with Word Wrap disabled) and save it with a .vbs
extension as
CreateUserHomeDirectory.vbs.
Option Explicit
Const WAIT_ON_RETURN = True
Const HIDE_WINDOW = 0
Const USER_ROOT_UNC = "\\dc1\users" 'Set Home Folder Location Here
Dim WshShell, WshNetwork, objFS, objServer, objShare
Set WshShell = Wscript.CreateObject("Wscript.Shell")
Set WshNetwork = WScript.CreateObject("WScript.Network")
Set objFS = CreateObject("Scripting.FileSystemObject")
Set ou = GetObject("LDAP://OU=Users,OU=Billing,OU=Network,DC=my,DC=domain,DC=com")
'Create the User
Set usr = ou.Create("user", "CN=James Smith")
usr.Put "samAccountName", "jsmith"
usr.Put "sn", "Smith"
usr.Put "givenName", "James"
usr.Put "userPrincipalName", "jsmith@my.domain.com"
usr.Put "telephoneNumber", "(555) 555 0111"
usr.Put "title", "Network Billing Dept"
usr.SetInfo
'Now that the user is created, reset their password and enable the account.
usr.SetPassword "secret***!"
usr.AccountDisabled = False
usr.SetInfo
'Now create the User's Home Folder and set permissions.
strUser = usr.samAccountName
Call objFS.CreateFolder(USER_ROOT_UNC & "\" & strUser)
Call WshShell.Run("cacls " & USER_ROOT_UNC & "\" & strUser & _
" /e /g Administrators:F", HIDE_WINDOW, WAIT_ON_RETURN)
Call WshShell.Run("cacls " & USER_ROOT_UNC & "\" & strUser & _
" /e /g " & strUser & ":C", HIDE_WINDOW, WAIT_ON_RETURN)
1. I couldn't figure out what the DN was that I was supposed to replace on line 12. I spent quite some time spelunking, until I figured it out, finally using LDP included in the Utilities.
2. Since option explicit is invoked at the top of the script, a couple additional variables need declaring: ou, usr, struser
3. The script correctly made the directory for the user, but when I went in and checked the user in Active Directory and although the directory was created, it wasn't assigned as the users home directory in the user's AD record.
Thanks for a *wonderful* book....Windows Server Hacks.