ComboBox.SelectedItem giving Null value - powershell

I have been stuck in this for whole day. I am creating a UI using PowerShell forms. In that:
user select an option from 1st combobox. click button Go
Based on the selection, a panel will appear having another combobox.
if user select another option in 1st combobox then another panel appears with another combobox
After selecting options from panel comboboxes, user clicks on start button.
This leads to a function which stores the selected options to a variable.
Problem
Now when user selects the options from the comboboxes of panels, I am using $combobox.selecteditem.Tostring to get the values.
But it gives me NULL result.
Here is my code..
$global:Button1Clicked = 0;
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
[System.Windows.Forms.Application]::EnableVisualStyles()
[System.Windows.Forms.FormClosingEventHandler]
$Form = New-Object system.Windows.Forms.Form
$Form.ClientSize = '1500,800'
$Form.text = "NextUI "
$Form.BackColor = 'White'
$Form.TopMost = $true
$Form.add_closing(
{
if($global:Button2Clicked)
{
$global:Button2Clicked = 0;
$_.cancel = $true;
}
})
$panel1 = New-Object system.Windows.Forms.Panel
$panel1.AutoSize = $true
$panel1.Width = 1200
$panel1.Height = 200
$panel1.location = New-Object System.Drawing.Point(50,100)
$panel1.Visible = $false
$panel1.Controls.Add($label3)
$panel1.Controls.Add($comboBox2)
$panel1.BorderStyle = 1
$panel2 = New-Object system.Windows.Forms.Panel
$panel2.AutoSize = $true
$panel2.Width = 1200
$panel2.Height =200
$panel2.location = New-Object system.Drawing.Point(50,100)
$panel2.Visible = $false
$panel2.Controls.Add($label4)
$panel2.Controls.Add($ComboBox3)
$panel2.BorderStyle = 1
$Label1 = New-Object system.Windows.Forms.Label
$Label1.text = "Select Module"
$Label1.AutoSize = $true
$Label1.location = New-Object System.Drawing.Point(35,50)
$Label1.Font = 'segoe ui,9.5'
$Label2 = New-Object system.Windows.Forms.Label
$Label2.text = "SharePoint Settings"
$Label2.AutoSize = $true
$Label2.location = New-Object System.Drawing.Point(35,15)
$Label2.Font = 'Segoe UI Semibold,9.5'
$Label3 = New-Object system.Windows.Forms.Label
$Label3.text = "Choose file and folder permission option"
$Label3.AutoSize = $true
$Label3.location = New-Object System.Drawing.Point(100,200)
$Label3.Font = 'segoe ui,9.5'
$Label4 = New-Object system.Windows.Forms.Label
$Label4.AutoSize = $True
$Label4.text = "File Permission"
$Label4.location = New-Object System.Drawing.Point(100,200)
$Label4.Font = 'Segoe UI ,9.5'
################ Module combo box ################
$ComboBox1 = New-Object system.Windows.Forms.ComboBox
$ComboBox1.text = "Select Module"
$ComboBox1.width = 200
$ComboBox1.height = 20
$ComboBox1.location = New-Object System.Drawing.Point(310, 45)
$ComboBox1.Font = 'Microsoft Sans Serif,10'
$combobox1.items.Add("ControlSettings")
$combobox1.items.Add("NextSettings")
######## file and folder permission #############
$ComboBox2 = New-Object system.Windows.Forms.ComboBox
$ComboBox2.text = "select an option"
$ComboBox2.width = 200
$ComboBox2.height = 20
$ComboBox2.location = New-Object System.Drawing.Point(450, 200)
$ComboBox2.Font = 'Microsoft Sans Serif,10'
$ComboBox2.items.Add("View")
$ComboBox2.items.Add("Edit")
$ComboBox3 = New-Object system.Windows.Forms.ComboBox
$ComboBox3.text = "select an option"
$ComboBox3.width = 200
$ComboBox3.height = 20
$ComboBox3.location = New-Object System.Drawing.Point(450, 200)
$ComboBox3.Font = 'Microsoft Sans Serif,10'
$ComboBox3.items.Add("View")
$ComboBox3.items.Add("Edit")
Function Button2_Click()
{
if ($ComboBox1.SelectedIndex -eq 0)
{
$panel2.Visible = $true
$panel1.Visible = $false
}
if ($ComboBox1.SelectedIndex -eq 1)
{
$panel1.Visible = $true
$panel2.Visible = $false
}
}
$Button1 = New-Object system.Windows.Forms.Button
$Button1.text = "Start"
$Button1.width = 150
$Button1.height = 30
$Button1.BackColor = '#F6CEE3'
$Button1.DialogResult = [System.Windows.Forms.DialogResult]::OK
$Button1.location = New-Object System.Drawing.Point(500, 700)
$Button1.Font = 'segoe ui,10'
$Button1.Add_Click({
Button1_Click;
$global:Button1Clicked = 1;
})
############# Button 'Go' #############
$Button2 = New-Object system.Windows.Forms.Button
$Button2.text = "Go"
$Button2.width = 100
$Button2.height = 30
$Button2.BackColor = '#F6CEE3'
$Button2.DialogResult = [System.Windows.Forms.DialogResult]::OK
$Button2.location = New-Object System.Drawing.Point(680, 43)
$Button2.Font = 'segoe ui,10'
$Button2.Add_Click({
Button2_Click;
$global:Button2Clicked = 1;
})
######### code Starts ###########
function Button1_Click()
{
$link = $comboBox2.SelectedItem.ToString();
$linktype = $comboBox3.SelectedItem.ToString();
}
####### end of code ######
$form.Controls.Add($Panel1)
$form.Controls.Add($Panel2)
$form.Controls.Add($button1)
$form.Controls.Add($comboBox1)
$form.Controls.Add($button2)
$form.Controls.Add($pictureBox1)
$form.Controls.Add($label1)
$form.Controls.Add($label2)
[void]$Form.Add_Shown({ $Form.Activate() })
[void]$Form.ShowDialog()

The issue is that your variables are being set in a function, so they are scoped to that function. There's really no reason to put that in a function, put it directly in the .add_click() instead. Or if you feel that you need to keep it in the function you can scope the variable like you did with $global:ButtonClicked and set them to $global:Link and $global:LinkType.
Edit: Order does make a difference in PowerShell, so I did move some stuff around in your script to make it work right, but I was able to get values to reflect fine when I put them in the global scope.
$global:Button1Clicked = 0;
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
[System.Windows.Forms.Application]::EnableVisualStyles()
[System.Windows.Forms.FormClosingEventHandler]
######### code Starts ###########
function Button1_Click()
{
write-host "Link = $global:link"
write-host "Link Type = $global:linktype"
}
Function Button2_Click()
{
if ($ComboBox1.SelectedIndex -eq 0)
{
$panel2.Visible = $true
$panel1.Visible = $false
}
if ($ComboBox1.SelectedIndex -eq 1)
{
$panel1.Visible = $true
$panel2.Visible = $false
}
}
####### end of code ######
$Form = New-Object system.Windows.Forms.Form
$Form.ClientSize = '1500,800'
$Form.text = "NextUI "
$Form.BackColor = 'White'
$Form.TopMost = $true
$Form.add_closing(
{
if($global:Button2Clicked)
{
$global:Button2Clicked = 0;
$_.cancel = $true;
}
})
################ Module combo box ################
$ComboBox1 = New-Object system.Windows.Forms.ComboBox
$ComboBox1.text = "Select Module"
$ComboBox1.width = 200
$ComboBox1.height = 20
$ComboBox1.location = New-Object System.Drawing.Point(310, 45)
$ComboBox1.Font = 'Microsoft Sans Serif,10'
$combobox1.items.Add("ControlSettings")
$combobox1.items.Add("NextSettings")
######## file and folder permission #############
$ComboBox2 = New-Object system.Windows.Forms.ComboBox
$ComboBox2.text = "select an option"
$ComboBox2.width = 200
$ComboBox2.height = 20
$ComboBox2.location = New-Object System.Drawing.Point(450, 200)
$ComboBox2.Font = 'Microsoft Sans Serif,10'
$ComboBox2.items.AddRange(#("View","Edit") )
#$ComboBox2.items.Add("Edit")
$ComboBox3 = New-Object system.Windows.Forms.ComboBox
$ComboBox3.text = "select an option"
$ComboBox3.width = 200
$ComboBox3.height = 20
$ComboBox3.location = New-Object System.Drawing.Point(450, 200)
$ComboBox3.Font = 'Microsoft Sans Serif,10'
$ComboBox3.items.Add("View")
$ComboBox3.items.Add("Edit")
$panel1 = New-Object system.Windows.Forms.Panel
$panel1.AutoSize = $true
$panel1.Width = 1200
$panel1.Height = 200
$panel1.location = New-Object System.Drawing.Point(50,100)
$panel1.Visible = $false
$panel1.Controls.Add($label3)
$panel1.Controls.Add($comboBox2)
$panel1.BorderStyle = 1
$panel2 = New-Object system.Windows.Forms.Panel
$panel2.AutoSize = $true
$panel2.Width = 1200
$panel2.Height =200
$panel2.location = New-Object system.Drawing.Point(50,100)
$panel2.Visible = $false
$panel2.Controls.Add($label4)
$panel2.Controls.Add($ComboBox3)
$panel2.BorderStyle = 1
$Label1 = New-Object system.Windows.Forms.Label
$Label1.text = "Select Module"
$Label1.AutoSize = $true
$Label1.location = New-Object System.Drawing.Point(35,50)
$Label1.Font = 'segoe ui,9.5'
$Label2 = New-Object system.Windows.Forms.Label
$Label2.text = "SharePoint Settings"
$Label2.AutoSize = $true
$Label2.location = New-Object System.Drawing.Point(35,15)
$Label2.Font = 'Segoe UI Semibold,9.5'
$Label3 = New-Object system.Windows.Forms.Label
$Label3.text = "Choose file and folder permission option"
$Label3.AutoSize = $true
$Label3.location = New-Object System.Drawing.Point(100,200)
$Label3.Font = 'segoe ui,9.5'
$Label4 = New-Object system.Windows.Forms.Label
$Label4.AutoSize = $True
$Label4.text = "File Permission"
$Label4.location = New-Object System.Drawing.Point(100,200)
$Label4.Font = 'Segoe UI ,9.5'
$Button1 = New-Object system.Windows.Forms.Button
$Button1.text = "Start"
$Button1.width = 150
$Button1.height = 30
$Button1.BackColor = '#F6CEE3'
$Button1.DialogResult = [System.Windows.Forms.DialogResult]::OK
$Button1.location = New-Object System.Drawing.Point(500, 700)
$Button1.Font = 'segoe ui,10'
$Button1.Add_Click({
$global:link = $comboBox2.SelectedItem.ToString();
$global:linktype = $comboBox3.SelectedItem.ToString();
Button1_Click;
$global:Button1Clicked = 1;
})
############# Button 'Go' #############
$Button2 = New-Object system.Windows.Forms.Button
$Button2.text = "Go"
$Button2.width = 100
$Button2.height = 30
$Button2.BackColor = '#F6CEE3'
$Button2.DialogResult = [System.Windows.Forms.DialogResult]::OK
$Button2.location = New-Object System.Drawing.Point(680, 43)
$Button2.Font = 'segoe ui,10'
$Button2.Add_Click({
Button2_Click;
$global:Button2Clicked = 1;
})
$form.Controls.Add($Panel1)
$form.Controls.Add($Panel2)
$form.Controls.Add($button1)
$form.Controls.Add($comboBox1)
$form.Controls.Add($button2)
$form.Controls.Add($pictureBox1)
$form.Controls.Add($label1)
$form.Controls.Add($label2)
[void]$Form.Add_Shown({ $Form.Activate() })
[void]$Form.ShowDialog()
When I ran that, set the two combobox values, and clicked Start I got text in the ISE saying what I set the Link and LinkType variables to, and was able to echo $global:link and $global:linktype to see the values correctly assigned.

Related

IF in a Function not working (PowerShell)

I got a problem with PowerShell and its IF-Cmdlet.
It can also be the TextBox where I want to input a variable to set a default text. In either way it doesn't work as intended. What it should do exactly is described im the Code^^
Thanks to all people helping me out^^
It is not for work or anything...its just a little project I try^^
Oh...and sorry for my bad English(maybe), I'm from Germany.
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
function windowInstall1()
{
$window = New-Object System.Windows.Forms.Form
$window.Width = 550
$window.Height = 600
$window.Text = "TimeStampProgram Installer"
#Adding a Label(Header)
$Label1 = New-Object System.Windows.Forms.Label
$Label1.Location = New-Object System.Drawing.Size(10,10)
$Label1.Text = "Welcome to the TimeStamp Installation Setup"
$Label1.Size = New-Object System.Drawing.Size(450,40)
$Label1.Font = New-Object System.Drawing.Font("Lucidia Console" , 15, [System.Drawing.FontStyle]::Regular)
$window.Controls.Add($Label1)
#Adding a Label
$Label2 = New-Object System.Windows.Forms.Label
$Label2.Location = New-Object System.Drawing.Size(10,50)
$Label2.Text = ("To Continue the Installation please press " + '"' + "Continue" + '"')
$Label2.Size = New-Object System.Drawing.Size(450,20)
$Label2.Font = New-Object System.Drawing.Font("Lucidia Console" , 10, [System.Drawing.FontStyle]::Regular)
$window.Controls.Add($Label2)
#Adding a Label
$Label3 = New-Object System.Windows.Forms.Label
$Label3.Location = New-Object System.Drawing.Size(10,71)
$Label3.Text = ("Else press " + '"' + "Exit" + '"' + " to cancel the installation")
$Label3.Size = New-Object System.Drawing.Size(450,20)
$Label3.Font = New-Object System.Drawing.Font("Lucidia Console" , 10, [System.Drawing.FontStyle]::Regular)
$window.Controls.Add($Label3)
#Adding a button
$windowButton1 = New-Object System.Windows.Forms.Button
$windowButton1.Location = New-Object System.Drawing.Size(100,500)
$windowButton1.Size = New-Object System.Drawing.Size(75,50)
$windowButton1.Text = "Continue"
$windowButton1.Add_Click({
$global:status1 = "true"
$window.Dispose()
windowInstall2
})
$window.Controls.Add($windowButton1)
#Adding a button
$windowButton2 = New-Object System.Windows.Forms.Button
$windowButton2.Location = New-Object System.Drawing.Size(300,500)
$windowButton2.Size = New-Object System.Drawing.Size(75,50)
$windowButton2.Text = "Exit"
$windowButton2.Add_Click({
$global:status1 = "false"
$window.Dispose()
})
$window.Controls.Add($windowButton2)
[void]$window.ShowDialog((New-Object System.Windows.Forms.Form -Property #{TopMost = $true }))
}
function windowInstall2()
{
$window = New-Object System.Windows.Forms.Form
$window.Width = 550
$window.Height = 600
$window.Text = "TimeStampProgram Installer"
#Adding a Label(Header)
$Label1 = New-Object System.Windows.Forms.Label
$Label1.Location = New-Object System.Drawing.Size(10,10)
$Label1.Text = "Choose your Program"
$Label1.Size = New-Object System.Drawing.Size(450,40)
$Label1.Font = New-Object System.Drawing.Font("Lucidia Console" , 15, [System.Drawing.FontStyle]::Regular)
$window.Controls.Add($Label1)
#Adding RadioButtons in a GroupBox
$radioButton1 = New-Object System.Windows.Forms.RadioButton
$radioButton2 = New-Object System.Windows.Forms.RadioButton
$groupBox1 = New-Object System.Windows.Forms.GroupBox
$groupBox1.Controls.AddRange(
#(
$radioButton1,
$radioButton2
))
$groupBox1.Location = New-Object System.Drawing.Point(10, 60)
$groupBox1.Name = 'groupBox'
$groupBox1.Size = New-Object System.Drawing.Size(160, 100)
$groupBox1.Text = 'Programms'
# radioButton1
$radioButton1.Location = New-Object System.Drawing.Point(8, 32)
$radioButton1.Name = 'Button1'
$radioButton1.Text = 'TimesStamp'
$radioButton1.Size = New-Object System.Drawing.Size(150, 20)
# radioButton2
$radioButton2.Location = New-Object System.Drawing.Point(8, 64)
$radioButton2.Name = 'Button2'
$radioButton2.Text = 'TimeStamp with Text'
$radioButton2.Size = New-Object System.Drawing.Size(150, 20)
$window.Controls.Add($groupBox1)
#Adding a Button
$windowButton1 = New-Object System.Windows.Forms.Button
$windowButton1.Location = New-Object System.Drawing.Size(100,500)
$windowButton1.Size = New-Object System.Drawing.Size(75,50)
$windowButton1.Text = "Continue"
$windowButton1.Add_Click({
$global:status2 = "true"
$window.Dispose()
windowInstall3
})
$window.Controls.Add($windowButton1)
#Adding a Button
$windowButton2 = New-Object System.Windows.Forms.Button
$windowButton2.Location = New-Object System.Drawing.Size(300,500)
$windowButton2.Size = New-Object System.Drawing.Size(75,50)
$windowButton2.Text = "Exit"
$windowButton2.Add_Click({
$global:status2 = "false"
$window.Dispose()
})
$window.Controls.Add($windowButton2)
[void]$window.ShowDialog((New-Object System.Windows.Forms.Form -Property #{TopMost = $true }))
$status2
}
function folderDialog()
{
#Choose a Folder
$ChooseFolder = New-Object System.Windows.Forms.FolderBrowserDialog
$ChooseFolder.Description = 'Select the Saving Location Folder'
$ChooseFolder.ShowDialog((New-Object System.Windows.Forms.Form -Property #{TopMost = $true }))
$global:tempDir = $ChooseFolder.SelectedPath
}
function windowInstall3()
{
$window = New-Object System.Windows.Forms.Form
$window.Width = 550
$window.Height = 600
$window.Text = "TimeStampProgram Installer"
#Another Label(Header)
$Label1 = New-Object System.Windows.Forms.Label
$Label1.Location = New-Object System.Drawing.Size(10,10)
$Label1.Text = "Setup Install Location"
$Label1.Size = New-Object System.Drawing.Size(450,40)
$Label1.Font = New-Object System.Drawing.Font("Lucidia Console" , 15, [System.Drawing.FontStyle]::Regular)
$window.Controls.Add($Label1)
#Another Label
$Label2 = New-Object System.Windows.Forms.Label
$Label2.Location = New-Object System.Drawing.Size(10,80)
$Label2.Text = "Choose the Folder where the Installation should take place"
$Label2.Size = New-Object System.Drawing.Size(450,40)
$Label2.Font = New-Object System.Drawing.Font("Lucidia Console" , 10, [System.Drawing.FontStyle]::Regular)
$window.Controls.Add($Label2)
#Here is where I have Problems
#If $tempDir is empty it should put a default Path
#If not it should use $tempDir
$windowTextBox1 = New-Object System.Windows.Forms.TextBox
$windowTextBox1.Location = New-Object System.Drawing.Size(10,130)
$windowTextBox1.Size = New-Object System.Drawing.Size(300,150)
if($tempDir = "")
{
$windowTextBox1.Text = "C:\Program Files (x86)"
}
else
{
$windowTextBox1.Text = $tempDir
}
$window.Controls.Add($windowTextBox1)
$windowButton1 = New-Object System.Windows.Forms.Button
$windowButton1.Location = New-Object System.Drawing.Size(100,500)
$windowButton1.Size = New-Object System.Drawing.Size(75,50)
$windowButton1.Text = "Continue"
$windowButton1.Add_Click({
$global:status3 = "true"
$window.Dispose()
})
$window.Controls.Add($windowButton1)
#Add another Button
$windowButton2 = New-Object System.Windows.Forms.Button
$windowButton2.Location = New-Object System.Drawing.Size(300,500)
$windowButton2.Size = New-Object System.Drawing.Size(75,50)
$windowButton2.Text = "Exit"
$windowButton2.Add_Click({
$global:status3 = "false"
$window.Dispose()
})
$window.Controls.Add($windowButton2)
[void]$window.ShowDialog((New-Object System.Windows.Forms.Form -Property #{TopMost = $true }))
$status3
}
windowInstall1
The equal sign here is incorrect.
= is used for variable assignment
You should be using the -eq operator instead.
Correct
if ($tempDir -eq "") {
$windowTextBox1.Text = "C:\Program Files (x86)"
}
else {
$windowTextBox1.Text = $tempDir
}
Incorrect
# This is not a valid IF condition
if ($tempDir = "") {
$windowTextBox1.Text = "C:\Program Files (x86)"
}
else {
$windowTextBox1.Text = $tempDir
}
Additionally, if your $tempdir variable is $null instead of an empty script, this will be seen as if $tempdir is correctly populated. To cover both an empty string and a $null value, you can use [String]::IsNullOrEmpty($tempdir) in your condition statement.

How to handle label size and location in the GUI form using PowerShell?

I create a simple GUI. I want to execute the GUI in any resolution screen. In my work screen, it show picture 1. Once I execute it in other screen, the size label, position is different, showed in the picture 2.
Anyone can help me please how to handle this? Thank you so much.
Picture 1
Picture 2
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()
$Form = New-Object system.Windows.Forms.Form
$Form.ClientSize = '400,400'
$Form.text = "Form"
$Form.TopMost = $false
$form.AutoScale = $true
$form.AutoScaleMode = "Font"
$Form.FormBorderStyle = "FixedDialog"
$Groupbox1 = New-Object system.Windows.Forms.Groupbox
$Groupbox1.height = 362
$Groupbox1.width = 367
$Groupbox1.text = "Group Box"
$Groupbox1.location = New-Object System.Drawing.Point(15,21)
$Label1 = New-Object system.Windows.Forms.Label
$Label1.text = "Do you need handling the job?"
$Label1.AutoSize = $true
$Label1.width = 25
$Label1.height = 10
$Label1.location = New-Object System.Drawing.Point(92,82)
$Label1.Font = 'Microsoft Sans Serif,10'
$Button1 = New-Object system.Windows.Forms.Button
$Button1.text = "Yes"
$Button1.width = 60
$Button1.height = 30
$Button1.location = New-Object System.Drawing.Point(34,263)
$Button1.Font = 'Microsoft Sans Serif,10'
$Button2 = New-Object system.Windows.Forms.Button
$Button2.text = "No"
$Button2.width = 60
$Button2.height = 30
$Button2.location = New-Object System.Drawing.Point(268,263)
$Button2.Font = 'Microsoft Sans Serif,10'
$Form.controls.AddRange(#($Groupbox1))
$Groupbox1.controls.AddRange(#($Label1,$Button1,$Button2))
[void]$Form.ShowDialog()
updated
I try this
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()
$Form = New-Object system.Windows.Forms.Form
$Form.FormBorderStyle = "FixedDialog"
$Width = [System.Windows.Forms.Screen]::AllScreens.bounds.width
$Heigt = [System.Windows.Forms.Screen]::AllScreens.bounds.height
$Widht_Form = $Width[0] / 3.5
Write-Host "$Widht_Form"
$Heigt_Form = $Heigt[0] / 1.8
Write-Host "$Heigt_Form"
$Form.Width = $Widht_Form
$Form.Height = $Heigt_Form
$label1 = New-Object 'System.Windows.Forms.Label'
$Yes = New-Object 'System.Windows.Forms.Button'
$No = New-Object 'System.Windows.Forms.Button'
$Title = New-Object 'System.Windows.Forms.Label'
$timer1 = New-Object 'System.Windows.Forms.Timer'
$InitialFormWindowState = New-Object 'System.Windows.Forms.FormWindowState'
$Form_Load = {
$TotalTime = 10 #in seconds
$script:StartTime = (Get-Date).AddSeconds($TotalTime)
#Start the timer
$timer1.Start()
}
$Cancel_Click={
}
$timer1_Tick={
#Use Get-Date for Time Accuracy
[TimeSpan]$span = $script:StartTime - (Get-Date)
#Update the display
$Form.Text = $label1.Text = "{0:N0}" -f $span.TotalSeconds
if ($span.TotalSeconds -le 0) {
$timer1.Stop()
$Form.Close()
}
}
$Form_StateCorrection_Load=
{
$Form.WindowState = $InitialFormWindowState
}
$Form_Cleanup_FormClosed=
{
try
{
# $Cancel.remove_Click($Cancel_Click)
$Form.remove_Load($Form_Load)
$timer1.remove_Tick($timer1_Tick)
$Form.remove_Load($Form_StateCorrection_Load)
$Form.remove_FormClosed($Form_Cleanup_FormClosed)
}
catch [Exception]
{ }
}
$Form.SuspendLayout()
$Form.Controls.Add($label1)
$Form.Controls.Add($Yes)
$Form.Controls.Add($No)
$Form.Controls.Add($Title)
$Form.StartPosition = "CenterScreen"
$Form.BackColor = "#f6f6f6"
$Form.add_Load($Form_Load)
$label1.Font = 'Microsoft Sans Serif,20,style=Bold'
$Label1.ForeColor = "#176faa"
$label1.AutoSize = $true
$label1.width = 25
$label1.height = 10
$label1_height = $Heigt_Form / 2.5
$label1_width = $Widht_Form / 2.2
$label1.location = New-Object System.Drawing.Point($label1_width,$label1_height)
$Title.Text = "Do you need handling the job?"
$Title.ForeColor = "#176faa"
$Title.Font = 'Microsoft Sans Serif,16,style=Bold'
$Title.AutoSize = $true
$Title.width = 25
$Title.height = 10
$Title_height = $Heigt_Form / 5
$Title_width = $Widht_Form / 5
$Title.location = New-Object System.Drawing.Point($Title_width,$Title_height)
$Yes.AutoSize = $true
$Yes_height = $Heigt_Form * 0.7
$Yes_width = $Widht_Form / 8
$Yes.Location = New-Object System.Drawing.Size($Yes_width,$Yes_height)
$Yes.Size = New-Object System.Drawing.Size(90,35)
$Yes.Text = "Yes"
$Yes.Add_Click(
{
Write-Host "Call GUI Control"
Start-Sleep -s 1
$Form.Close()
}
)
$No.AutoSize = $true
$No_height = $Heigt_Form * 0.7
$No_width = ($Yes_width * 6) - 35
$No.Location = New-Object System.Drawing.Size($No_width,$No_height)
$No.Size = New-Object System.Drawing.Size(90,35)
# $No.BackColor = "#9fd5f3"
$No.Text = "No"
$No.Add_Click(
{
Write-Host "Continue the process"
$Form.Close()
}
)
$timer1.add_Tick($timer1_Tick)
$Form.ResumeLayout()
#Save the initial state of the form
$InitialFormWindowState = $Form.WindowState
#Init the OnLoad event to correct the initial state of the form
$Form.add_Load($Form_StateCorrection_Load)
#Clean up the control events
$Form.add_FormClosed($Form_Cleanup_FormClosed)
#Show the Form
return $Form.ShowDialog()
This way alson mesh up the label and button once execute in the different screen
You could use WMI Object for adjusting your objects in this form.
WMI objects returns key value information about your system.
This is how you can create the GUI that its objects' depends on the size of the screen:
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()
$sw = (Get-WmiObject -Class Win32_DesktopMonitor).ScreenWidth[1]
$sh = (Get-WmiObject -Class Win32_DesktopMonitor).ScreenHeight[1]
$Form = New-Object system.Windows.Forms.Form
$Form.ClientSize = "$($sw / 2),$($sh / 2)"
$Form.text = "Form"
$Form.TopMost = $false
$form.AutoScale = $true
$form.AutoScaleMode = "Font"
$Form.FormBorderStyle = "FixedDialog"
$Groupbox1 = New-Object system.Windows.Forms.Groupbox
$Groupbox1.height = $($sw / 2)
$Groupbox1.width = $($sh / 2)
$Groupbox1.text = "Group Box"
$Groupbox1.location = New-Object System.Drawing.Point(15,21)
$Label1 = New-Object system.Windows.Forms.Label
$Label1.text = "Do you need handling the job?"
$Label1.AutoSize = $true
$Label1.width = 25
$Label1.height = 10
$Label1.location = New-Object System.Drawing.Point($($sw / 16),$($sh / 12))
$Label1.Font = 'Microsoft Sans Serif,10'
$Button1 = New-Object system.Windows.Forms.Button
$Button1.text = "Yes"
$Button1.width = 60
$Button1.height = 30
$Button1.location = New-Object System.Drawing.Point($($sw / 20),$($sh / 3))
$Button1.Font = 'Microsoft Sans Serif,10'
$Button2 = New-Object system.Windows.Forms.Button
$Button2.text = "No"
$Button2.width = 60
$Button2.height = 30
$Button2.location = New-Object System.Drawing.Point($($sw / 6),$($sh / 3))
$Button2.Font = 'Microsoft Sans Serif,10'
$Form.controls.AddRange(#($Groupbox1))
$Groupbox1.controls.AddRange(#($Label1,$Button1,$Button2))
[void]$Form.ShowDialog()

How do I return errorlevel by handling GUI using PowerShell?

I have a GUI, the form will close automatically in 10s. But if we click the pause button, it also will close the form. I want to return the error level each process that I do. If the form closes automatically, it will return error level 10. but if we click the button pause, it will return error level 20. anyone can help, please.
this is my code.
function Timer_GUI {
[System.Windows.Forms.Application]::EnableVisualStyles()
$form1 = New-Object 'System.Windows.Forms.Form'
$label1 = New-Object 'System.Windows.Forms.Label'
$label2 = New-Object 'System.Windows.Forms.Label'
$Cancel = New-Object 'System.Windows.Forms.Button'
$timer1 = New-Object 'System.Windows.Forms.Timer'
$form1_Load = {
$TotalTime = 10 #in seconds
$script:StartTime = (Get-Date).AddSeconds($TotalTime)
#Start the timer
$timer1.Start()
}
$label1.Location = New-Object System.Drawing.Size(220,60)
$label1.Size = New-Object System.Drawing.Size(500,30)
$label2.Text = "The Process Will Continue in 10s"
$label2.Location = New-Object System.Drawing.Size(140,30)
$label2.Size = New-Object System.Drawing.Size(500,30)
$form1.SuspendLayout()
$form1.Controls.Add($label1)
$form1.Controls.Add($label2)
$form1.Controls.Add($Cancel)
$form1.Width = 500
$form1.Height = 200
$form1.StartPosition = "CenterScreen"
$form1.BackColor = "#e2e2e2"
$form1.add_Load($form1_Load)
$Cancel.DialogResult = 'Cancel'
$Cancel.Location = New-Object System.Drawing.Size(350,100)
$Cancel.Size = New-Object System.Drawing.Size(100,30)
$Cancel.Text = "Pause"
$Cancel.add_Click($Cancel_Click)
$timer1.add_Tick($timer1_Tick)
$form1.ResumeLayout()
#Show the Form
return $form1.ShowDialog()
exit 100
}
#Call the form
Timer_GUI | Out-Null
Not a GUI expert, but, here it is. Add a global variable $Global:formresult default set to 10, if button clicked set to 20.
The following 3 lines added or updated,
Add-Type -AssemblyName System.Windows.Forms
$Global:formresult = 10
$Cancel.add_Click({ $Global:formresult = 20 })
$Global:formresult
Full code, copy and paste from yours.
Add-Type -AssemblyName System.Windows.Forms
function Timer_GUI {
[System.Windows.Forms.Application]::EnableVisualStyles()
$form1 = New-Object 'System.Windows.Forms.Form'
$label1 = New-Object 'System.Windows.Forms.Label'
$label2 = New-Object 'System.Windows.Forms.Label'
$Cancel = New-Object 'System.Windows.Forms.Button'
$timer1 = New-Object 'System.Windows.Forms.Timer'
$Global:formresult = 10
$form1_Load = {
$TotalTime = 10 #in seconds
$script:StartTime = (Get-Date).AddSeconds($TotalTime)
#Start the timer
$timer1.Start()
}
$label1.Location = New-Object System.Drawing.Size(220,60)
$label1.Size = New-Object System.Drawing.Size(500,30)
$label2.Text = "The Process Will Continue in 10s"
$label2.Location = New-Object System.Drawing.Size(140,30)
$label2.Size = New-Object System.Drawing.Size(500,30)
$form1.SuspendLayout()
$form1.Controls.Add($label1)
$form1.Controls.Add($label2)
$form1.Controls.Add($Cancel)
$form1.Width = 500
$form1.Height = 200
$form1.StartPosition = "CenterScreen"
$form1.BackColor = "#e2e2e2"
$form1.add_Load($form1_Load)
$Cancel.DialogResult = 'Cancel'
$Cancel.Location = New-Object System.Drawing.Size(350,100)
$Cancel.Size = New-Object System.Drawing.Size(100,30)
$Cancel.Text = "Pause"
$Cancel.add_Click({ $Global:formresult = 20 })
$timer1.add_Tick($timer1_Tick)
$form1.ResumeLayout()
#Show the Form
return $form1.ShowDialog()
exit 100
}
#Call the form
Timer_GUI | Out-Null
$Global:formresult

Problems with Powershell and imageboxes

Trying to create a simple GUI Script using powershell to ping a TP ip address and display it with a simple Green/Red Image depending whether its on/off.
But for some reason, when hitting the Close button, I cant seem to remove the green/red dots.
The File contains:
Arial,172.0.0.0
Bodoni,172.0.0.0
Caslon,172.0.0.1
Script:
[void][System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[void][System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
$Form = New-Object System.Windows.Forms.Form
$Form.text = "Hardware Checks"
$Form.Size = New-Object System.Drawing.Size(600,600)
$arrayTPs = Get-Content -path "C:\Activity monitor\TPs.txt"
#Image Locations
$filered = (get-item 'C:\Activity monitor\red1.png')
$filegreen = (get-item 'C:\Activity monitor\green1.png')
$imgred = [System.Drawing.Image]::Fromfile($filered)
$imggreen = [System.Drawing.Image]::Fromfile($filegreen)
$outputBox = New-Object System.Windows.Forms.TextBox
$outputBox.Location = New-Object System.Drawing.Size(10,200)
$outputBox.Size = New-Object System.Drawing.Size(565,200)
$outputBox.MultiLine = $True
$outputBox.ScrollBars = "Vertical"
$Form.Controls.Add($outputBox)
$Button = New-Object System.Windows.Forms.Button
$Button.Location = New-Object System.Drawing.Size(450,30)
$Button.Size = New-Object System.Drawing.Size(75,75)
$Button.Text = "Check"
$Button.Add_Click({
$Form.controls.Remove($outputBox)
$Form.controls.refresh()
foreach($room in $arrayTPs){
#Get Name of room from array
$roomAlone = $room -split ","
$roomAlone[0]
$pictureBoxRed = new-object Windows.Forms.PictureBox
$pictureBoxGreen = new-object Windows.Forms.PictureBox
#Add room name to textbox
if($roomAlone[1] -eq "172.0.0.0"){
$pictureBoxGreen.Width = 10
$pictureBoxGreen.Height = 10
$pictureBoxGreen.Image = $imggreen
$pictureBoxGreen.Location = New-Object System.Drawing.Point(90,$counterpic)
$outputBox.controls.add($pictureBoxGreen)
$Form.Controls.Add($outputBox)}
else{
$pictureBoxRed.Width = 10
$pictureBoxRed.Height = 10
$pictureBoxRed.Image = $imgred
$pictureBoxRed.Location = New-Object System.Drawing.Point(90,$counterpic)
$outputBox.controls.add($pictureBoxRed)
$Form.Controls.Add($outputBox)
}
$counterpic = $Counterpic + 20
$counter = $Counter + 20
}})
$Form.Controls.Add($Button)
$cancelButton = New-Object System.Windows.Forms.Button
$cancelButton.Location = New-Object System.Drawing.Size(450,520)
$cancelButton.Size = New-Object System.Drawing.Size(75,25)
$cancelButton.Text = "Close"
$cancelButton.Add_Click({
$outputBox.controls.Remove($pictureBoxRed)
$outputBox.controls.Remove($pictureBoxGreen)
$outputBox.Controls.Equals($null)
$outputBox.controls.update()
$Form.Refresh()
foreach($room in $arrayTPs){
#Get Name of room from array
$roomAlone = $room -split ","
$roomAlone[0]
$pictureBoxRed = new-object Windows.Forms.PictureBox
$pictureBoxGreen = new-object Windows.Forms.PictureBox
}})
$Form.Controls.Add($cancelButton)
$Form.Add_Shown({$Form.Activate()})
[void] $Form.ShowDialog()

Powershell - List value of boxes checked

I have a Powershell scripts that creates 12 unique check boxes. Later in the script, it creates an email and enter information. I am trying to get the email to include the value of the boxes that are checked, and not the others. The variable name for each box is $CB1 up through $CB12.
Team,
Per ticket $SCTicket, $User is requesting access to GetPaid.
Details from RITM ticket $RITMTicket are below:
Access Type: $AccessType
Roll: $Roll
Responsibilities:
Thank You
Doing something such as $CB1,$CB2,$CB3, ect. does not look right, as it leaves a bunch of random commas. Putting each checkbox value on it's own line can work, but then there are potentially several blank lines for no reason.
If the user checks off, say, boxes 4, 7, and 10, how can I get just those values written??
Full code is below:
if ($startupvariables) { try {Remove-Variable -Name startupvariables -Scope Global -ErrorAction SilentlyContinue } catch { } }
New-Variable -force -name startupVariables -value ( Get-Variable | ForEach-Object { $_.Name } )
Write-Output "Don't close this window!"
Add-Type -AssemblyName PresentationCore,PresentationFramework
$ButtonType = [System.Windows.MessageBoxButton]::OKCancel
$MessageIcon = [System.Windows.MessageBoxImage]::Warning
$MessageTitle = "GetPaid - Access Request"
$MessageBody = "This script sends a GetPaid access request setup to xxxxxxxx and xxxxxx.`n`nTo use it, enter the below information:`n`n`n`tTicket Number`n`n`tUser's Email Address`n`n`tAccess Requested`n`n`nIf this is the script you want to use, click OK.`nIf not, click Cancel."
$Result = [System.Windows.MessageBox]::Show($MessageBody,$MessageTitle,$ButtonType,$MessageIcon)
if ($Result -eq "Cancel")
{
Exit-PSSession
}
else
{
[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic') | Out-Null
$Separator = ".", "#"
$SCTicket = [Microsoft.VisualBasic.Interaction]::InputBox("Enter the SCTask ticket number" , "Ticket Number")
$RITMTicket = [Microsoft.VisualBasic.Interaction]::InputBox("Enter the RITM ticket number" , "Ticket Number")
$UserID = [Microsoft.VisualBasic.Interaction]::InputBox("Enter the user's email address" , "User Email Address")
$User = $UserID.split($Separator)
$Firstname = $User[0].substring(0,1).toupper()+$User[0].substring(1).tolower()
$Lastname = $User[1].substring(0,1).toupper()+$User[1].substring(1).tolower()
$User = $Firstname, $Lastname
function Access_Type{
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
$Form = New-Object System.Windows.Forms.Form
$Form.width = 225
$Form.height = 215
$Form.Text = ”Select Type of Access"
$Font = New-Object System.Drawing.Font("Verdana",11)
$Form.Font = $Font
$MyGroupBox = New-Object System.Windows.Forms.GroupBox
$MyGroupBox.Location = '5,5'
$MyGroupBox.size = '190,125'
$RadioButton1 = New-Object System.Windows.Forms.RadioButton
$RadioButton1.Location = '20,20'
$RadioButton1.size = '120,30'
$RadioButton1.Checked = $false
$RadioButton1.Text = "Grant"
$RB1 = "Grant"
$RadioButton2 = New-Object System.Windows.Forms.RadioButton
$RadioButton2.Location = '20,50'
$RadioButton2.size = '120,30'
$RadioButton2.Checked = $false
$RadioButton2.Text = "Change"
$RB2 = "Change"
$RadioButton3 = New-Object System.Windows.Forms.RadioButton
$RadioButton3.Location = '20,80'
$RadioButton3.size = '120,30'
$RadioButton3.Checked = $false
$RadioButton3.Text = "Revoke"
$RB3 = "Revoke"
$OKButton = new-object System.Windows.Forms.Button
$OKButton.Location = '5,135'
$OKButton.Size = '90,35'
$OKButton.Text = 'OK'
$OKButton.DialogResult=[System.Windows.Forms.DialogResult]::OK
$CancelButton = new-object System.Windows.Forms.Button
$CancelButton.Location = '105,135'
$CancelButton.Size = '90,35'
$CancelButton.Text = "Cancel"
$CancelButton.Add_Click({$objForm.Close()})
$CancelButton.DialogResult=[System.Windows.Forms.DialogResult]::Cancel
$form.Controls.AddRange(#($MyGroupBox,$OKButton,$CancelButton))
$MyGroupBox.Controls.AddRange(#($Radiobutton1,$RadioButton2,$RadioButton3))
$form.AcceptButton = $OKButton
$form.CancelButton = $CancelButton
$form.Add_Shown({$form.Activate()})
$dialogResult = $form.ShowDialog()
if ($DialogResult -eq "OK")
{
if ($RadioButton1.Checked){$global:AccessType = $RB1}
elseif ($RadioButton2.Checked){$global:AccessType = $RB2}
elseif ($RadioButton3.Checked){$global:AccessType = $RB3}
}
elseif ($DialogResult -eq "Cancel")
{
break
}
}
Access_Type
function Access_Roll{
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
$Form = New-Object System.Windows.Forms.Form
$Form.width = 225
$Form.height = 180
$Form.Text = ”Select Type of Access"
$Font = New-Object System.Drawing.Font("Verdana",11)
$Form.Font = $Font
$MyGroupBox = New-Object System.Windows.Forms.GroupBox
$MyGroupBox.Location = '5,5'
$MyGroupBox.size = '190,90'
$RadioButton1 = New-Object System.Windows.Forms.RadioButton
$RadioButton1.Location = '20,20'
$RadioButton1.size = '130,30'
$RadioButton1.Checked = $false
$RadioButton1.Text = "User"
$RB11 = "User"
$RadioButton2 = New-Object System.Windows.Forms.RadioButton
$RadioButton2.Location = '20,50'
$RadioButton2.size = '130,30'
$RadioButton2.Checked = $false
$RadioButton2.Text = "Administrator"
$RB22 = "Administrator"
$OKButton = new-object System.Windows.Forms.Button
$OKButton.Location = '5,100'
$OKButton.Size = '90,35'
$OKButton.Text = 'OK'
$OKButton.DialogResult=[System.Windows.Forms.DialogResult]::OK
$CancelButton = new-object System.Windows.Forms.Button
$CancelButton.Location = '105,100'
$CancelButton.Size = '90,35'
$CancelButton.Text = "Cancel"
$CancelButton.Add_Click({$objForm.Close()})
$CancelButton.DialogResult=[System.Windows.Forms.DialogResult]::Cancel
$form.Controls.AddRange(#($MyGroupBox,$OKButton,$CancelButton))
$MyGroupBox.Controls.AddRange(#($Radiobutton1,$RadioButton2))
$form.AcceptButton = $OKButton
$form.CancelButton = $CancelButton
$form.Add_Shown({$form.Activate()})
$dialogResult = $form.ShowDialog()
if ($DialogResult -eq "OK")
{
if ($RadioButton1.Checked){$global:Roll = $RB11}
elseif ($RadioButton2.Checked){$global:Roll = $RB22}
}
elseif ($DialogResult -eq "Cancel")
{
break
}
}
Access_Roll
function Access_Responsibilities{
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
$Form = New-Object System.Windows.Forms.Form
$Form.width = 265
$Form.height = 510
$Form.Text = ”Select Type of Access"
$Font = New-Object System.Drawing.Font("Verdana",11)
$Form.Font = $Font
$MyGroupBox = New-Object System.Windows.Forms.GroupBox
$MyGroupBox.Location = '5,5'
$MyGroupBox.size = '230,420'
$Checkbox1 = New-Object System.Windows.Forms.Checkbox
$Checkbox1.Location = '20,20'
$Checkbox1.size = '200,25'
$Checkbox1.Checked = $false
$Checkbox1.Text = "Collections"
$CB1 = "Collections"
$Checkbox2 = New-Object System.Windows.Forms.Checkbox
$Checkbox2.Location = '20,45'
$Checkbox2.size = '200,25'
$Checkbox2.Checked = $false
$Checkbox2.Text = "Credit Management"
$CB2 = "Credit Management"
$Checkbox3 = New-Object System.Windows.Forms.Checkbox
$Checkbox3.Location = '20,70'
$Checkbox3.size = '200,25'
$Checkbox3.Checked = $false
$Checkbox3.Text = "Cash Application"
$CB3 = "Cash Application"
$Checkbox4 = New-Object System.Windows.Forms.Checkbox
$Checkbox4.Location = '20,95'
$Checkbox4.size = '200,25'
$Checkbox4.Checked = $false
$Checkbox4.Text = "Sales and Service User"
$CB4 = "Sales and Service User"
$Checkbox5 = New-Object System.Windows.Forms.Checkbox
$Checkbox5.Location = '20,115'
$Checkbox5.size = '200,50'
$Checkbox5.Checked = $false
$Checkbox5.Text = "Controller (specify plant below)"
$CB5 = "Controller (specify plant below)"
$Checkbox6 = New-Object System.Windows.Forms.Checkbox
$Checkbox6.Location = '20,160'
$Checkbox6.size = '200,50'
$Checkbox6.Checked = $false
$Checkbox6.Text = "Plant (specify plant below)"
$CB6 = "Plant (specify plant below)"
$Checkbox7 = New-Object System.Windows.Forms.Checkbox
$Checkbox7.Location = '20,205'
$Checkbox7.size = '200,25'
$Checkbox7.Checked = $false
$Checkbox7.Text = "Sales"
$CB7 = "Sales"
$Checkbox8 = New-Object System.Windows.Forms.Checkbox
$Checkbox8.Location = '20,230'
$Checkbox8.size = '200,25'
$Checkbox8.Checked = $false
$Checkbox8.Text = "Pricing"
$CB8 = "Pricing"
$Checkbox9 = New-Object System.Windows.Forms.Checkbox
$Checkbox9.Location = '20,255'
$Checkbox9.size = '200,25'
$Checkbox9.Checked = $false
$Checkbox9.Text = "Warranty"
$CB9 = "Warranty"
$Checkbox10 = New-Object System.Windows.Forms.Checkbox
$Checkbox10.Location = '20,280'
$Checkbox10.size = '200,25'
$Checkbox10.Checked = $false
$Checkbox10.Text = "GL Accountant"
$CB10 = "GL Accountant"
$Checkbox11 = New-Object System.Windows.Forms.Checkbox
$Checkbox11.Location = '20,300'
$Checkbox11.size = '200,50'
$Checkbox11.Checked = $false
$Checkbox11.Text = "Mexico Billing (specific plant below)"
$CB11 = "Mexico Billing (specific plant below)"
$Checkbox12 = New-Object System.Windows.Forms.Checkbox
$Checkbox12.Location = '20,340'
$Checkbox12.size = '200,75'
$Checkbox12.Checked = $false
$Checkbox12.Text = "Non Problem Owner (To be used for Inquiry Only)"
$CB12 = "Non Problem Owner (To be used for Inquiry Only)"
$OKButton = new-object System.Windows.Forms.Button
$OKButton.Location = '10,430'
$OKButton.Size = '90,35'
$OKButton.Text = 'OK'
$OKButton.DialogResult=[System.Windows.Forms.DialogResult]::OK
$CancelButton = new-object System.Windows.Forms.Button
$CancelButton.Location = '110,430'
$CancelButton.Size = '90,35'
$CancelButton.Text = "Cancel"
$CancelButton.Add_Click({$objForm.Close()})
$CancelButton.DialogResult=[System.Windows.Forms.DialogResult]::Cancel
$form.Controls.AddRange(#($MyGroupBox,$OKButton,$CancelButton))
$MyGroupBox.Controls.AddRange(#($Checkbox1,$Checkbox2,$Checkbox3,$Checkbox4,$Checkbox5,$Checkbox6,$Checkbox7,$Checkbox8,$Checkbox9,$Checkbox10,$Checkbox11,$Checkbox12))
$form.AcceptButton = $OKButton
$form.CancelButton = $CancelButton
$form.Add_Shown({$form.Activate()})
$dialogResult = $form.ShowDialog()
if ($DialogResult -eq "OK")
{
if ($Checkbox1.Checked){$global:AccessResponsibilities = $CB1}
elseif ($Checkbox2.Checked){$global:AccessResponsibilities = $CB2}
elseif ($Checkbox2.Checked){$global:AccessResponsibilities = $CB3}
elseif ($Checkbox2.Checked){$global:AccessResponsibilities = $CB4}
elseif ($Checkbox2.Checked){$global:AccessResponsibilities = $CB5}
elseif ($Checkbox2.Checked){$global:AccessResponsibilities = $CB6}
elseif ($Checkbox2.Checked){$global:AccessResponsibilities = $CB7}
elseif ($Checkbox2.Checked){$global:AccessResponsibilities = $CB8}
elseif ($Checkbox2.Checked){$global:AccessResponsibilities = $CB9}
elseif ($Checkbox2.Checked){$global:AccessResponsibilities = $CB10}
elseif ($Checkbox2.Checked){$global:AccessResponsibilities = $CB11}
elseif ($Checkbox2.Checked){$global:AccessResponsibilities = $CB12}
}
elseif ($DialogResult -eq "Cancel")
{
break
}
}
Access_Responsibilities
function Read-MultiLineInputBoxDialog([string]$Message, [string]$WindowTitle, [string]$DefaultText)
{
Add-Type -AssemblyName System.Drawing
Add-Type -AssemblyName System.Windows.Forms
$label = New-Object System.Windows.Forms.Label
$label.Location = New-Object System.Drawing.Size(10,10)
$label.Size = New-Object System.Drawing.Size(280,20)
$label.AutoSize = $true
$label.Text = $Message
$textBox = New-Object System.Windows.Forms.TextBox
$textBox.Location = New-Object System.Drawing.Size(10,40)
$textBox.Size = New-Object System.Drawing.Size(575,200)
$textBox.AcceptsReturn = $true
$textBox.AcceptsTab = $false
$textBox.Multiline = $true
$textBox.ScrollBars = 'Both'
$textBox.Text = $DefaultText
$okButton = New-Object System.Windows.Forms.Button
$okButton.Location = New-Object System.Drawing.Size(415,250)
$okButton.Size = New-Object System.Drawing.Size(75,25)
$okButton.Text = "OK"
$okButton.Add_Click({ $form.Tag = $textBox.Text; $form.Close() })
$cancelButton = New-Object System.Windows.Forms.Button
$cancelButton.Location = New-Object System.Drawing.Size(510,250)
$cancelButton.Size = New-Object System.Drawing.Size(75,25)
$cancelButton.Text = "Cancel"
$CancelButton.Add_Click({$objForm.Close()})
$cancelButton.Add_Click({ $form.Tag = $null; $form.Close() })
$form = New-Object System.Windows.Forms.Form
$form.Text = $WindowTitle
$form.Size = New-Object System.Drawing.Size(610,320)
$form.FormBorderStyle = 'FixedSingle'
$form.StartPosition = "CenterScreen"
$form.AutoSizeMode = 'GrowAndShrink'
$form.Topmost = $True
$form.AcceptButton = $okButton
$form.CancelButton = $cancelButton
$form.ShowInTaskbar = $true
$form.Controls.Add($label)
$form.Controls.Add($textBox)
$form.Controls.Add($okButton)
$form.Controls.Add($cancelButton)
$form.Add_Shown({$form.Activate()})
$form.ShowDialog() > $null
return $form.Tag
}
$global:Comments = Read-MultiLineInputBoxDialog -Message "Enter any comments from the ticket" -WindowTitle "Comments"
$Username = [System.Environment]::UserName
$subject = "Ticket $SCTicket - $User's GetPaid Access Request"
$body = "
Team,
Per ticket $SCTicket, $User is requesting access to GetPaid.
Details from RITM ticket $RITMTicket are below:
Access Type: $AccessType
Roll: $Roll
Responsibilities:
Comments: $Comments
Thank You,
"
$ButtonType = [System.Windows.MessageBoxButton]::YesNo
$MessageIcon = [System.Windows.MessageBoxImage]::Warning
$MessageTitle = "GetPaid - Access Request"
$MessageBody = "The information you have entered is show below:`n`n`nTicket Number: $Ticket`n`nUser's Email Address: $UserID`n`nAccess Type: $AccessType`n`nRoll: $Roll`n`nResponsibilities: `n`nComments: $Comments`n`n`nIf you would like to send the email, click Yes.`nOtherwise, click No."
$Result = [System.Windows.MessageBox]::Show($MessageBody,$MessageTitle,$ButtonType,$MessageIcon)
if ($Result -eq "No")
{
Exit-PSSession
}
else
{
Send-MailMessage -To "<$Username#xxx.com>" -bcc "<$Username#xxx.com>" -from "<itservicedesk#xxx.com>" -Subject $subject -SmtpServer "mailrelay.xxx.com" -body $body
}
}
Function Clean-Memory {
Get-Variable |
Where-Object { $startupVariables -notcontains $_.Name } |
ForEach-Object {
try { Remove-Variable -Name "$($_.Name)" -Force -Scope "global" -ErrorAction SilentlyContinue -WarningAction SilentlyContinue}
catch { }
}
}
Here's a procedural way to retrieve the matching values.
First, we retrieve all variables with $Checkbox in the name, then grab only ones with a CheckState value. Then, for each, we substring out the number and append that to the string CB to retrieve the value of the corresponding $CB# variable.
Then we store that in an array list and join with a comma at the end, for the output you see below.
$properties = New-object System.Collections.ArrayList
$CheckedBoxes = Get-Variable checkbox* | Where-object {$_.Value.CheckState -eq 'Checked'}
ForEach ($Checked in $CheckedBoxes){
$CBNumber = $CheckBox.Name.Split('Checkbox')[-1]
$Properties.Add((Get-variable "CB$CBNumber").Value) | out-null
}
$Properties -join ','
>GL Accountant, Pricing, Credit Management
All of this code should be used where you have this code today:
if ($Checkbox1.Checked){$global:AccessResponsibilities = $CB1}
#Insert new code here <---------------
elseif ($Checkbox2.Checked){$global:AccessResponsibilities = $CB2}
elseif ($Checkbox2.Checked){$global:AccessResponsibilities = $CB3}
Just from a preliminary look through the code and the question that you are asking, this is what I have come up with.
Basically, if the checkbox is checked, it appends the $CB(1-12) string to the $global:AccessResponsibilities and then in your email just reference $global:AccessResponsibilities and it should have all the strings of the checkboxes that were checked
if ($Checkbox1.Checked){$global:AccessResponsibilities += $CB1 + "`r`n"}
if ($Checkbox2.Checked){$global:AccessResponsibilities += $CB2 + "`r`n"}
if ($Checkbox3.Checked){$global:AccessResponsibilities += $CB3 + "`r`n"}
if ($Checkbox4.Checked){$global:AccessResponsibilities += $CB4 + "`r`n"}
if ($Checkbox5.Checked){$global:AccessResponsibilities += $CB5 + "`r`n"}
if ($Checkbox6.Checked){$global:AccessResponsibilities += $CB6 + "`r`n"}
if ($Checkbox7.Checked){$global:AccessResponsibilities += $CB7 + "`r`n"}
if ($Checkbox8.Checked){$global:AccessResponsibilities += $CB8 + "`r`n"}
if ($Checkbox9.Checked){$global:AccessResponsibilities += $CB9 + "`r`n"}
if ($Checkbox10.Checked){$global:AccessResponsibilities += $CB10 + "`r`n"}
if ($Checkbox11.Checked){$global:AccessResponsibilities += $CB11 + "`r`n"}
if ($Checkbox12.Checked){$global:AccessResponsibilities += $CB12 + "`r`n"}
heres a small loop that should work for HeedfulCrayon's answer.
foreach ($i in 1..12)
{
if ((Get-Variable -Name Checkbox$i).Value.Checked)
{
$Global:AccessResponsibilities += ((Get-Variable -Name CB$i).Value + "`r`n")
}
}