I am creating a powershell script to check if RSAT is installed, install it if it isn't, then create a mailbox in exchange, set the company property on the user based on input in the form, and move it to an OU based on the company. Everything in the script works, the user creation, moving it, etc... but there is one thing that I want to do to get it polished off. On the main form that asks for all the user info I have an OK and Cancel button. What I would like to do is have it so that when you click the cancel button the script ends, but at the moment when you click cancel it just continues processing.
I have been searching around for a while but I haven't found any clear indication of what is required to make the script exit when you click cancel. I will be the first to admit that I don't know as much as I would like about powershell, so if its something super obvious then my apologies.
If anyone is wondering the functions with the write-host are there as a crude way to jump around the script. Also the RSAT install is commented out and i have a pause just to make sure that it actually does that step, I don't want to install it on the workstation I'm doing this on.
Add-PSSnapin Microsoft.Exchange.Management.PowerShell.SnapIn
Add-Type -AssemblyName PresentationCore,PresentationFramework
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
$ENDSCRIPT = $false
function CheckRSAT()
{
Write-Host 'Beginning RSAT check...'
}
if (!(Get-Module "RSAT-AD-PowerShell")){
$MODULEBUTTONTYPE = [System.Windows.MessageBoxButton]::YesNo
$MODULEMESSAGETITLE = “RSAT Not Installed”
$MODULEMESSAGEBODY = @"
RSAT is not installed and is required to move user to appropiate OU.
Would you like to install it now? Choosing "No" will end script.
"@
$MODULEICON = [System.Windows.MessageBoxImage]::Warning
$MODULECHOICE = [System.Windows.MessageBox]::Show($MODULEMESSAGEBODY,$MODULEMESSAGETITLE,$MODULEBUTTONTYPE,$MODULEICON)
}
else{
CreateMailBox
}
if ($MODULECHOICE -eq 'Yes'){
<#Import-Module ServerManager
Add-WindowsFeature -Name "RSAT-AD-PowerShell" –IncludeAllSubFeature#>
pause
}
else{
if ($MODULECHOICE -eq 'No'){
exit
}
}
CheckRSAT
function CreateMailBox()
{
Write-Host 'Starting CreateMailBox form'
}
$form = New-Object System.Windows.Forms.Form
$form.Text = 'Mailbox Creation'
$form.Size = New-Object System.Drawing.Size(400,550)
$form.StartPosition = 'CenterScreen'
$okButton = New-Object System.Windows.Forms.Button
$okButton.Location = New-Object System.Drawing.Point(75,480)
$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)
$cancelButton = New-Object System.Windows.Forms.Button
$cancelButton.Location = New-Object System.Drawing.Point(150,480)
$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)
$label = New-Object System.Windows.Forms.Label
$label.Location = New-Object System.Drawing.Point(10,20)
$label.Size = New-Object System.Drawing.Size(480,20)
$label.Text = @"
Please enter the user's first name in the space below:
"@
$form.Controls.Add($label)
$textBox = New-Object System.Windows.Forms.TextBox
$textBox.Location = New-Object System.Drawing.Point(10,40)
$textBox.Size = New-Object System.Drawing.Size(260,20)
$form.Controls.Add($textBox)
$label1 = New-Object System.Windows.Forms.Label
$label1.Location = New-Object System.Drawing.Point(10,70)
$label1.Size = New-Object System.Drawing.Size(480,20)
$label1.Text = @"
Please enter the user's last name in the space below:
"@
$form.Controls.Add($label1)
$textBox1 = New-Object System.Windows.Forms.TextBox
$textBox1.Location = New-Object System.Drawing.Point(10,90)
$textBox1.Size = New-Object System.Drawing.Size(260,20)
$form.Controls.Add($textBox1)
$label2 = New-Object System.Windows.Forms.Label
$label2.Location = New-Object System.Drawing.Point(10,120)
$label2.Size = New-Object System.Drawing.Size(480,20)
$label2.Text = @"
Please select the user's company from below:
"@
$form.Controls.Add($label2)
$listBox = New-Object System.Windows.Forms.ListBox
$listBox.Location = New-Object System.Drawing.Point(10,140)
$listBox.Size = New-Object System.Drawing.Size(260,20)
$listBox.SelectionMode = 'One'
[void] $listBox.Items.Add('company')
[void] $listBox.Items.Add('company2')
$listBox.Height = 40
$form.Controls.Add($listBox)
$label3 = New-Object System.Windows.Forms.Label
$label3.Location = New-Object System.Drawing.Point(10,180)
$label3.Size = New-Object System.Drawing.Size(380,30)
$label3.Text = @"
Please enter password to be assigned to the account. Password must adhere to domain security policies:
"@
$form.Controls.Add($label3)
$textBox2 = New-Object System.Windows.Forms.TextBox
$textBox2.Location = New-Object System.Drawing.Point(10,210)
$textBox2.Size = New-Object System.Drawing.Size(260,20)
$form.Controls.Add($textBox2)
$label4 = New-Object System.Windows.Forms.Label
$label4.Location = New-Object System.Drawing.Point(10,250)
$label4.Size = New-Object System.Drawing.Size(380,20)
$label4.Text = @"
Please enter the user's full name below:
"@
$form.Controls.Add($label4)
$textBox3 = New-Object System.Windows.Forms.TextBox
$textBox3.Location = New-Object System.Drawing.Point(10,270)
$textBox3.Size = New-Object System.Drawing.Size(260,20)
$form.Controls.Add($textBox3)
$label5 = New-Object System.Windows.Forms.Label
$label5.Location = New-Object System.Drawing.Point(10,300)
$label5.Size = New-Object System.Drawing.Size(380,30)
$label5.Text = @"
Please enter the user's email address below. This will be used for authentication from email clients, OWA, etc...:
"@
$form.Controls.Add($label5)
$textBox4 = New-Object System.Windows.Forms.TextBox
$textBox4.Location = New-Object System.Drawing.Point(10,330)
$textBox4.Size = New-Object System.Drawing.Size(260,20)
$form.Controls.Add($textBox4)
$label6 = New-Object System.Windows.Forms.Label
$label6.Location = New-Object System.Drawing.Point(10,370)
$label6.Size = New-Object System.Drawing.Size(380,40)
$label6.Text = @"
Please enter the user's alias below, this is the "Pre-Windows 2000 Username" and can be used to logon to computers. Follow company naming convention when specifying:
"@
$form.Controls.Add($label6)
$textBox5 = New-Object System.Windows.Forms.TextBox
$textBox5.Location = New-Object System.Drawing.Point(10,410)
$textBox5.Size = New-Object System.Drawing.Size(260,20)
$form.Controls.Add($textBox5)
$form.Topmost = $true
$form.Add_Shown({$textBox.Select()})
$result = $form.ShowDialog()
Function MailBoxCommandPrompt(){
Write-Host 'Beginning final mailbox confirmation and creation'
}
$FirstName = $textBox.Text
$LastName = $textBox1.Text
$Password = ConvertTo-SecureString $textBox2.Text -AsPlainText -Force
$FullName = $textBox3.Text
$UPN = $textBox4.Text
$Alias = $textBox5.Text
$Company = $listBox.SelectedItem
$BUTTONTYPE = [System.Windows.MessageBoxButton]::YesNo
$MESSAGETITLE = “Mailbox Creation”
$MESSAGEBODY = @"
Mailbox creation will proceed with the following commands.
-->New-Mailbox -Name "$FullName" -UserPrincipalName $UPN -Password $Password -Alias $Alias -FirstName $FirstName -LastName $LastName
-->Set-User -Identity $Alias -Company $Company
-->Move-ADObject 'CN=$Alias,CN=Users,DC=company,DC=local' -TargetPath 'OU=$Company,DC=company,DC=local'
Do you wish to continue? Choosing "No" will end the script.
"@
$ICON = [System.Windows.MessageBoxImage]::Information
$CHOICE = [System.Windows.MessageBox]::Show($MESSAGEBODY,$MESSAGETITLE,$BUTTONTYPE,$ICON)
if ( $CHOICE -eq 'Yes')
{
New-Mailbox -Name "$FullName" -UserPrincipalName $UPN -Password $Password -Alias $Alias -FirstName $FirstName -LastName $LastName
Set-User -Identity $Alias -Company $Company
Move-ADObject 'CN=$Alias,CN=Users,DC=domain,DC=local' -TargetPath 'OU=$Company,DC=domain,DC=local'
}
else
{
Exit
}
pause