Custom button logic Powershell - powershell

I'm creating a script for silent installation of some applications, with a choice box!
I also created a custom button, but I need help to create the button logic.
I thank everyone!
Add-Type -AssemblyName System.Windows.Forms
$Form = New-Object System.Windows.Forms.Form
$Form.Text ='Softwares Padrão'
$Form.Width = 500
$Form.Height = 300
$Form.AutoSize = $true
$Painel = New-Object System.Windows.Forms.Panel
$Painel.Height = 200
$Painel.Width = 154
$Painel.Location = New-Object System.Drawing.Point(24,45)
$Painel.BackColor = [System.Drawing.ColorTranslator]::FromHtml("#ffffff")
$CheckBox_Selec = New-Object System.Windows.Forms.CheckBox
$CheckBox_Selec.Text = "Selecionar Todos"
$CheckBox_Selec.Width = 200
$CheckBox_Selec.Height = 20
$CheckBox_Selec.AutoSize = $true
$CheckBox_Selec.Location = New-Object System.Drawing.Size(34, 20)
$CheckBox_Selec.Checked = $false
$Form.Controls.Add($CheckBox_Selec)
$CheckBox_Selec1 = New-Object System.Windows.Forms.CheckBox
$CheckBox_Selec1.Text = "Winrar"
$CheckBox_Selec1.AutoSize = $true
$CheckBox_Selec1.Location = New-Object System.Drawing.Size(10, 10)
$CheckBox_Selec1.Checked = $false
$Form.Controls.Add($CheckBox_Selec1)
$CheckBox_Selec2 = New-Object System.Windows.Forms.CheckBox
$CheckBox_Selec2.Text = "Java"
$CheckBox_Selec2.AutoSize = $true
$CheckBox_Selec2.Location = New-Object System.Drawing.Size(10, 30)
$CheckBox_Selec2.Checked = $false
$Form.Controls.Add($CheckBox_Selec2)
$CheckBox_Selec3 = New-Object System.Windows.Forms.CheckBox
$CheckBox_Selec3.Text = "Adobe Reader"
$CheckBox_Selec3.AutoSize = $true
$CheckBox_Selec3.Location = New-Object System.Drawing.Size(10, 50)
$CheckBox_Selec3.Checked = $false
$Form.Controls.Add($CheckBox_Selec3)
$CheckBox_Selec4 = New-Object System.Windows.Forms.CheckBox
$CheckBox_Selec4.Text = "Office 2013x32"
$CheckBox_Selec4.AutoSize = $true
$CheckBox_Selec4.Location = New-Object System.Drawing.Size(10, 70)
$CheckBox_Selec4.Checked = $false
$Form.Controls.Add($CheckBox_Selec4)
$CheckBox_Selec5 = New-Object System.Windows.Forms.CheckBox
$CheckBox_Selec5.Text = "Google Chrome"
$CheckBox_Selec5.AutoSize = $true
$CheckBox_Selec5.Location = New-Object System.Drawing.Size(10, 90)
$CheckBox_Selec5.Checked = $false
$Form.Controls.Add($CheckBox_Selec5)
$CheckBox_Selec6 = New-Object System.Windows.Forms.CheckBox
$CheckBox_Selec6.Text = "CutePdf"
$CheckBox_Selec6.AutoSize = $true
$CheckBox_Selec6.Location = New-Object System.Drawing.Size(10, 110)
$CheckBox_Selec6.Checked = $false
$Form.Controls.Add($CheckBox_Selec6)
$CheckBox_Selec7 = New-Object System.Windows.Forms.CheckBox
$CheckBox_Selec7.Text = "Oracle11gx32"
$CheckBox_Selec7.AutoSize = $true
$CheckBox_Selec7.Location = New-Object System.Drawing.Size(10, 130)
$CheckBox_Selec7.Checked = $false
$Form.Controls.Add($CheckBox_Selec7)
$CheckBox_Selec8 = New-Object System.Windows.Forms.CheckBox
$CheckBox_Selec8.Text = "Piramide"
$CheckBox_Selec8.AutoSize = $true
$CheckBox_Selec8.Location = New-Object System.Drawing.Size(10, 150)
$CheckBox_Selec8.Checked = $false
$Form.Controls.Add($CheckBox_Selec8)
$CheckBox_Selec9 = New-Object System.Windows.Forms.CheckBox
$CheckBox_Selec9.Text = "Rh3"
$CheckBox_Selec9.AutoSize = $true
$CheckBox_Selec9.Location = New-Object System.Drawing.Size(10, 170)
$CheckBox_Selec9.Checked = $false
$Form.Controls.Add($CheckBox_Selec9)
$Button1 = New-Object System.Windows.Forms.Button
$Button1.Text = "Instalar"
$Button1.AutoSize = $true
$Button1.Width = 60
$Button1.Height = 30
$Button1.Location = New-Object System.Drawing.Point(290,215)
$Button1.Font = New-Object System.Drawing.Font('Microsoft Sans Serif', 10)
$Button1.Enabled = $false
$Form.Controls.Add($Button1)
$Form.controls.AddRange(#($Painel,$CheckBox_Selec,$Button1))
$Painel.controls.AddRange(#($CheckBox_Selec1,$CheckBox_Selec2,$CheckBox_Selec3,$CheckBox_Selec4,$CheckBox_Selec5,$CheckBox_Selec6,$CheckBox_Selec7,$CheckBox_Selec8,$CheckBox_Selec9))
$CheckBox_Selec.Add_CheckStateChanged({$CheckBox_Selec1.Checked = $CheckBox_Selec.Checked})
$CheckBox_Selec.Add_CheckStateChanged({$CheckBox_Selec2.Checked = $CheckBox_Selec.Checked})
$CheckBox_Selec.Add_CheckStateChanged({$CheckBox_Selec3.Checked = $CheckBox_Selec.Checked})
$CheckBox_Selec.Add_CheckStateChanged({$CheckBox_Selec4.Checked = $CheckBox_Selec.Checked})
$CheckBox_Selec.Add_CheckStateChanged({$CheckBox_Selec5.Checked = $CheckBox_Selec.Checked})
$CheckBox_Selec.Add_CheckStateChanged({$CheckBox_Selec6.Checked = $CheckBox_Selec.Checked})
$CheckBox_Selec.Add_CheckStateChanged({$CheckBox_Selec7.Checked = $CheckBox_Selec.Checked})
$CheckBox_Selec.Add_CheckStateChanged({$CheckBox_Selec8.Checked = $CheckBox_Selec.Checked})
$CheckBox_Selec.Add_CheckStateChanged({$CheckBox_Selec9.Checked = $CheckBox_Selec.Checked})
Function Test-AnyButtonCheked {
if (
$CheckBox_Selec.Checked -or
$CheckBox_Selec1.Checked -or
$CheckBox_Selec2.Checked -or
$CheckBox_Selec3.Checked -or
$CheckBox_Selec4.Checked -or
$CheckBox_Selec5.Checked -or
$CheckBox_Selec6.Checked -or
$CheckBox_Selec7.Checked -or
$CheckBox_Selec8.Checked -or
$CheckBox_Selec9.Checked
) {
$Button1.Enabled = $true
}
else {
$Button1.Enabled = $false
}
}
$CheckBox_Selec.Add_CheckedChanged( { Test-AnyButtonCheked})
$CheckBox_Selec1.Add_CheckedChanged( { Test-AnyButtonCheked})
$CheckBox_Selec2.Add_CheckedChanged( { Test-AnyButtonCheked})
$CheckBox_Selec3.Add_CheckedChanged( { Test-AnyButtonCheked})
$CheckBox_Selec4.Add_CheckedChanged( { Test-AnyButtonCheked})
$CheckBox_Selec5.Add_CheckedChanged( { Test-AnyButtonCheked})
$CheckBox_Selec6.Add_CheckedChanged( { Test-AnyButtonCheked})
$CheckBox_Selec7.Add_CheckedChanged( { Test-AnyButtonCheked})
$CheckBox_Selec8.Add_CheckedChanged( { Test-AnyButtonCheked})
$CheckBox_Selec9.Add_CheckedChanged( { Test-AnyButtonCheked})
[void] $Form.ShowDialog()
This is my structure, I know it's not in the best practices but it's running very well!
I would like to install the applications according to the checkbox chosen by the user!
I would like to configure the button so that if the user selects, "install all" he will install all applications or just the chosen checkbox.
Thank you for your help!

Related

Need help running functions in parallel

I've written a little GUI script that gathers files from specified location,
groups files based on hashes, filenames comparison and gathers zero length files.
Each after a button press. What I'd like to do is to combine those button presses into one.
I tried converting button scriptblock to a function. And that works for single function.
I understand that I need to run those functions in parallel but I believe that there is an issue with scoping (at least). I tried using workflow parallel (I get Write Error) and assigning each scriptblock to a background-job, but I think that because the jobs are then nested, everything is treated as a string object (double hop?).
I know these can be run in simultaneously (I pressed each after another and the timer as well as progressbar appeared). So any help would be appreciated.
Add-Type -AssemblyName System.Windows.Forms
add-type -AssemblyName system.drawing
[System.Windows.Forms.Application]::EnableVisualStyles()
$window = New-Object system.Windows.Forms.Form
$window.Height = 450
$window.Width = 400
$window.StartPosition = "CenterScreen"
$window.Font = New-Object System.Drawing.Font("Calibri",11,[System.Drawing.FontStyle]::Bold )
$window.Text = "Duplicates 2.0"
$location = New-Object System.Drawing.Point(10, 10)
$size = New-Object System.Drawing.Size(365, 20)
$ProgressBar1 = New-Object System.Windows.Forms.ProgressBar
$ProgressBar1.Location = New-Object System.Drawing.Point(10, 10)
$ProgressBar1.Size = New-Object System.Drawing.Size(365, 20)
$ProgressBar1.Style = "Marquee"
$ProgressBar1.MarqueeAnimationSpeed = 20
$ProgressBar1.UseWaitCursor = $true
$ProgressBar1.Visible = $false
$button = New-Object System.Windows.Forms.Button
$button.size = New-Object system.drawing.size #(100,50)
$button.Location = New-Object System.Drawing.Point(20, 90)
$button.TextAlign = 'middlecenter'
$button.Text = "Gather `r`n Files"
$window.Controls.add($button)
$label = New-Object System.Windows.Forms.Label
$label.Size = New-Object System.Drawing.Size #(100, 50)
$label.Location = New-Object System.Drawing.Point (135, 90)
$label.BorderStyle = 'Fixed3D'
$label.ForeColor = 'green'
$label.TextAlign = 'middlecenter'
$window.Controls.Add($label)
$check_font = New-Object System.Drawing.Font ("Calibri",20,[System.Drawing.FontStyle]::Bold)
$label3 = New-Object system.windows.forms.label
$label3.Size = New-Object System.Drawing.Size (100, 50)
$label3.Location = New-Object system.drawing.point (250 ,90)
$label3.BorderStyle = 'Fixed3D'
$label3.ForeColor = 'green'
$label3.TextAlign = 'middlecenter'
$label3.Font = $check_font
$window.Controls.Add($label3)
$label4 = New-Object system.windows.forms.label
$label4.Location = New-Object System.Drawing.Point(9, 5)
$label4.Size = New-Object System.Drawing.Size(365, 20)
$label4.TextAlign = 'middlecenter'
$label4.Text = 'COMPLETED!!!'
$label4.Visible = $false
$label4.BackColor = [System.Drawing.Color]::FromArgb(6, 176, 37)
$window.Controls.Add($label4)
$timer = New-Object System.Windows.Forms.Timer
$timer.Interval = 1000
$timer.add_Tick({$script:time2 =((get-date)-$script:time1).ToString("hh\:mm\:ss")
$label.text = $script:time2
})
function rectangle {
$brush = New-Object System.Drawing.SolidBrush ([System.Drawing.Color]::FromArgb(6, 176, 37))
$groupboxGraphics.FillRectangle($brush, 5, 10, 365, 20)
}
$button.add_Click(
{
$Script:time1 = Get-Date
$timer.start()
$ProgressBar1.BringToFront()
$ProgressBar1.Show()
$this.Enabled = $false
$job = Start-Job -ScriptBlock {
Get-ChildItem -File -recurse "Z:\Merck_Peleton" -ErrorAction SilentlyContinue
}
while($job.State -eq 'Running') {
[System.Windows.Forms.Application]::DoEvents()
}
$job|Wait-job|Receive-Job|Set-Variable -Name PORNO -Scope Script
$groupbox.Controls.Remove($ProgressBar1)
$this.Enabled = $true
$timer.stop()
$label3.Text = [char]8730
$groupbox.Refresh()
rectangle
$label4.Show()
}
)
$label5 = New-Object system.windows.forms.label
$label5.Location = New-Object System.Drawing.Point(9, 35)
$label5.Size = New-Object System.Drawing.Size(365, 20)
$label5.TextAlign = 'middlecenter'
$label5.Text = 'COMPLETED!!!'
$label5.Visible = $false
$label5.BackColor = [System.Drawing.Color]::FromArgb(6, 176, 37)
$window.Controls.Add($label5)
$button2 = New-Object System.Windows.Forms.Button
$button2.Height = 50
$button2.Width = ($window.Width * 0.85)
$button2.Location = New-Object System.Drawing.Point (20, 350)
$button2.TextAlign = 'middlecenter'
$button2.Text = 'Exit'
$window.Controls.Add($button2)
$ProgressBar2 = New-Object System.Windows.Forms.ProgressBar
$ProgressBar2.Location = New-Object System.Drawing.Point(8, 35)
$ProgressBar2.Size = New-Object System.Drawing.Size(365, 20)
$ProgressBar2.Style = "Marquee"
$ProgressBar2.MarqueeAnimationSpeed = 20
$ProgressBar2.UseWaitCursor = $true
$ProgressBar2.Visible = $false
$button2.add_Click({$window.Close()})
$button3 = new-object System.Windows.Forms.Button
$button3.Size = New-Object System.Drawing.Size (100, 50)
$button3.Location = New-Object System.Drawing.Point (20, 150)
$button3.Text = "Hashes"
$window.Controls.Add($button3)
$timer2 = New-Object System.Windows.Forms.Timer
$timer2.Interval = 1000
$timer2.add_Tick({$script:time4 =((get-date)-$script:time3).ToString("hh\:mm\:ss")
$label2.text = $script:time4
})
$button3.add_Click({
$Script:time3 = Get-Date
$timer2.start()
$ProgressBar2.BringToFront()
$ProgressBar2.Show()
$this.Enabled = $false
$job2 = start-job -scriptblock {
$using:PORNO|Get-FileHash -Algorithm MD5
}
while($job2.State -eq 'Running') {
[System.Windows.Forms.Application]::DoEvents()
}
$job2|Wait-job|Receive-Job|Set-Variable -Name DUSZNO -Scope Script
$ProgressBar2.Hide()
$groupbox.Controls.Remove($ProgressBar2)
$groupbox.Refresh()
$label5.show()
$this.Enabled = $true
$timer2.stop()
$label6.Text = [char]8730
})
$label2 = New-Object System.Windows.Forms.Label
$label2.size = New-Object System.Drawing.Size (100, 50)
$label2.Location = New-Object System.Drawing.Point (135, 150)
$label2.BorderStyle = 'Fixed3D'
$label2.ForeColor = 'green'
$label2.TextAlign = 'middlecenter'
$window.Controls.Add($label2)
$label6 = new-object System.Windows.Forms.Label
$label6.Size = New-Object System.Drawing.Size (100, 50)
$label6.Location = New-Object System.Drawing.Point (250, 150)
$label6.ForeColor = 'green'
$label6.TextAlign = 'middlecenter'
$label6.Font = $check_font
$label6.BorderStyle = 'Fixed3D'
$window.Controls.Add($label6)
$button4 = New-Object System.Windows.Forms.Button
$button4.size = New-Object System.Drawing.Size (100, 50)
$button4.Location = New-Object System.Drawing.Point (20, 210)
$button4.Text = "Extensions"
$window.Controls.Add($button4)
$timer3 = New-Object System.Windows.Forms.Timer
$timer3.Interval = 1000
$timer3.add_Tick({$script:time6 =((get-date)-$script:time5).ToString("hh\:mm\:ss")
$label7.text = $script:time6
})
$button4.Add_Click({
$Script:time5 = Get-Date
$timer3.start()
$job3 = start-job -scriptblock {
$using:PORNO|
Select #{l="Algorithm";e={}}, #{l="Hash";e={}}, #{l='File';e={$_.PSChildName}}, #{l='Compare Filename';e={$_.BaseName.replace('_','*').replace(' ','*').replace('-','*')}}, #{l="Path";e={$path + $_.PSParentPath.Substring(38,$_.PSParentPath.Length-38)}}, #{l="Link";e={$_.FullName}}, #{l="Extension";e={$_.Extension}}|
group -Property 'Compare Filename'|
Where {#($_.Group.Extension |Sort -Unique).Count -ge 2}
}
while($job3.State -eq 'Running') {
[System.Windows.Forms.Application]::DoEvents()
}
$job3|Wait-job|Receive-Job|Set-Variable -Name CIASNO -Scope Script
$timer3.Stop()
$label8.Text = [char]8730
})
$label7 = New-Object System.Windows.Forms.Label
$label7.Size = New-Object System.Drawing.Size (100, 50)
$label7.Location = New-Object System.Drawing.Point (135, 210)
$label7.BorderStyle = 'Fixed3D'
$label7.ForeColor = 'green'
$label7.TextAlign = 'middlecenter'
$window.Controls.Add($label7)
$label8 = New-Object System.Windows.Forms.Label
$label8.Size = New-Object System.Drawing.Size (100, 50)
$label8.Location = New-Object System.Drawing.Point (250, 210)
$label8.BorderStyle = 'Fixed3D'
$label8.ForeColor = 'green'
$label8.Font = $check_font
$label8.TextAlign = 'middlecenter'
$window.Controls.Add($label8)
$timer4 = new-object System.Windows.Forms.Timer
$timer4.add_Tick({$script:time8 =((get-date)-$script:time7).ToString("hh\:mm\:ss")
$label9.text = $script:time8
})
$button5 = New-Object System.Windows.Forms.Button
$button5.size = New-Object System.Drawing.Size (100, 50)
$button5.Location = New-Object System.Drawing.Point (20, 270)
$button5.Text = "Zero Length"
$window.Controls.Add($button5)
$button5.Add_Click({
$Script:time7 = Get-Date
$timer4.start()
$job4 = start-job -scriptblock {
$using:PORNO|
? {$_.Length -eq 0}|
Select #{l='File';e={$_.PSChildName}}, Length, Directory, FullName
}
while($job4.State -eq 'Running') {
[System.Windows.Forms.Application]::DoEvents()
}
$job4|Wait-job|Receive-Job|Set-Variable -Name PRZYJEMNIE -Scope Script
$timer4.Stop()
if ($PRZYJEMNIE -eq $null){$label10.Text = "EMPTY"}else{$label10.Text = [char]8730}
})
$label9 = New-Object System.Windows.Forms.Label
$label9.Size = New-Object System.Drawing.Size (100, 50)
$label9.Location = New-Object System.Drawing.Point (135, 270)
$label9.BorderStyle = 'Fixed3D'
$label9.ForeColor = 'green'
$label9.TextAlign = 'middlecenter'
$window.Controls.Add($label9)
$label10 = New-Object System.Windows.Forms.Label
$label10.Size = New-Object System.Drawing.Size (100,50)
$label10.Location = New-Object System.Drawing.Point (250, 270)
$label10.BorderStyle = 'Fixed3D'
$label10.ForeColor = 'green'
$label10.Font = $check_font
$label10.TextAlign = 'middlecenter'
$window.Controls.Add($label10)
$groupbox = New-Object System.Windows.Forms.GroupBox
$groupbox.size = New-Object system.drawing.size #(377, 413)
$groupbox.Location = New-Object System.Drawing.point (4, -5)
$groupboxGraphics = $groupbox.CreateGraphics()
$groupbox.Controls.Add($ProgressBar1)
$groupbox.Controls.Add($ProgressBar2)
$window.Controls.Add($groupbox)
$window.ShowDialog()|out-null
For anyone interested on how I solved this. Since I could not succesfully call two functions at the same time and nested jobs did not work out for me I turned to other solution. Timer since the tick interval can be controlled and on every tick a scriptblock or function can be executed I created additional two timers (will clean up and optimise later) that chceck .status of jobs I wanted to be run in background. Upon fining .state to be finished they execute functions that previously were later parts of initial button script. I need to work on progressbars locations relative to 'completed' labels since they don't match-up exactly. But for the most part I achieved what I wanted.
UPDATED CODE
Add-Type -AssemblyName System.Windows.Forms
Add-type -AssemblyName system.drawing
[System.Windows.Forms.Application]::EnableVisualStyles()
Function TEST_1
{
$Script:time1 = Get-Date
$timer.start()
$ProgressBar1.BringToFront()
$ProgressBar1.Show()
$this.Enabled = $false
$job = Start-Job -ScriptBlock {
Get-ChildItem -File -recurse $env:HOMEPATH -ErrorAction SilentlyContinue
}
while($job.State -eq 'Running') {
[System.Windows.Forms.Application]::DoEvents()
}
$job|Wait-job|Receive-Job|Set-Variable -Name PORNO -Scope Script
$groupbox.Controls.Remove($ProgressBar1)
$this.Enabled = $true
$timer.stop()
$label3.Text = [char]8730
$groupbox.Refresh()
$label4.Show()
}
$window = New-Object system.Windows.Forms.Form
$window.Height = 500
$window.Width = 400
$window.StartPosition = "CenterScreen"
$window.Font = New-Object System.Drawing.Font("Calibri",11,[System.Drawing.FontStyle]::Bold )
$window.Text = "Duplicates 2.0"
$location = New-Object System.Drawing.Point(10, 10)
$size = New-Object System.Drawing.Size(365, 20)
$ProgressBar1 = New-Object System.Windows.Forms.ProgressBar
$ProgressBar1.Location = New-Object System.Drawing.Point(4, 14)
$ProgressBar1.Size = New-Object System.Drawing.Size(364, 20)
$ProgressBar1.Style = "Marquee"
$ProgressBar1.MarqueeAnimationSpeed = 20
$ProgressBar1.UseWaitCursor = $true
$ProgressBar1.Visible = $false
$ProgressBar2 = New-Object System.Windows.Forms.ProgressBar
$ProgressBar2.Location = New-Object System.Drawing.Point(4, 39)
$ProgressBar2.Size = New-Object System.Drawing.Size(364, 20)
$ProgressBar2.Style = "Marquee"
$ProgressBar2.MarqueeAnimationSpeed = 20
$ProgressBar2.UseWaitCursor = $true
$ProgressBar2.Visible = $false
$ProgressBar3 = New-Object System.Windows.Forms.ProgressBar
$ProgressBar3.Location = New-Object System.Drawing.Point(4, 64)
$ProgressBar3.Size = New-Object System.Drawing.Size(364, 20)
$ProgressBar3.Style = "Marquee"
$ProgressBar3.MarqueeAnimationSpeed = 20
$ProgressBar3.UseWaitCursor = $true
$ProgressBar3.Visible = $false
$ProgressBar4 = New-Object System.Windows.Forms.ProgressBar
$ProgressBar4.Location = New-Object System.Drawing.Point(4, 89)
$ProgressBar4.Size = New-Object System.Drawing.Size(364, 20)
$ProgressBar4.Style = "Marquee"
$ProgressBar4.MarqueeAnimationSpeed = 20
$ProgressBar4.UseWaitCursor = $true
$ProgressBar4.Visible = $false
$button = New-Object System.Windows.Forms.Button
$button.size = New-Object system.drawing.size #(100,50)
$button.Location = New-Object System.Drawing.Point(20, 140)
$button.TextAlign = 'middlecenter'
$button.Text = "Gather `r`n Files"
$window.Controls.add($button)
$label = New-Object System.Windows.Forms.Label
$label.Size = New-Object System.Drawing.Size #(100, 50)
$label.Location = New-Object System.Drawing.Point (135, 140)
$label.BorderStyle = 'Fixed3D'
$label.ForeColor = 'green'
$label.TextAlign = 'middlecenter'
$window.Controls.Add($label)
$check_font = New-Object System.Drawing.Font ("Calibri",20,[System.Drawing.FontStyle]::Bold)
$label3 = New-Object system.windows.forms.label
$label3.Size = New-Object System.Drawing.Size (100, 50)
$label3.Location = New-Object system.drawing.point (250 ,140)
$label3.BorderStyle = 'Fixed3D'
$label3.ForeColor = 'green'
$label3.TextAlign = 'middlecenter'
$label3.Font = $check_font
$window.Controls.Add($label3)
$label4 = New-Object system.windows.forms.label
$label4.Location = New-Object System.Drawing.Point(7, 10)
$label4.Size = New-Object System.Drawing.Size(365, 20)
$label4.TextAlign = 'middlecenter'
$label4.Text = 'COMPLETED!!!'
$label4.Visible = $false
$label4.BackColor = [System.Drawing.Color]::FromArgb(6, 176, 37)
$window.Controls.Add($label4)
$label5 = New-Object system.windows.forms.label
$label5.Location = New-Object System.Drawing.Point(7, 35)
$label5.Size = New-Object System.Drawing.Size(365, 20)
$label5.TextAlign = 'middlecenter'
$label5.Text = 'COMPLETED!!!'
$label5.Visible = $false
$label5.BackColor = [System.Drawing.Color]::FromArgb(6, 176, 37)
$window.Controls.Add($label5)
$label11 = New-Object system.windows.forms.label
$label11.Location = New-Object System.Drawing.Point(7, 60)
$label11.Size = New-Object System.Drawing.Size(365, 20)
$label11.TextAlign = 'middlecenter'
$label11.Text = 'COMPLETED!!!'
$label11.Visible = $false
$label11.BackColor = [System.Drawing.Color]::FromArgb(6, 176, 37)
$window.Controls.Add($label11)
$label12 = New-Object system.windows.forms.label
$label12.Location = New-Object System.Drawing.Point(7, 85)
$label12.Size = New-Object System.Drawing.Size(365, 20)
$label12.TextAlign = 'middlecenter'
$label12.Text = 'COMPLETED!!!'
$label12.Visible = $false
$label12.BackColor = [System.Drawing.Color]::FromArgb(6, 176, 37)
$window.Controls.Add($label12)
$timer = New-Object System.Windows.Forms.Timer
$timer.Interval = 1000
$timer.add_Tick({$script:time2 =((get-date)-$script:time1).ToString("hh\:mm\:ss")
$label.text = $script:time2
})
$button.add_Click({TEST_1})
$button2 = New-Object System.Windows.Forms.Button
$button2.Height = 50
$button2.Width = ($window.Width * 0.85)
$button2.Location = New-Object System.Drawing.Point (20, 400)
$button2.TextAlign = 'middlecenter'
$button2.Text = 'Exit'
$window.Controls.Add($button2)
$button2.add_Click({$window.Close()})
$button3 = new-object System.Windows.Forms.Button
$button3.Size = New-Object System.Drawing.Size (100, 50)
$button3.Location = New-Object System.Drawing.Point (20, 200)
$button3.Text = "Hashes"
$window.Controls.Add($button3)
$timer2 = New-Object System.Windows.Forms.Timer
$timer2.Interval = 1000
$timer2.add_Tick({$script:time4 =((get-date)-$script:time3).ToString("hh\:mm\:ss")
$label2.text = $script:time4
})
function W_pizdeczke
{
write-host "W PIZDE!"
}
function W_deseczke2
{
$ProgressBar3.Hide()
$groupbox.Controls.Remove($ProgressBar3)
$groupbox.Refresh()
$label11.show()
$button4.Enabled = $true
$timer3.stop()
$button4_timer.stop()
$label8.Text = "Kurwo"
}
function W_deseczke
{
$ProgressBar2.Hide()
$groupbox.Controls.Remove($ProgressBar2)
$groupbox.Refresh()
$label5.show()
$button3.Enabled = $true
$timer2.stop()
$button3_timer.stop()
$label6.Text = "Udało się"
}
$button3_timer = new-object system.windows.forms.timer
$button3_timer.Interval = 1000
$button3_timer.add_Tick({
$ticker = (get-job -name TESTOWA).state
if ($ticker -eq 'Completed'){W_deseczke}
})
$button4_timer = new-object system.windows.forms.timer
$button4_timer.Interval = 1000
$button4_timer.add_Tick({
$ticker2 = (get-job -name TESTOWA2).state
if ($ticker2 -eq 'Completed'){W_deseczke2}
})
$button3.add_Click({
$button3_timer.Start()
$button4_timer.Start()
$Script:time3 = Get-Date
$Script:time5 = Get-Date
$timer2.start()
$timer3.start()
$ProgressBar2.BringToFront()
$ProgressBar2.Show()
$ProgressBar3.BringToFront()
$ProgressBar3.Show()
$this.Enabled = $false
$button4.enabled= $false
$job2 = start-job -Name "TESTOWA" -scriptblock {
$using:PORNO|
Get-FileHash -Algorithm MD5
}
$job3 = start-job -Name "TESTOWA2" -scriptblock {
$using:PORNO|
Select #{l="Algorithm";e={}}, #{l="Hash";e={}}, #{l='File';e={$_.PSChildName}}, #{l='Compare Filename';e={$_.BaseName.replace('_','*').replace(' ','*').replace('-','*')}}, #{l="Path";e={$path + $_.PSParentPath.Substring(38,$_.PSParentPath.Length-38)}}, #{l="Link";e={$_.FullName}}, #{l="Extension";e={$_.Extension}}|
group -Property 'Compare Filename'|
Where {#($_.Group.Extension |Sort -Unique).Count -ge 2}
}
while(($job2.State -eq 'Running') -or ($job3.state -eq 'Running')) {
[System.Windows.Forms.Application]::DoEvents()
}
$job2|Wait-job|Receive-Job|Set-Variable -Name DUSZNO -Scope Script
$job3|Wait-job|Receive-Job|Set-Variable -Name CIASNO -Scope Script
})
$label2 = New-Object System.Windows.Forms.Label
$label2.size = New-Object System.Drawing.Size (100, 50)
$label2.Location = New-Object System.Drawing.Point (135, 200)
$label2.BorderStyle = 'Fixed3D'
$label2.ForeColor = 'green'
$label2.TextAlign = 'middlecenter'
$window.Controls.Add($label2)
$label6 = new-object System.Windows.Forms.Label
$label6.Size = New-Object System.Drawing.Size (100, 50)
$label6.Location = New-Object System.Drawing.Point (250, 200)
$label6.ForeColor = 'green'
$label6.TextAlign = 'middlecenter'
$label6.Font = $check_font
$label6.BorderStyle = 'Fixed3D'
$window.Controls.Add($label6)
$button4 = New-Object System.Windows.Forms.Button
$button4.size = New-Object System.Drawing.Size (100, 50)
$button4.Location = New-Object System.Drawing.Point (20, 260)
$button4.Text = "Extensions"
$window.Controls.Add($button4)
$timer3 = New-Object System.Windows.Forms.Timer
$timer3.Interval = 1000
$timer3.add_Tick({$script:time6 =((get-date)-$script:time5).ToString("hh\:mm\:ss")
$label7.text = $script:time6
})
$button4.Add_Click({
})
$label7 = New-Object System.Windows.Forms.Label
$label7.Size = New-Object System.Drawing.Size (100, 50)
$label7.Location = New-Object System.Drawing.Point (135, 260)
$label7.BorderStyle = 'Fixed3D'
$label7.ForeColor = 'green'
$label7.TextAlign = 'middlecenter'
$window.Controls.Add($label7)
$label8 = New-Object System.Windows.Forms.Label
$label8.Size = New-Object System.Drawing.Size (100, 50)
$label8.Location = New-Object System.Drawing.Point (250, 260)
$label8.BorderStyle = 'Fixed3D'
$label8.ForeColor = 'green'
$label8.Font = $check_font
$label8.TextAlign = 'middlecenter'
$window.Controls.Add($label8)
$timer4 = new-object System.Windows.Forms.Timer
$timer4.add_Tick({$script:time8 =((get-date)-$script:time7).ToString("hh\:mm\:ss")
$label9.text = $script:time8
})
$button5 = New-Object System.Windows.Forms.Button
$button5.size = New-Object System.Drawing.Size (100, 50)
$button5.Location = New-Object System.Drawing.Point (20, 320)
$button5.Text = "Zero Length"
$window.Controls.Add($button5)
$button5.Add_Click({
$Script:time7 = Get-Date
$timer4.start()
$job4 = start-job -scriptblock {
$using:PORNO|
? {$_.Length -eq 0}|
Select #{l='File';e={$_.PSChildName}}, Length, Directory, FullName
}
while($job4.State -eq 'Running') {
[System.Windows.Forms.Application]::DoEvents()
}
$job4|Wait-job|Receive-Job|Set-Variable -Name PRZYJEMNIE -Scope Script
$timer4.Stop()
if ($PRZYJEMNIE -eq $null){$label10.Text = "EMPTY"}else{$label10.Text = [char]8730}
})
$label9 = New-Object System.Windows.Forms.Label
$label9.Size = New-Object System.Drawing.Size (100, 50)
$label9.Location = New-Object System.Drawing.Point (135, 320)
$label9.BorderStyle = 'Fixed3D'
$label9.ForeColor = 'green'
$label9.TextAlign = 'middlecenter'
$window.Controls.Add($label9)
$label10 = New-Object System.Windows.Forms.Label
$label10.Size = New-Object System.Drawing.Size (100,50)
$label10.Location = New-Object System.Drawing.Point (250, 320)
$label10.BorderStyle = 'Fixed3D'
$label10.ForeColor = 'green'
$label10.Font = $check_font
$label10.TextAlign = 'middlecenter'
$window.Controls.Add($label10)
$groupbox = New-Object System.Windows.Forms.GroupBox
$groupbox.size = New-Object system.drawing.size #(377, 413)
$groupbox.Location = New-Object System.Drawing.point (4, -5)
$groupboxGraphics = $groupbox.CreateGraphics()
$groupbox.Controls.Add($ProgressBar1)
$groupbox.Controls.Add($ProgressBar2)
$groupbox.Controls.Add($ProgressBar3)
$groupbox.Controls.Add($ProgressBar4)
$window.Controls.Add($groupbox)
$window.ShowDialog()|out-null

ComboBox.SelectedItem giving Null value

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.

Checkboxes in powershell GUI not working as supposed to

I have a GUI created in powershell, which contains some checkboxes. Later in the script I use the values from the checkboxes to set parameters of a user account, but it seems the script doesn't take the values as intended. I mean, sometimes the account is created with correct values, sometimes not. I wasn't able to discover any pattern when it works and when not.
$Orig_exec_policy = Get-ExecutionPolicy
Set-ExecutionPolicy Bypass -Force
<# This form was created using POSHGUI.com a free online gui designer for PowerShell
.NAME
Untitled
#>
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()
#region Window properties
$AD_user_creation = New-Object system.Windows.Forms.Form
$AD_user_creation.ClientSize = '480,740'
$AD_user_creation.text = "AD user creation - WG Mustang"
$AD_user_creation.TopMost = $false
#endregion
[void]$AD_user_creation.SuspendLayout()
#region Real name of the user
$Display_name_lbl = New-Object system.Windows.Forms.Label
$Display_name_lbl.text = "User`'s real name"
$Display_name_lbl.AutoSize = $true
$Display_name_lbl.width = 25
$Display_name_lbl.height = 10
$Display_name_lbl.location = New-Object System.Drawing.Point(10,10)
$First_name_val = New-Object system.Windows.Forms.TextBox
$First_name_val.multiline = $false
$First_name_val.width = 120
$First_name_val.height = 20
$First_name_val.location = New-Object System.Drawing.Point(200,10)
$Second_name_val = New-Object system.Windows.Forms.TextBox
$Second_name_val.multiline = $false
$Second_name_val.width = 120
$Second_name_val.height = 20
$Second_name_val.location = New-Object System.Drawing.Point(330,10)
$Display_name_val = New-Object system.Windows.Forms.Label
$Display_name_val.Text = ""
$Display_name_val.width = 250
$Display_name_val.height = 20
$Display_name_val.location = New-Object System.Drawing.Point(200,40)
#endregion
#region User name of the user
$User_name_lbl = New-Object system.Windows.Forms.Label
$User_name_lbl.text = "User logon name"
$User_name_lbl.AutoSize = $true
$User_name_lbl.width = 25
$User_name_lbl.height = 10
$User_name_lbl.location = New-Object System.Drawing.Point(10,70)
$User_name_val = New-Object system.Windows.Forms.TextBox
$User_name_val.multiline = $false
$User_name_val.width = 250
$User_name_val.height = 20
$User_name_val.location = New-Object System.Drawing.Point(200,70)
#endregion
#region Account password
$Password_lbl = New-Object system.Windows.Forms.Label
$Password_lbl.text = "Password"
$Password_lbl.AutoSize = $true
$Password_lbl.width = 25
$Password_lbl.height = 10
$Password_lbl.location = New-Object System.Drawing.Point(10,100)
$Password_ini_val = New-Object system.Windows.Forms.MaskedTextBox
$Password_ini_val.multiline = $false
$Password_ini_val.width = 250
$Password_ini_val.height = 20
$Password_ini_val.UseSystemPasswordChar = $true
$Password_ini_val.location = New-Object System.Drawing.Point(200,100)
$Password_conf_val = New-Object system.Windows.Forms.MaskedTextBox
$Password_conf_val.multiline = $false
$Password_conf_val.width = 250
$Password_conf_val.height = 20
$Password_conf_val.UseSystemPasswordChar = $true
$Password_conf_val.location = New-Object System.Drawing.Point(200,130)
#endregion
#region Location of the user
$Location_lbl = New-Object system.Windows.Forms.Label
$Location_lbl.text = "Location"
$Location_lbl.AutoSize = $true
$Location_lbl.width = 25
$Location_lbl.height = 10
$Location_lbl.location = New-Object System.Drawing.Point(10,160)
$Location_val = New-Object system.Windows.Forms.TextBox
$Location_val.multiline = $false
$Location_val.text = "OU=Users,OU=ADM,DC=Mustangeng,DC=com"
$Location_val.width = 250
$Location_val.height = 20
$Location_val.location = New-Object System.Drawing.Point(200,160)
#endregion
#region Checkboxes
$Must_chg_pass = New-Object system.Windows.Forms.CheckBox
$Must_chg_pass.text = "User must change password at next logon"
$Must_chg_pass.AutoSize = $false
$Must_chg_pass.width = 290
$Must_chg_pass.height = 20
$Must_chg_pass.location = New-Object System.Drawing.Point(200,190)
$Cannot_chg_pass = New-Object system.Windows.Forms.CheckBox
$Cannot_chg_pass.text = "User cannot change password"
$Cannot_chg_pass.AutoSize = $false
$Cannot_chg_pass.width = 250
$Cannot_chg_pass.height = 20
$Cannot_chg_pass.location = New-Object System.Drawing.Point(200,220)
$Cannot_chg_pass.Checked = $true
$Pass_not_expires = New-Object system.Windows.Forms.CheckBox
$Pass_not_expires.text = "Password never expires"
$Pass_not_expires.AutoSize = $false
$Pass_not_expires.width = 250
$Pass_not_expires.height = 20
$Pass_not_expires.location = New-Object System.Drawing.Point(200,250)
$Pass_not_expires.Checked = $true
$Account_disabled_val = New-Object system.Windows.Forms.CheckBox
$Account_disabled_val.text = "Account is active"
$Account_disabled_val.AutoSize = $false
$Account_disabled_val.width = 250
$Account_disabled_val.height = 20
$Account_disabled_val.location = New-Object System.Drawing.Point(200,280)
$Account_disabled_val.Checked = $false
#endregion
#region Description
$GECOS_lbl = New-Object system.Windows.Forms.Label
$GECOS_lbl.text = "Description"
$GECOS_lbl.AutoSize = $true
$GECOS_lbl.width = 25
$GECOS_lbl.height = 10
$GECOS_lbl.location = New-Object System.Drawing.Point(10,310)
$GECOS_val = New-Object system.Windows.Forms.TextBox
$GECOS_val.multiline = $false
$GECOS_val.width = 250
$GECOS_val.height = 20
$GECOS_val.location = New-Object System.Drawing.Point(200,310)
#endregion
#region Group membership
$ADGroups_lbl = New-Object system.Windows.Forms.Label
$ADGroups_lbl.text = "AD Groups"
$ADGroups_lbl.AutoSize = $true
$ADGroups_lbl.width = 25
$ADGroups_lbl.height = 10
$ADGroups_lbl.location = New-Object System.Drawing.Point(10,340)
$ADGroups_val = New-Object system.Windows.Forms.TextBox
$ADGroups_val.multiline = $true
$ADGroups_val.width = 250
$ADGroups_val.height = 160
$ADGroups_val.location = New-Object System.Drawing.Point(200,340)
#endregion
#region Additional attributes
$Ext_Attribute5_lbl = New-Object System.Windows.Forms.Label
$Ext_Attribute5_lbl.Text = "Extension Attribute5"
$Ext_Attribute5_lbl.AutoSize = $true
$Ext_Attribute5_lbl.Width = 25
$Ext_Attribute5_lbl.Height = 10
$Ext_Attribute5_lbl.Location = New-Object System.Drawing.Point(10,510)
$Ext_Attribute5_val = New-Object System.Windows.Forms.TextBox
$Ext_Attribute5_val.Text = "Company name"
$Ext_Attribute5_val.Multiline = $false
$Ext_Attribute5_val.Width = 250
$Ext_Attribute5_val.Height = 20
$Ext_Attribute5_val.Location = New-Object System.Drawing.Point(200,510)
$Ext_Attribute10_lbl = New-Object System.Windows.Forms.Label
$Ext_Attribute10_lbl.Text = "Extension Attribute10"
$Ext_Attribute10_lbl.AutoSize = $true
$Ext_Attribute10_lbl.Width = 25
$Ext_Attribute10_lbl.Height = 10
$Ext_Attribute10_lbl.Location = New-Object System.Drawing.Point(10,540)
$Ext_Attribute10_val = New-Object System.Windows.Forms.TextBox
$Ext_Attribute10_val.Text = "Region"
$Ext_Attribute10_val.Multiline = $false
$Ext_Attribute10_val.Width = 250
$Ext_Attribute10_val.Height = 20
$Ext_Attribute10_val.Location = New-Object System.Drawing.Point(200,540)
$Ext_Attribute15_lbl = New-Object System.Windows.Forms.Label
$Ext_Attribute15_lbl.Text = "Extension Attribute15"
$Ext_Attribute15_lbl.AutoSize = $true
$Ext_Attribute15_lbl.Width = 25
$Ext_Attribute15_lbl.Height = 10
$Ext_Attribute15_lbl.Location = New-Object System.Drawing.Point(10,570)
$Ext_Attribute15_val = New-Object System.Windows.Forms.TextBox
$Ext_Attribute15_val.Text = "EH/WH"
$Ext_Attribute15_val.Multiline = $false
$Ext_Attribute15_val.Width = 250
$Ext_Attribute15_val.Height = 20
$Ext_Attribute15_val.Location = New-Object System.Drawing.Point(200,570)
$Job_Title_lbl = New-Object System.Windows.Forms.Label
$Job_Title_lbl.Text = "Job title"
$Job_Title_lbl.AutoSize = $true
$Job_Title_lbl.Width = 25
$Job_Title_lbl.Height = 10
$Job_Title_lbl.Location = New-Object System.Drawing.Point(10,600)
$Job_Title_val = New-Object System.Windows.Forms.TextBox
$Job_Title_val.Text = "NA"
$Job_Title_val.Multiline = $false
$Job_Title_val.Width = 250
$Job_Title_val.Height = 20
$Job_Title_val.Location = New-Object System.Drawing.Point(200,600)
$Department_lbl = New-Object System.Windows.Forms.Label
$Department_lbl.Text = "Department"
$Department_lbl.AutoSize = $true
$Department_lbl.Width = 25
$Department_lbl.Height = 10
$Department_lbl.Location = New-Object System.Drawing.Point(10,630)
$Department_val = New-Object System.Windows.Forms.TextBox
$Department_val.Text = "NA"
$Department_val.Multiline = $false
$Department_val.Width = 250
$Department_val.Height = 20
$Department_val.Location = New-Object System.Drawing.Point(200,630)
$Company_lbl = New-Object System.Windows.Forms.Label
$Company_lbl.Text = "Company"
$Company_lbl.AutoSize = $true
$Company_lbl.Width = 25
$Company_lbl.Height = 10
$Company_lbl.Location = New-Object System.Drawing.Point(10,660)
$Company_val = New-Object System.Windows.Forms.TextBox
$Company_val.Text = "IBM"
$Company_val.Multiline = $false
$Company_val.Width = 250
$Company_val.Height = 20
$Company_val.Location = New-Object System.Drawing.Point(200,660)
#endregion
#region Buttons
$Confirm_Button = New-Object system.Windows.Forms.Button
$Confirm_Button.BackColor = "#00ff00"
$Confirm_Button.text = "OK"
$Confirm_Button.width = 100
$Confirm_Button.height = 30
$Confirm_Button.location = New-Object System.Drawing.Point(200,690)
$Confirm_Button.Font = 'Microsoft Sans Serif,10,style=Bold'
$Create_ADuser = {
if ($Password_ini_val.Text -cne $Password_conf_val.Text)
{
[System.Windows.MessageBox]::Show("Passwords don't match")
} elseif ($Password_ini_val.Text.Length -lt 8)
{
[System.Windows.MessageBox]::Show("Password is too short")
} else {
$password = $Password_ini_val.Text | ConvertTo-SecureString -AsPlainText -Force
$Display_name = $Display_name_val.Text + " [ADM]"
New-ADUser -GivenName $First_name_val.Text -Surname $Second_name_val.Text -DisplayName $Display_name -AccountPassword $password -Path $Location_val.Text -Name $User_name_val.Text`
-CannotChangePassword $Cannot_chg_pass.Checked -PasswordNeverExpires $Pass_not_expires.Checked -ChangePasswordAtLogon $Must_chg_pass.Checked -Enabled $Account_disabled_val.Checked`
-Description $GECOS_val.Text -OtherAttributes #{'ExtensionAttribute5' = $Ext_Attribute5_val.Text;'ExtensionAttribute9' = "People";'ExtensionAttribute10' = $Ext_Attribute10_val.Text;`
'ExtensionAttribute11' = "Other";'ExtensionAttribute12' = "No";'ExtensionAttribute14' = "NA";'ExtensionAttribute15' = $Ext_Attribute15_val.Text;'Division' = "WG Mustang"}`
-Office "NA" -OfficePhone "NA" -Title $Job_Title_val.Text -Department $Department_val.Text -Company $Company_val.Text -SamAccountName $User_name_val.Text -PassThru | `
Add-ADPrincipalGroupMembership -MemberOf $ADGroups_val.Text
$AD_user_creation.Close()
}
}
$Confirm_Button.add_Click($Create_ADuser)
$Cancel_button = New-Object system.Windows.Forms.Button
$Cancel_button.BackColor = "#ff0000"
$Cancel_button.text = "Cancel"
$Cancel_button.width = 100
$Cancel_button.height = 30
$Cancel_button.location = New-Object System.Drawing.Point(350,690)
$Cancel_button.Font = 'Microsoft Sans Serif,10,style=Bold'
<#$Cancel = {
$AD_user_creation.Close()
exit
}#>
$Cancel_button.add_Click({
$AD_user_creation.Close()
exit
})
$AD_user_creation.AcceptButton = $Confirm_Button
$AD_user_creation.CancelButton = $Cancel_button
#endregion
$AD_user_creation.controls.AddRange(#($Display_name_lbl,$First_name_val,$Second_name_val,$User_name_lbl,$Display_name_val,$User_name_val,$Password_lbl,$Password_ini_val,$Password_conf_val,$Location_lbl,`
$Location_val,$Must_chg_pass,$Cannot_chg_pass,$Pass_not_expires,$Account_disabled_val,$GECOS_lbl,$GECOS_val,$ADGroups_lbl,$ADGroups_val,$Ext_Attribute5_lbl,$Ext_Attribute5_val,$Ext_Attribute10_lbl,`
$Ext_Attribute10_val,$Ext_Attribute15_lbl,$Ext_Attribute15_val,$Job_Title_lbl,$Job_Title_val,$Department_lbl,$Department_val,$Company_lbl,$Company_val,$Confirm_Button,$Cancel_button))
$showFullName = { $Display_name_val.Text = ($First_name_val.Text + " " + $Second_name_val.Text) }
[void]$Second_name_val.Add_Leave( { & $showFullName } )
[void]$First_name_val.Add_Leave( { & $showFullName } )
[void]$AD_user_creation.ResumeLayout()
$result = $AD_user_creation.ShowDialog()
[void]$AD_user_creation.Dispose()
Set-ExecutionPolicy $Orig_exec_policy -Force
Definitions of checkboxes start at the row 101 and user gets created at the rows 268-272.
The user sometimes gets created as active, sometimes as disabled. And once I notice that even the "user cannot change password" doesn't get checked in the account, even though the value in the form is set to True. I didn't notice any problems with the other checkboxes. Any idea what's happening?
I believe the error stems from the way you use the New-User cmdlet. That piece of code looks complicated enough to not see that there is at least one backtick in the wrong place.
Better use Splatting for this:
$params = #{
'GivenName' = $First_name_val.Text
'Surname' = $Second_name_val.Text
'DisplayName' = $Display_name
'AccountPassword' = $password
'Path' = $Location_val.Text
'Name' = $User_name_val.Text
'CannotChangePassword' = $Cannot_chg_pass.Checked
'PasswordNeverExpires' = $Pass_not_expires.Checked
'ChangePasswordAtLogon' = $Must_chg_pass.Checked
'Enabled' = $Account_disabled_val.Checked
'Description' = $GECOS_val.Text
'Office' = "NA"
'OfficePhone' = "NA"
'Title' = $Job_Title_val.Text
'Department' = $Department_val.Text
'Company' = $Company_val.Text
'SamAccountName' = $User_name_val.Text
'OtherAttributes' = #{'ExtensionAttribute5' = $Ext_Attribute5_val.Text
'ExtensionAttribute9' = "People"
'ExtensionAttribute10' = $Ext_Attribute10_val.Text
'ExtensionAttribute11' = "Other"
'ExtensionAttribute12' = "No"
'ExtensionAttribute14' = "NA"
'ExtensionAttribute15' = $Ext_Attribute15_val.Text
'Division' = "WG Mustang"}
'PassThru' = $true
}
$newUser = New-ADUser #params
if ($newUser) {
$newUser | Add-ADPrincipalGroupMembership -MemberOf $ADGroups_val.Text
}
else {
[System.Windows.MessageBox]::Show("Error creating new user")
}

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.

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")
}
}