0

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
mrlizard
  • 1
  • 2
  • 1
    See this answer https://stackoverflow.com/a/3097383/15339544 – Santiago Squarzon Dec 10 '21 at 01:39
  • I'm not sure if that answer applies here, or I didn't understand it, plus that's C#. Regardless I tried Form.Close() after Add_Click but the script just kept going. This is a basic idea of what i'm trying now `function CANCELCHECK(){ exit } $cancelButton.Add_Click({CANCELCHECK}) ` The idea is that when you click cancel it goes to the CANCELCHECK function, which then exits the script. But now when i click cancel it gives me a JIT error. I was reading that exit only exits the function when called in a function, which i guess puts me at square one. – mrlizard Dec 10 '21 at 19:43
  • Good lord I think stack overflow got tired of editing that comment over and over again, was trying to figure out how to put line breaks and a code block. If its not clear then i'll just put it as an edit to the original question. – mrlizard Dec 10 '21 at 19:50
  • **always** post code in the question and not in a comment, because that quickly becomes unreadable. P.S. You shouldn't use `exit` inside a form action, but instead `.Close()` the form gracefully. I also notice you don't do a `$form.Dispose()` when you are done with it, so running this over and over again would eventually gobble up all your available memory. AND your code would improve a lot if you would **indent** the code properly. – Theo Dec 11 '21 at 13:51

1 Answers1

0

Ok i found a way to get this to work. I basically put everything in a function, and then at the end of the function i call the next one in the work flow, anytime i need to exit i use return. There is one last thing I can't figure out but i'll post a new question for that cause its not related to exiting the script. When i try to install the AD powershell tools when running the script by right clicking on it or starting it from a command prompt it just exits the script, but it works fine from ISE, not sure why that's happening. Below is the script.

Import-Module ServerManager
Add-Type -AssemblyName PresentationCore,PresentationFramework
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing


$ENDSCRIPT = $false
$CANCELCLICK = $null
$MODULECHOICE = $null
$NULLCHOICE = $null
$global:ISNULL = $null
<#$global:FirstName = $null
$global:LastName = $null
$global:Password = $null
$global:FullName = $null
$global:UPN = $null
$global:Alias = $null
$global:Company = $null#>

$global:FirstName = $textBox.Text
$global:LastName = $textBox1.Text
$global:Password = $textBox2.Text 
$global:FullName = $textBox3.Text
$global:UPN = $textBox4.Text
$global:Alias = $textBox5.Text
$global:Company = $listbox.SelectedItem


Function CREATEMAILBOX(){
$form = New-Object System.Windows.Forms.Form
$form.Text = 'Mailbox Creation1'
$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
$okButton.Add_Click({DefineVars})
$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
$cancelButton.Add_Click({CANCELCHECK})
$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)

$global:textBox = New-Object System.Windows.Forms.TextBox
$global:textBox.Location = New-Object System.Drawing.Point(10,40)
$global: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)

$global:textBox1 = New-Object System.Windows.Forms.TextBox
$global:textBox1.Location = New-Object System.Drawing.Point(10,90)
$global: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)

$global:listBox = New-Object System.Windows.Forms.ListBox
$global:listBox.Location = New-Object System.Drawing.Point(10,140)
$global:listBox.Size = New-Object System.Drawing.Size(260,20)
$global:listBox.SelectionMode = 'One'

[void] $global:listBox.Items.Add('company1')
[void] $global:listBox.Items.Add('company2')

$global: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)

$global:textBox2 = New-Object System.Windows.Forms.TextBox
$global:textBox2.Location = New-Object System.Drawing.Point(10,210)
$global: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)

$global:textBox3 = New-Object System.Windows.Forms.TextBox
$global:textBox3.Location = New-Object System.Drawing.Point(10,270)
$global: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)

$global:textBox4 = New-Object System.Windows.Forms.TextBox
$global:textBox4.Location = New-Object System.Drawing.Point(10,330)
$global: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)

$global:textBox5 = New-Object System.Windows.Forms.TextBox
$global:textBox5.Location = New-Object System.Drawing.Point(10,410)
$global: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 DefineVars(){
$global:FirstName = $textBox.Text
$global:LastName = $textBox1.Text
$global:Password = ConvertTo-SecureString $textBox2.Text -AsPlainText -Force
$global:FullName = $textBox3.Text
$global:UPN = $textBox4.Text
$global:Alias = $textBox5.Text
$global:Company = $listBox.SelectedItem
NullCheck2
}

function CheckRSAT(){
    if (!(Get-Module -listavailable "ActiveDirectory")){
    $MODULEBUTTONTYPE = [System.Windows.MessageBoxButton]::YesNo
    $MODULEMESSAGETITLE = “RSAT Not Installed”
    $MODULEMESSAGEBODY = @"
RSAT AD Tools are not installed and are required to set company attribute on AD object.  

Would you like to install it now?  Choosing "No" will end script.
"@
    $MODULEICON = [System.Windows.MessageBoxImage]::Warning

    $global:MODULECHOICE = [System.Windows.MessageBox]::Show($MODULEMESSAGEBODY,$MODULEMESSAGETITLE,$MODULEBUTTONTYPE,$MODULEICON)

    RSATChoice
    }
    else{
    CREATEMAILBOX
    }
}


function RSATChoice(){
    if ($MODULECHOICE -eq 'Yes')
    {
    Add-WindowsFeature -Name "RSAT-AD-PowerShell" –IncludeAllSubFeature -Verbose
    CheckRSAT
    CREATEMAILBOX
    }
    else
    {
        if ($MODULECHOICE -eq 'No'){
        return
        }
    }
}

function NullCheck2(){
if (!$FirstName -or !$LastName -or !$Password -or !$FullName -or !$UPN -or !$Alias -or !$Company){
$ISNULL = $true
NullCheck
}
    else{
    MailBoxCommandPrompt
    }
}

function NullCheck(){
if ($global:ISNULL = $true) {
$NULLBUTTONTYPE = [System.Windows.MessageBoxButton]::YesNo
$NULLMESSAGETITLE = “Mailbox Creation”
$NULLMESSAGEBODY = "
You have not specified a value for one of the fields, please review them below for empty values


First Name: $FirstName
Last Name: $LastName
Password: $Password
Full Name: $FullName
UPN: $UPN
Alias: $Alias
Company: $Company

Would you like to try again?  Choosing No will end the script."
$NULLICON = [System.Windows.MessageBoxImage]::Warning

$NULLCHOICE = [System.Windows.MessageBox]::Show($NULLMESSAGEBODY,$NULLMESSAGETITLE,$NULLBUTTONTYPE,$NULLICON)
    if ($NULLCHOICE -eq 'Yes'){
    CREATEMAILBOX
    }
        elseif ($NULLCHOICE -eq 'No'){
        return
        }
    }
}


Function MailBoxCommandPrompt(){
$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 -OrganizationalUnit domain.local/$Company
-->Set-User -Identity $Alias -Company $Company  

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 -OrganizationalUnit "domain.local/$Company"
    Set-User -Identity $Alias -Company $Company
}
    else
    {
    return
    }
}

function CANCELCHECK(){
return
}

CheckRSAT
mrlizard
  • 1
  • 2