Custom WinForm Creation in PowerShell

Been a while, again. Must sound like a broken record by now to all of my 0 adoring fans…

Today, I thought I would make a post on something that I could see anywhere on the internet, and trust me, I looked. Mainly because I didn’t know how to do it. But I managed to figure it out and create my own custom form with multiple different types of input.

This is the form I create which I use when creating new users. It gets all the relavant information from one place and does all of the heavy lifting. Instead of having to having to navigate to our Exchange web page, create a new mailbox, then select usually the same options over and over again, then go into Active Directory and add any additional settings. Then find a user that they need to be set up similarly to, and individually add the group membership to the new users profile except for giving them access to a certain group.

I’m only going to show you the form part of my script. The GUI (Graphical User Interface) if you will. Thats because there is alot of custom coding in the back end that I don’t want to get out, you know. Since I did work hard on this.

So here goes, here is the script that creates my custom WinForm for creating new users:


#########################################################################################################################################################################
##MENU GUI CREATION## #
#########################################################################################################################################################################

Add-Type -AssemblyName System.Windows.Forms
#First variables
$global:orgunit = $null
$global:locationnumber = $null
$result = $null

 

$global:form = New-Object System.Windows.Forms.Form
$form.Text = "New User Creation Form"
$form.Size = New-Object System.Drawing.Size(500,590)
$form.StartPosition = "CenterScreen"
#Getting location for icon
$location = Get-Location
#Getting icon from script location
$form.Icon = [system.drawing.icon]::ExtractAssociatedIcon("$location\logo.bmp")

#OK BUTTON
$global:OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Point(175,520)
$OKButton.Size = New-Object System.Drawing.Size(75,23)
$OKButton.Text = "OK"
$OKButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
$form.AcceptButton = $OKButton
$form.Controls.Add($OKButton)

#CANCEL BUTTON
$global:CancelButton = New-Object System.Windows.Forms.Button
$CancelButton.Location = New-Object System.Drawing.Point(250,520)
$CancelButton.Size = New-Object System.Drawing.Size(75,23)
$CancelButton.Text = "Cancel"
$CancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
$form.CancelButton = $CancelButton
$form.Controls.Add($CancelButton)

 

#######DISPLAY NAME#######

$locationnumber = 20

#DISPLAY NAME HEADER
$global:displaynamelabel = New-Object System.Windows.Forms.Label
$displaynamelabel.Location = New-Object System.Drawing.Point(10,$locationnumber)
$displaynamelabel.Size = New-Object System.Drawing.Size(280,20)
$displaynamelabel.Text = "Display Name:"
$form.Controls.Add($displaynamelabel)

#DISPLAY NAME INPUT
$global:displayname = New-Object System.Windows.Forms.TextBox
$displayname.Location = New-Object System.Drawing.Point(10,($locationnumber + 20))
$displayname.Size = New-Object System.Drawing.Size(460,20)
$form.Controls.Add($displayname)

 

#######FIRST NAME#######

$locationnumber = 70

#FIRST NAME LABEL
$global:firstnamelabel = New-Object System.Windows.Forms.Label
$firstnamelabel.Location = New-Object System.Drawing.Point(10,$locationnumber)
$firstnamelabel.Size = New-Object System.Drawing.Size(100,20)
$firstnamelabel.Text = "First Name:"
$form.Controls.Add($firstnamelabel)

#FIRST NAME INPUT
$global:firstname = New-Object System.Windows.Forms.TextBox
$firstname.Location = New-Object System.Drawing.Point(10,($locationnumber + 20))
$firstname.Size = New-Object System.Drawing.Size(180,20)
$form.Controls.Add($firstname)

 

########LAST NAME#######

$locationnumber = 70

#LAST NAME LABEL
$global:lastnamelabel = New-Object System.Windows.Forms.Label
$lastnamelabel.Location = New-Object System.Drawing.Point(210,$locationnumber)
$lastnamelabel.Size = New-Object System.Drawing.Size(280,20)
$lastnamelabel.Text = "Last Name:"
$form.Controls.Add($lastnamelabel)

#LAST NAME INPUT
$global:lastname = New-Object System.Windows.Forms.TextBox
$lastname.Location = New-Object System.Drawing.Point(210,($locationnumber + 20))
$lastname.Size = New-Object System.Drawing.Size(180,20)
$form.Controls.Add($lastname)

 

#######UserPrincipalName#######

$locationnumber = 120

#USERPRINCNAMEHEADER
$global:userprincipalnamelabel = New-Object System.Windows.Forms.Label
$userprincipalnamelabel.Location = New-Object System.Drawing.Point(10,$locationnumber)
$userprincipalnamelabel.Size = New-Object System.Drawing.Size(280,20)
$userprincipalnamelabel.Text = "Logon Name:"
$form.Controls.Add($userprincipalnamelabel)

#USERPRINCNAMEHEADER
$global:userprincipalname = New-Object System.Windows.Forms.TextBox
$userprincipalname.Location = New-Object System.Drawing.Point(10,($locationnumber + 20))
$userprincipalname.Size = New-Object System.Drawing.Size(180,20)
$form.Controls.Add($userprincipalname)

 

#######PASSWORD#######

[string]$initialpassword = ([char[]](Get-Random -Input $(47..57 + 65..90 + 97..122) -Count 7)) + (Get-Random -Minimum 0 -Maximum 10)
$passwordwithspacesremoved = $initialpassword.Replace(' ','')
$finalpassword = $passwordwithspacesremoved
$securepassword = ConvertTo-SecureString -AsPlainText $passwordwithspacesremoved -Force

 

$locationnumber = 170

#PASSWORD LABEL
$global:PasswordLabel = New-Object System.Windows.Forms.Label
$PasswordLabel.Location = New-Object System.Drawing.Point(10,$locationnumber)
$PasswordLabel.Size = New-Object System.Drawing.Size(280,20)
$PasswordLabel.Text = "Password (Make note of this!):"
$form.Controls.Add($PasswordLabel)

#PASSWORD
$global:Password = New-Object System.Windows.Forms.TextBox
$Password.Location = New-Object System.Drawing.Point(10,($locationnumber + 20))
$Password.Size = New-Object System.Drawing.Size(180,20)
$password.Text = $finalpassword
$form.Controls.Add($Password)

 

#######DESCRIPTION#######

 

$locationnumber = 220

#DESCRIPTION LABEL
$global:DescriptionLabel = New-Object System.Windows.Forms.Label
$DescriptionLabel.Location = New-Object System.Drawing.Point(10,$locationnumber)
$DescriptionLabel.Size = New-Object System.Drawing.Size(280,20)
$DescriptionLabel.Text = "Description:"
$form.Controls.Add($DescriptionLabel)

#DESCRIPTION
$global:Description = New-Object System.Windows.Forms.TextBox
$Description.Location = New-Object System.Drawing.Point(10,($locationnumber + 20))
$Description.Size = New-Object System.Drawing.Size(460,20)
$form.Controls.Add($Description)

 

#######OFFICE#######

 

$locationnumber = 320

$global:officeHeader = New-Object System.Windows.Forms.Label
$officeHeader.Location = New-Object System.Drawing.Point(10,$locationnumber)
$officeHeader.Size = New-Object System.Drawing.Size(280,20)
$officeHeader.Text = "Please Select an Office:"
$form.Controls.Add($officeHeader)

[array]$arrayOffices=@("Adelaide","Belfast","Birmingham","Bradford","Bristol","Coventry","Manchester","Nelson","Pembrokeshire","Rossendale","Sheffield","Sydney")

$global:officeComboBox = New-Object System.Windows.Forms.ComboBox
$officeComboBox.Location = New-Object System.Drawing.Size(10,($locationnumber + 20))
$officeComboBox.Size = New-Object System.Drawing.Size(180,20)
$officeComboBox.DropDownHeight = 200
$Form.Controls.Add($officeComboBox)

foreach ($office in $arrayOffices) {
$officeComboBox.Items.Add($office)
}

#######GROUP SETUP#######

$locationnumber = 270

#GROUP LABEL
$global:GroupLabel = New-Object System.Windows.Forms.Label
$GroupLabel.Location = New-Object System.Drawing.Point(10,$locationnumber)
$GroupLabel.Size = New-Object System.Drawing.Size(280,20)
$GroupLabel.Text = "Set Up The User Like This User (Full Name):"
$form.Controls.Add($GroupLabel)

#GROUP
$global:Group = New-Object System.Windows.Forms.TextBox
$Group.Location = New-Object System.Drawing.Point(10,($locationnumber + 20))
$Group.Size = New-Object System.Drawing.Size(180,20)
$form.Controls.Add($Group)
$guideuser = $Group.Text

 

#######OU SELECTION#######

$locationnumber = 370

$global:OUHeader = New-Object System.Windows.Forms.Label
$OUHeader.Location = New-Object System.Drawing.Point(10,$locationnumber)
$OUHeader.Size = New-Object System.Drawing.Size(280,20)
$OUHeader.Text = "Please Select the an Organizational Unit:"
$form.Controls.Add($OUHeader)

[array]$arrayOU=@("Process Industries","Production & Print","Proteus","Mail Order","Multi-Channel Solutions(SMCSL)","One Iota","Priam","Wholesale & Distribution","Finance & Admin","Management","Marketing","Training")

$global:OUComboBox = New-Object System.Windows.Forms.ComboBox
$OUComboBox.Location = New-Object System.Drawing.Size(10,($locationnumber + 20))
$OUComboBox.Size = New-Object System.Drawing.Size(180,20)
$OUComboBox.DropDownHeight = 200
$Form.Controls.Add($OUComboBox)

foreach ($OU in $arrayOU) {
$OUComboBox.Items.Add($OU)
}

 

switch ($OUComboBox){
"Process Industries"{$OrgUnit = "domain.com/Business Units/Manufacturing/Process Industries"}
"Production & Print"{$OrgUnit = "domain.com/Business Units/Manufacturing/Production & Print"}
"Proteus"{$OrgUnit= "domain.com/Business Units/Manufacturing/Proteus"}
"Mail Order"{$OrgUnit = "domain.com/Business Units/Multi-Channel/Mail Order"}
"Multi-Channel Solutions(SMCSL)"{$OrgUnit = "domain.com/Business Units/Multi-Channel/Multi-Channel Solutions (SMCSL)"}
"One Iota"{$OrgUnit = "domain.com/Business Units/Multi-Channel/One Iota"}
"Priam"{$OrgUnit = "domain.com/Business Units/Multi-Channel/Priam"}
"Wholesale & Distribution"{$OrgUnit = "domain.com/Business Units/Multi-Channel/Wholesale & Distribution"}
"Finance & Admin"{$OrgUnit = "domain.com/Business Units/Sanderson Group/Finance & Admin"}
"Management"{$OrgUnit = "domain.com/Business Units/Sanderson Group/Management"}
"Marketing"{$OrgUnit = "domain.com/Business Units/Sanderson Group/Marketing"}
"Training"{$OrgUnit = "domain.com/Business Units/Sanderson Group/Training"}
}

#######DATABASE SELECTION#######

$locationnumber = 420

$global:DataBaseHeader = New-Object System.Windows.Forms.Label
$DataBaseHeader.Location = New-Object System.Drawing.Point(10,$locationnumber)
$DataBaseHeader.Size = New-Object System.Drawing.Size(280,20)
$DataBaseHeader.Text = "Please Select a mailbox database:"
$form.Controls.Add($DataBaseHeader)

[array]$arrayDataBase=@("User Mailboxes","Leavers")

$global:DataBaseComboBox = New-Object System.Windows.Forms.ComboBox
$DataBaseComboBox.Location = New-Object System.Drawing.Size(10,($locationnumber + 20))
$DataBaseComboBox.Size = New-Object System.Drawing.Size(180,20)
$DataBaseComboBox.DropDownHeight = 200
$Form.Controls.Add($DataBaseComboBox)

foreach ($Database in $arrayDataBase) {
$DataBaseComboBox.Items.Add($Database)
}

#######RESET PASSWORD CHECKBOX#######
$global:resetpasswordcheckbox = new-object System.Windows.Forms.checkbox
$resetpasswordcheckbox.Location = new-object System.Drawing.Size(10,470)
$resetpasswordcheckbox.Size = new-object System.Drawing.Size(200,45)
$resetpasswordcheckbox.Text = "Reset Password Upon First Logon?"
$resetpasswordcheckbox.Checked = $false
$form.Controls.Add($resetpasswordcheckbox)

#########################################################################################################################################################################
##CONSOLIDATING ALL VARIABLES## #
#########################################################################################################################################################################

$form.Topmost = $True

$form.Add_Shown({$displayname.Select()})
$global:result = $form.ShowDialog()

#DONE
$global:name = $displayname.Text
$name
#DONE
$global:firstname = $firstname.Text
$firstname
#DONE
$global:lastname = $lastname.Text
$lastname
#DONE
$global:loginname = $userprincipalname.Text
$loginname
$global:samname = $loginname + "@domain.com"
#DONE
$global:jobtitle = $jobtitle.Text
$jobtitle
#DONE
$global:finalpassword
#DONE
$global:Description = $Description.Text
$Description
#DONE
$global:office = $officeComboBox.Text
$office
#DONE
$global:guideuser = $Group.Text
$guideuser
#DONE
$global:OrganizationalUnit = $OUComboBox.Text
$OrganizationalUnit
#DONE
$global:Database = $DataBaseComboBox.Text
$Database
#DONE
$global:resetpassword = $resetpasswordcheckbox.Checked
$resetpassword

Hopefully someone finds this useful. I might one day post my entire script. Of which consists of around 4 files and a folder for the new uses information to be added to.

You can probably see that I also changed the logo or icon on the form to make it more customized for its purpose. All you need to do to fix this is create a bmp image and put it in the same location/ folder as the main script. Then you should get a custom logo. I might do a full post on that one day. With pictures and everything. It’ll look so pretty.

Enjoy!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s