Jump to content
Sign in to follow this  

Copy Template User in AD

Recommended Posts


I am creating a script to copy a template user in Active Directory. I have gotten the add a new user to work but having problems copying a user. It seems to be related to the For Next statement at line 130. Error is variable must be of type "object". I am new to this scripting and the array is killing me and the mess that I call a script. I am trying to get it to work then I will clean it and make it look pretty as I learn more. Any help would be appreciated. I did the conversion from VBS to AutoIT with the script but still have issues.

;Include constants
#include <GUIConstants.au3>
#include <GUICombo.au3>

;Initialize variables
Global $GUIWidth
Global $GUIHeight

$GUIWidth = 600
$GUIHeight = 500

;Create window
GUICreate("Organizational Unit", $GUIWidth, $GUIHeight)

;Create an edit box with no text in it
;$Edit_1 = GUICtrlCreateEdit("", 10, 10, 280, 190)

;Create a "Next" button
$CreateUser_Btn = GUICtrlCreateButton("Create User", 100, 400, 70, 25)

;Create a "CANCEL" button
$Cancel_Btn = GUICtrlCreateButton("Cancel", 175, 400, 70, 25)

;Create a "DropDown" box
$Combo_Box = GUICtrlCreateCombo("Select OU", 75, 100, 500, 10)

;Create Input Box
$Lbl_firstName =GUICtrlCreateInput("First Name", 75, 50, 100, 20)

;Create Input Box
$Lbl_MI =GUICtrlCreateInput("M.I.", 185, 50, 25, 20)

;Create Input Box
$Lbl_lastName =GUICtrlCreateInput("Last Name", 220, 50, 200, 20)

;Show window/Make the window visible

;Loop until:
;- user presses Esc
;- user presses Alt+F4
;- user clicks the close button 

  ;Populate ComboBox with OU's
Local $objCommand = ObjCreate("ADODB.Command")
Local $objConnection = ObjCreate("ADODB.Connection")

$objConnection.Provider = "ADsDSOObject"
$objConnection.Open ("Active Directory Provider")
$objCommand.ActiveConnection = $objConnection

Local $strBase = "<GC://ou=User-Accounts,dc=xxxxxx,dc=xxxxx>"
Local $strFilter = "(objectCategory=organizationalUnit)"
Local $strAttributes = "distinguishedName"
Local $strQuery = $strBase & ";" & $strFilter & ";" & $strAttributes & ";subtree"

$objCommand.CommandText = $strQuery
$objCommand.Properties ("Page Size") = 100
$objCommand.Properties ("Timeout") = 30
$objCommand.Properties ("Cache Results") = False
$objCommand.Properties ("searchscope") = $ADS_SCOPE_SUBTREE

Local $objRecordSet = $objCommand.Execute

        While Not $objRecordSet.EOF
            $strdistinguishedName = $objRecordSet.Fields("distinguishedName").value


While 1
 ;After every loop check if the user clicked something in the GUI window
   $msg = GUIGetMsg() 
   $userName = ("testsiegman")
    ;Check if user clicked on the close button
        Case $msg = $GUI_EVENT_CLOSE
        ;Destroy the GUI including the controls
        ;Exit the script
    ;Check if user clicked on the "OK" button
        Case $msg = $CreateUser_Btn
            $testTemplate=("ttempl" & $destinationOU)
            Global $Array[4]
        ;$arrAttrs      = $Array
                Case $destinationOU=("OU=TEST OU,OU=User-Accounts,DC=xxxxxx,DC=xxxxxx")
                    $objTemplate = ObjGet("LDAP://" & "cn=" & $testTemplate)
                    $objParent = ObjGet("LDAP://" & $destinationOU)
                    $objUser = $objParent.Create("user", "cn=" & $userName)
                    $objUser.Put ("sAMAccountName",$userName)
                ;$objUser.Put ("userAccountControl", ADS_UF_NORMAL_ACCOUNT)
                    Const $ADS_UF_NORMAL_ACCOUNT = 512 ; from ADS_USER_FLAG_ENUM
                ;$objUser.Put ($sAMAccountName, $strNewUser)
                For $strAttr in $Array
                    $objUser.Put ($strAttr, $objTemplate.objGet($strAttr))

                    $objUser.Put ("userAccountControl", $ADS_UF_NORMAL_ACCOUNT)
                    $objUser.AccountDisabled = 0

                MsgBox(64, "User Creation", $firstName & $lastName & "Has been Created in" & $destinationOU)

            $objParent = ObjGet("LDAP://" & $destinationOU)
            $objUser = $objParent.Create("user", "cn=" & $userName) 
            $objUser.Put ("givenName", $firstName)  
            $objUser.Put ("sAMAccountName",$userName)
            $objUser.Put ("sn", $lastName)
            $objUser.AccountDisabled = True
            MsgBox(64, "User Creation", $firstName & $lastName & "Has been Created in" & $destinationOU)

    ;Check if user clicked on the "CANCEL" button
        Case $msg = $Cancel_Btn
        ;MsgBox(64, "New GUI", "You clicked on the Cancel button!")
        ;Destroy the GUI including the controls
        ;Exit the script


$objConnection = ""
$objCommand = ""
$objRecordSet = ""

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this  


Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.