1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
| ' ------------------------------------------------------'
Option Explicit
Dim objConnection, objCommand, objRecordSet
Dim strUserAccount, strFullName, strLastName, strFirstName, strMail, strPassword
Dim objRootLDAP, objContainer, objNewUser
Dim var, intAccValue, intPwdValue
strPassword = "P@ssw0rd"
' Here is where we set the value to enable the account
' 512 = Enable, 514 = Disable.
intAccValue = 512
intPwdValue = 0 ' Default is -1 : 0 to change
set var = WScript.Arguments
strUserAccount = var(0)
strFullName = var(1)
strLastName = var(2)
strFirstName = var(3)
strMail = var(4)
Set objConnection = CreateObject("ADODB.Connection")
objConnection.Open "Provider=ADsDSOObject;"
Set objCommand = CreateObject("ADODB.Command")
objCommand.ActiveConnection = objConnection
objCommand.CommandText = _
"<LDAP://dc=xxxx,dc=xxx,dc=local>;(&(objectCategory=User)" & _
"(samAccountName=" & strUserAccount & "));samAccountName;subtree"
Set objRecordSet = objCommand.Execute
If objRecordset.RecordCount = 0 Then
' Bind to Active Directory, Users container.
Set objRootLDAP = GetObject("LDAP://xxxxx")
Set objContainer = GetObject("LDAP://OU=xxxxx," & _
objRootLDAP.Get("defaultNamingContext"))
' Build the actual User.
Set objNewUser = objContainer.Create("User", "cn=" & strFullName)
objNewUser.Put "sAMAccountName", strUserAccount
objNewUser.Put "displayName", strFullName
objNewUser.Put "sn", strLastName
objNewUser.Put "givenName", strFirstName
objNewUser.Put "mail", strMail
objNewUser.SetInfo
objNewUser.SetPassword strPassword
objNewUser.Put "userAccountControl", intAccValue
objNewUser.Put "PwdLastSet", intPwdValue
objNewUser.SetInfo
Else
WScript.Echo strUserAccount & " already exists."
End If
objConnection.Close
WScript.Quit
' End of free sample Create Users VBScript. |
Partager