Sunday, May 13, 2007

Create User in LDAP

http://brentp.net/2007/02/20/create-user-in-ldap/

Recently i’ve been playing with WS2003 in a Virtual Machine, and i’ve had to create some stuff to automatically setup a user with a roaming profile, and a home directory (and set the appropriate permissions). Enjoy. Save it as createUser.vbs, double click on it and you’re ready to go. NOTE: You might want to remove some domain specific stuff (and if you have special OU’s, edit the script accordingly. The script assigns permissions to the users profile/home dir folders, in this script it gives Administrator full control, and the user themselves full control.

   1:  Option Explicit
   2:  Dim wshShell
   3:  Dim strUser
   4:  Dim objRootLDAP, objContainer, objNewUser
   5:  Dim fso
   6:  Dim profilePath
   7:  Dim strFullName
   8:  dim strPassword
   9:  Dim homePath
  10:  Set fso = CreateObject(“Scripting.FileSystemObject”)
  11:   
  12:  Set wshShell = WScript.CreateObject(“WScript.Shell”)
  13:   
  14:  strUser = InputBox(“User Name?”, “Prompt”, “”, 100, 100)
  15:  strFullName = InputBox(“Full Name?”, “Prompt”, “”, 100, 100)
  16:  strPassword = InputBox(“Password?”, “Prompt”, “”, 100, 100)
  17:   
  18:  ‘ Bind to Active Directory, Users container.
  19:  Set objRootLDAP = GetObject(“LDAP://rootDSE”)
  20:  Set objContainer = GetObject(“LDAP://cn=Users,” & _
  21:  objRootLDAP.Get(“defaultNamingContext”))
  22:   
  23:  homePath = “\HURRICANEHomes”+strUser
  24:  profilePath = “\HURRICANEProfiles”+strUser
  25:   
  26:  Dim objFSO
  27:  Set objFSO = CreateObject(“Scripting.FileSystemObject”)
  28:   
  29:   
  30:  If fso.FolderExists(homePath) = False Then
  31:  fso.CreateFolder(homePath)
  32:  End If
  33:   
  34:  If fso.FolderExists(profilePath) = False Then
  35:  fso.CreateFolder(profilePath)
  36:  End If
  37:   
  38:  ‘ Build the actual User.
  39:  Set objNewUser = objContainer.Create(“User”, “cn=” & strUser)
  40:  objNewUser.Put “sAMAccountName”, strUser
  41:  objNewUser.Put “userPrincipalName”, strUser + “@homegroup.int”
  42:  objNewUser.Put “displayName”, strFullName
  43:  objNewUser.Put “HomeDirectory”, “\HURRICANEHomes”+strUser
  44:  objNewUser.Put “profilePath”, “\HURRICANEProfiles”+strUser
  45:  objNewUser.SetInfo
  46:  objNewUser.SetPassword strPassword
  47:  objNewUser.Put “userAccountControl”, 512
  48:  objNewUser.SetInfo
  49:   
  50:  Dim intRunError
  51:  Dim strHomeFolder
  52:  strHomeFolder = “\HURRICANEHomes”+strUser
  53:  If strHomeFolder <> “” Then
  54:  If Not objFSO.FolderExists(strHomeFolder) Then
  55:  On Error Resume Next
  56:  objFSO.CreateFolder strHomeFolder
  57:       If Err.Number <> 0 Then
  58:       On Error GoTo 0
  59:       Wscript.Echo “Cannot create: “ & strHomeFolder
  60:       End If
  61:  On Error GoTo 0
  62:  End If
  63:       If objFSO.FolderExists(strHomeFolder) Then
  64:       ‘ Assign user permission to home folder.
  65:       intRunError = wshShell.Run(“%COMSPEC% /c Echo Y| cacls “_
  66:       & strHomeFolder & ” /t /c /g Administrators:f “_
  67:       & strUser & “:F”, 2, True)
  68:          If intRunError <> 0 Then
  69:          Wscript.Echo “Error assigning permissions for user “ _
  70:          & strUser & ” to home folder “ & strHomeFolder
  71:          End If
  72:      End If
  73:  End If
  74:   
  75:  Dim strProfileFolder
  76:  strProfileFolder = “\HURRICANEProfiles”+strUser
  77:  If strProfileFolder <> “” Then
  78:  If Not objFSO.FolderExists(strProfileFolder) Then
  79:  On Error Resume Next
  80:  objFSO.CreateFolder strProfileFolder
  81:       If Err.Number <> 0 Then
  82:       On Error GoTo 0
  83:       Wscript.Echo “Cannot create: “ & strProfileFolder
  84:       End If
  85:  On Error GoTo 0
  86:  End If
  87:       If objFSO.FolderExists(strProfileFolder) Then
  88:       ‘ Assign user permission to home folder.
  89:       intRunError = wshShell.Run(“%COMSPEC% /c Echo Y| cacls “_
  90:       & strProfileFolder & ” /t /c /g Administrators:f “_
  91:       & strUser & “:F”, 2, True)
  92:          If intRunError <> 0 Then
  93:          Wscript.Echo “Error assigning permissions for user “ _
  94:          & strUser & ” to profile folder “ & strProfileFolder
  95:          End If
  96:      End If
  97:  End If
  98:  WScript.Echo(“Done.”)
  99:  WScript.Quit

No comments: