Monday, April 02, 2012

AutoIt - Add Users to Active Directory / Creating New Users

So I'm progressing with Autoit quite nicely and I'm trying to get it to add new users to Active Directory. There isn't much info about this on google. I'm guessing that most people use professional software for this task. Well we're cheap here so I've had to come up with my own way of doing it.

So if you want to use the code below, the first thing you'll have to do is download the AD.au3 files and put them all in your 'include' directory.

Now this script will alter your active directory. Please test it first before you do anything. I'm not being held responsible for you messing up your AD.

As you can see in the code below, it takes a list of names from the 'c:\test.txt' file (each name should be on its own line). It then takes this list, creates each one a password and creates a userarea a network drive (I've used Z:\). Then outputs each user a text file containing a welcome message that you can edit, their username and their password. Finally it will add the user to Active Directory.

So here's the code

#include <file.au3>
#include <AD.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

   If @error Then Exit MsgBox(16, "Active Directory Script", "Function _AD_Open encountered a problem. @error = " & @error & ", @extended = " & @extended)
   Global $Reply = MsgBox(308, "Active Directory Functions", "This script creates a new user in the specified OU."  "Are you sure you want to change the Active Directory?")
If $Reply <> 6 Then Exit
$fileopen = "C:\test.txt"
$file = FileOpen($fileopen,0)

for $x = 1 to _FileCountlines($fileopen)
   Local $line = FileReadLine($file)
   $pwd = ""
   Dim $aSpace[3]
   $digits = 8
   For $i = 1 To $digits
   $aSpace[0] = Chr(Random(65, 90, 1)) ;A-Z
   $aSpace[1] = Chr(Random(97, 122, 1)) ;a-z
   $aSpace[2] = Chr(Random(48, 57, 1)) ;0-9
   $pwd &= $aSpace[Random(0, 2, 1)]
   DirCreate("z:\"&$line&"\Application Data")
   DirCreate("z:\"&$line&"\My Documents")
   DirCreate("z:\"&$line&"\My Pictures")
   DirCreate("z:\"&$line&"\My Videos")
   DirCreate("z:\"&$line&"\My Music")
   local $WriteFile = FileOpen("C:\"&$line&".txt",1)
   $name = StringStripWS($line,8)
   FileWriteLine($writefile, "Welcome to WHAT EVER YOUR BUSINESS IS CALLED")
   FileWriteLine($writefile, "")
   FileWriteLine($writefile, "Username = "& $name)
   FileWriteLine($writefile, "Password = "& $Pwd)

      local $OU = "OU=Staff,OU=Active Users,DC=dcname,DC=local"
   Global $iValue = _AD_CreateUser($sOU, $name, $line)
If $iValue = 1 Then
 MsgBox(64, "Active Directory Functions", "User '" & $line & "' in OU '" & $sOU & "' successfully created")
ElseIf @error = 1 Then
 MsgBox(64, "Active Directory Functions", "User '" & $line & "' already exists")
ElseIf @error = 2 Then
 MsgBox(64, "Active Directory Functions", "OU '" & $sOU & "' does not exist")
ElseIf @error = 3 Then
 MsgBox(64, "Active Directory Functions", "Value for CN (e.g. Lastname Firstname) is missing")
ElseIf @error = 4 Then
 MsgBox(64, "Active Directory Functions", "Value for User is missing")
 MsgBox(64, "Active Directory Functions", "Return code '" & @error & "' from Active Directory")

Now a few things to talk about. First it doesn't set permissions on the users folders. You'll have to either write that bit in yourself or do it manually. Secondly it doesn't set the password on Active directory. I haven't worked out how to do this yet, let me know if you figure out how. Thirdly the active directory section, only fills in the basic information. If you look through the AD functions you'll be able to see what else you can add, but this should start you off.

Have fun and try not to break anything.

No comments: