header
header Register : : Login header
header
divider
menuleft
menuright
submenu
left

[August 25th, 2008] Check the home page regarding PowerShell related news from a brand new sponsor: Idera

Creating AD users and verifying the username isn't being used
Last Post 30 Nov 2007 01:04 AM by yefimovah. 5 Replies.
Printer Friendly
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages
yefimovahUser is Offline
New Member
New Member
Posts:24
Avatar

--
28 Nov 2007 11:26 PM  
Is there a way to simplify the pop-ups to be able to populate them all at one time?

--------------------------------------------------------------------------------------------------

# A succession of pop-ups to prompt for the new user name details
$fname = new-object -comobject MSScriptControl.ScriptControl
$fname.language = "vbscript"
$mInitial = new-object -comobject MSScriptControl.ScriptControl
$mInitial.language = "vbscript"
$lname = new-object -comobject MSScriptControl.ScriptControl
$lname.language = "vbscript"
$fname.addcode("function getInput() getInput = inputbox(`"Please Provide First Name`",`"First Name`") end function" )
$mInitial.addcode("function getInput() getInput = inputbox(`"Please Provide Middle Initial`",`"Middle Initial`") end function" )
$lname.addcode("function getInput() getInput = inputbox(`"Please Provide Last Name`",`"Last Name`") end function" )
$strFirstName = $fname.eval("getInput")
$strMiddleInitial = $mInitial.eval("getInput")
$strLastName = $lname.eval("getInput")

# Grab the first letter of the first name and concatenate it with the last name to create username
$strUserName = $strFirstName.Substring(0,1) + $strLastName

# Set display name
$strDisplayName = $strFirstName + " " + $strMiddleInitial + ". " + $strLastName

# Set UPN
$strUpn = $strUserName + "@ham.sitel.co.nz"

# Set email address
$strEmail = $strFirstName + "." + $strLastName + "@ham.sitel.co.nz"

# Set SamID for legacy purposes
$strSamid = $strUserName

# Set conical name for user container
$strCNName = "OU=MyTestOU,DC=ham,DC=sitel,DC=co,DC=nz"

# Set conical name for user by concatenating the Display name with the CN Name for the user container
$strValue = "CN="+ $strDisplayName + "," + $strCNName

# Search AD for the existence of the username and push that value to #strSamAccount, If username doesn't exist, then $strSamAccount will be null.
$strSamAccount = get-adobject -value $strUserName

# Set up messagebox to display success or failure of user creation
[void][Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
$MsgBox = [Windows.Forms.MessageBox]
$Button = [Windows.Forms.MessageBoxButtons]::OK

# Create user function, after verifying that the $strUserName is unique. This function calls "dsadd #user" from the CLI
# and then displays via pop-up the creation after checking that the account is unique.
# Also sends an email to system guys to finish off configuration of user.
function CreateAccount
{
    # Verify that the account is unique by checking AD for the account using the display name.
    $displaynametest = Get-ADObject -Value $strdisplayname
    if($displaynametest.Displayname -eq $strdisplayname)
    {
        # Set up message box for pop up indicating there is a problem with an already existing account
        $Icon = [Windows.Forms.MessageBoxIcon]::Warning
        $MsgBox::Show("An account for " + $strDisplayName + " already exists. `n If you believe this is an error, please try again `
        `n", "Unsuccessful Acccount Creation", $Button, $Icon)
    }
    else
    {
        # Add user
        dsadd user $strValue -upn $strUpn -samid $strSamid -fn $strFirstName -mi $strMiddleInitial -ln $strLastName -display $strDisplayName  -email $strEmail -pwd PASSWORD -mustchpwd yes
        # Set up message box for pop up and display of user details
        $Icon = [Windows.Forms.MessageBoxIcon]::Information
        $MsgBox::Show("The windows account for " + $strDisplayName + " has been created. `n`n" + $strFirstName + "'s username is " + $strUserName + ". `n" `
        + $strFirstName + "'s email address is " + $strEmail + ". `n", "Successful Acccount Creation", $Button, $Icon)
        # Prompt with pop-up to populate 3 named groups
        $group1 = new-object -comobject MSScriptControl.ScriptControl
        $group1.language = "vbscript"
        $group2 = new-object -comobject MSScriptControl.ScriptControl
        $group2.language = "vbscript"
        $group3 = new-object -comobject MSScriptControl.ScriptControl
        $group3.language = "vbscript"
        $group1.addcode("function getInput() getInput = inputbox(`"Please provide group you want to add user to`",`"First Group`") end function" )
        $group2.addcode("function getInput() getInput = inputbox(`"Please Provide Second Group name you want to add user to`",`"Second Group`") end function" )
        $group3.addcode("function getInput() getInput = inputbox(`"Please Provide Second Group name you want to add user to`",`"Third Group`") end function" )
        $strFirstGroup = $group1.eval("getInput") > c:\groups.txt
        $strSecondGroup = $group2.eval("getInput") >> c:\groups.txt
        $strThirdGroup = $group3.eval("getInput") >> c:\groups.txt
        # Actual addition to add user to groups from c:\groups.txt
        $username = $strUserName
        $aryGroups = get-Content "c:\groups.txt"
        foreach ($groupname in $aryGroups)
        {
            $usermodname = get-ADObject -value $username
            $usermodname.distinguishedName
            dsquery group -samid $groupname | dsmod group -addmbr $usermodname.distinguishedName
        }
        $groups = dsquery user -name $strDisplayName | dsget user -memberof
        # Mail enable account that you just created.
        # Send email to IT to finish work around the new user to include mail enabling the account
        Send-SmtpMail -SMTPHost "hamxch02.ham.sitel.co.nz" -To "anatoli.yefimov@ham.sitel.co.nz" -From "anatoli.yefimov@ham.sitel.co.nz" -Subject "New Employee added to Domain by HR" `
        -Body "$strDisplayName has had an account created in AD by HR.

Username is $strUserName.

Current group membership is: $groups

Please mail enable this account and set up OU location"
       
    }
   
}

# If $strSamAccount.SamAccountName (username) doesn't exist than run CreatAccount function
if ($strSamAccount.samaccountname -ne $strUserName)
{
    CreateAccount
}
else
    {
    # If username was already in use, then a second letter from the first name is added to the username, samid and upn
        $strUserName = $strFirstName.Substring(0,2) + $strLastName
        $strSamAccount = get-adobject -value $strUserName
        $strEmail = $strFirstName + "." + $strLastName + "@ham.sitel.co.nz"
        $strSamid = $strUserName
        $strUpn = $strUserName + "@ham.sitel.co.nz"
    # If $strSamAccount.SamAccountName (username with second letter from first name) doesn't exist than run CreatAccount function
        if ($strSamAccount.samaccountname -ne $strUserName)
        {
            CreateAccount
        }
     else
        {
            # If username was already in use, then a third letter from the first name is added to the username, samid and upn
            $strUserName = $strFirstName.Substring(0,3) + $strLastName
            $strSamAccount = get-adobject -value $strUserName
            $strEmail = $strFirstName + "." + $strLastName + "@ham.sitel.co.nz"
            $strSamid = $strUserName
            $strUpn = $strUserName + "@ham.sitel.co.nz"
            # If $strSamAccount.SamAccountName (username with second and third letter from first name) doesn't exist than run CreatAccount function
            if ($strSamAccount.samaccountname -ne $strUserName)
            {
                CreateAccount
            }
            else
               {
                $Icon = [Windows.Forms.MessageBoxIcon]::Warning
                $MsgBox::Show("There was a problem with duplicate account names. Please contact IT! `n", "Warning!", $Button, $Icon)
               }
        }
    }
DonJUser is Offline
PowerShell MVP
Basic Member
Basic Member
Posts:134
Avatar

--
29 Nov 2007 03:38 PM  
Well, if you want to use a GUI, the only way would be to programmatically build your own dialog box using WinForms. There's a good chapter on doing so in "Windows PowerShell v1.0: TFM, 2nd Edition" (see the Library, here, for a sample and the table of contents), and there are some shorter examples littered across the Web. It's not for the faint of heart, though - before I launch into an explanation, can you tell me if you have any .NET and WinForms experience already?
- Don Jones
www.ConcentratedTech.com
Subscribe (RSS) or visit for weekly PowerShell tips and lessons
yefimovahUser is Offline
New Member
New Member
Posts:24
Avatar

--
29 Nov 2007 05:06 PM  
Hi Don,

Unfortunately, my experience with scripting is limited to what you see above.. I will purchase "Windows PowerShell v2.0: TFM" and "Windows PowerShell v1.0: TFM (2nd Edition)". In the mean time, if you could help me out, that would be great. :-)
DonJUser is Offline
PowerShell MVP
Basic Member
Basic Member
Posts:134
Avatar

--
29 Nov 2007 09:13 PM  
Sure. So, you've got the basics - loading the WinForms assembly. Cool.

[Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") | out-null

# create new controls
$form = new-object system.windows.forms.form
$button = new-object system.windows.forms.button
$textbox = new-object system.windows.forms.textbox
$label = new-object system.windows.forms.label

# set up label
$label.text = "Enter something"
$label.top = 10
$label.left = 10
$label.height = 20
$label.width = 100

# set up text box
$textbox.top = 30
$textbox.left = 10
$textbox.height = 20
$textbox.width = 100

# set up button
$button.text = "OK"
$button.width = 70
$button.height = 25
$button.left = 10
$button.top = 60

# set up button's click event
# this script block will just hide the form,
# allowing the main script to continue
$button_click = { $form.hide() }
$button.add_click($button_click)

# set up form
$form.text = "My dialog box"
$form.formborderstyle = 2
$form.height = 100
$form.width = 120

# add controls to form
$form.controls.add($label)
$form.controls.add($textbox)
$form.controls.add($button)

# show form
# script will pause at this point
$form.showdialog() | out-null

# get user input from text box
$userinput = $textbox.text

So, you'll get the idea - play with the top, left, height, and width of everything. Add as many labels and textboxes as you like; on the last line I showed you how to get whatever was entered into the textbox into a variable that you can work with. You might need five textboxes, let's say, so I'd use $textbox1, $textbox2, or give them better names - $tb_username, $tb_domain, etc. just to make it easier to keep track.
- Don Jones
www.ConcentratedTech.com
Subscribe (RSS) or visit for weekly PowerShell tips and lessons
yefimovahUser is Offline
New Member
New Member
Posts:24
Avatar

--
30 Nov 2007 01:03 AM  

Don,

That was great!! I've added more text boxes and some color as well. I do appreciate it..

I do have one other question.

How would I create a dropdown box where I could choose groups to add the user to? So I can select as many groups as necessary. The reason I ask is that right now, I have to type in the names of the groups I want to add the user to. We unfortunately have some 60 groups, so it's not practical, how I've written it..

thanks Again.

Anatoli

yefimovahUser is Offline
New Member
New Member
Posts:24
Avatar

--
30 Nov 2007 01:04 AM  

I meant, dropdown list.. :-)

You are not authorized to post a reply.

Active Forums 4.1
right
   
footer Sponsored by Quest Software • SAPIEN Technologies • ShellTools, LLC • Microsoft Windows Server 2008 footer
footer