Can't get ComboBox text to appear correctly [closed] - powershell

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I have a script that will do the following:
Create a Form with 2 ComboBoxes and a Text Box for user entry
I populate ComboBox 1 and populate ComboBox 2 based on the selection from ComboBox 1. Another thing that partially works is I can get Text to appear on the form based on the selection of ComboBox 1.
My Challenges are challenges I have, by priority are:
Challenge 1:
How can I change the the text on the form doesn't update if I change the selection in ComboBox 1. selection in ComboBox 1?
Challenge 2: (linked to Challenge 1)
I am looking for a way to combing ComboBox 1 with ComboBox 2 with the Text Box
Example: Comp1-Tst-MyTst?
Challenge 3:
I'm looking to import a CSV file / OR using a Get-AD.... for the variables instead of hard coding in the script.
Challenge 4:
I'm struggling with adding an Icon to the form
Challenge 5:
I'd like to prevent the Ok Button from appearing until a Checkbox is checked
Here's the code I have:
# Below is one of the Array's I'm adding
$ADSites=#("S01","S02","S03")
$ADSiteS01=#("AAA","BBB","CCC")
$ADSiteS02=#("DDD","EEE","FFF")
$ADSiteS03=#("GGG","HHH","JJJ")
####################################################################################################
##### Create Combo Boxes
####################################################################################################
$Form = New-Object System.Windows.Forms.Form
$Form.Size = New-Object System.Drawing.Size(625,625)
$Form.FormBorderStyle = "FixedToolWindow"
$Combobox1 = New-Object System.Windows.Forms.Combobox
$Combobox1.Location = New-Object System.Drawing.Size(26,25)
$Combobox1.Size = New-Object System.Drawing.Size(105,20)
$Combobox1.items.AddRange($ADSites)
$combobox2 = New-Object System.Windows.Forms.Combobox
$combobox2.Location = New-Object System.Drawing.Size(143,25)
$combobox2.Size = New-Object System.Drawing.Size(105,20)
$textBoxFPS = New-Object System.Windows.Forms.TextBox
$textBoxFPS.Location = New-Object System.Drawing.Point(26,75)
$textBoxFPS.Size = New-Object System.Drawing.Size(165,20)
$form.Controls.Add($textBoxFPS)
$Form.Controls.Add($combobox1)
$Form.Controls.Add($combobox2)
################################################################################################
##### Create Text Box
################################################################################################
$textBoxFPS = New-Object System.Windows.Forms.TextBox
$textBoxFPS.Location = New-Object System.Drawing.Point(26,75)
$textBoxFPS.Size = New-Object System.Drawing.Size(165,20)
$form.Controls.Add($textBoxFPS)
################################################################################################
##### Add Labels for the Combo Boxes
###################################################################################################
$lbADSub = New-Object System.Windows.Forms.Label
$lbADSub.Text = "Select AD Site"; $lbADSub.Top = 5; $lbADSub.Left = 26; $lbADSub.Autosize = $true
$form.Controls.Add($lbADSub)
$lbDeptSub = New-Object System.Windows.Forms.Label
$lbDeptSub.Text = "Select Department"; $lbDeptSub.Top = 5; $lbDeptSub.Left = 143;
$lbDeptSub.Autosize = $true
$form.Controls.Add($lbDeptSub)
$lbFPSSub = New-Object System.Windows.Forms.Label
$lbFPSSub.Text = "Type in the Asset Tag"; $lbFPSSub.Top = 55; $lbFPSSub.Left = 26;
$lbFPSSub.Autosize = $true
$form.Controls.Add($lbFPSSub)
## CheckBox
$chkThis = New-Object Windows.Forms.checkbox
$chkThis.Text = "Verify New Computer Name" ; $chkThis.Left = 26; $chkThis.Top = 105;
$chkThis.AutoSize = $true
$chkThis.Checked = $false # set a default value
$form.Controls.Add($chkThis)
<#
$lbCompSub = New-Object System.Windows.Forms.Label
$lbCompSub.Text = "Verify Computer Name"; $lbCompSub.Top = 105; $lbCompSub.Left = 26;
$lbCompSub.Autosize = $true
$form.Controls.Add($lbCompSub)
#>
############################################################################################
##### Create Ok and Cancel Buttons
############################################################################################
$buttonPanel = New-Object Windows.Forms.Panel
$buttonPanel.Size = New-Object Drawing.Size #(400,40)
$buttonPanel.Dock = "Bottom"
## Creating the Ok Button
$okButton = New-Object Windows.Forms.Button
$okButton.Top = $cancelButton.Top ; $okButton.Left = $cancelButton.Left - $okButton.Width - 5
$okButton.Text = "Ok"
$okButton.DialogResult = "Ok"
$okButton.Anchor = "Left"
## Creating the Cancel Button
$cancelButton = New-Object Windows.Forms.Button
$cancelButton.Left = $buttonPanel.Height - $cancelButton.Height - 10; $cancelButton.Left = $buttonPanel.Width - $cancelButton.Width - 10
$cancelButton.Text = "Cancel"
$cancelButton.DialogResult = "Cancel"
$cancelButton.Anchor = "Right"
## Add the buttons to the button panel
$buttonPanel.Controls.Add($okButton)
$buttonPanel.Controls.Add($cancelButton)
## Add the button panel to the form
$form.Controls.Add($buttonPanel)
## Set Default actions for the buttons
$form.AcceptButton = $okButton # ENTER = Ok
$form.CancelButton = $cancelButton # ESCAPE = Cancel
##################################################################################################
##### Now we do stuff
##################################################################################################
# Populate Combobox 2 When Combobox 1 changes
$ComboBox1_SelectedIndexChanged= {
$combobox2.Items.Clear() # Clear the list
$combobox2.Text = $null # Clear the current entry
Switch ($ComboBox1.Text) {
"S01"{
$ADSiteS01 | ForEach {
$labelClub = New-Object System.Windows.Forms.Label
$labelClub.Location = New-Object System.Drawing.Point(20,200)
$labelClub.Size = New-Object System.Drawing.Size(280,20)
$labelClub.Text = "$($combobox1.SelectedItem)-"
$form.Controls.Add($labelClub)
$combobox2.Items.Add($_)
#$labelClub.Text = "$($combobox2.SelectedItem)-"
}
}
"S02"{
$ADSiteS02 | ForEach {
$labelClub = New-Object System.Windows.Forms.Label
$labelClub.Location = New-Object System.Drawing.Point(20,200)
$labelClub.Size = New-Object System.Drawing.Size(280,20)
$labelClub.Text = "$($combobox1.SelectedItem)-"
$form.Controls.Add($labelClub)
$combobox2.Items.Add($_)
}
}
"S03"{
$ADSiteS03 | ForEach {
$labelClub = New-Object System.Windows.Forms.Label
$labelClub.Location = New-Object System.Drawing.Point(20,200)
$labelClub.Size = New-Object System.Drawing.Size(280,20)
$labelClub.Text = "$($combobox1.SelectedItem)-"
$form.Controls.Add($labelClub)
$combobox2.Items.Add($_)
}
}
}
}
$ComboBox1.add_SelectedIndexChanged($ComboBox1_SelectedIndexChanged)
$Form.ShowDialog()

This should help you out on the combo box issue and the OK button:
# Below is one of the Array's I'm adding
$ADSites=#("S01","S02","S03")
$ADSiteS01=#("AAA","BBB","CCC")
$ADSiteS02=#("DDD","EEE","FFF")
$ADSiteS03=#("GGG","HHH","JJJ")
Add-Type -AssemblyName System.Windows.Forms
#
####################################################################################################
##### Create Combo Boxes
####################################################################################################
$Form = New-Object System.Windows.Forms.Form
$Form.Size = '625,625'
$Form.FormBorderStyle = "FixedToolWindow"
#
$Combobox1 = New-Object System.Windows.Forms.Combobox
$Combobox1.Location = '26,25'
$Combobox1.Size = '105,20'
$Combobox1.items.AddRange($ADSites)
#
$combobox2 = New-Object System.Windows.Forms.Combobox
$combobox2.Location = '143,25'
$combobox2.Size = '105,20'
#
#
$Form.Controls.Add($combobox1)
$Form.Controls.Add($combobox2)
################################################################################################
##### Create Text Box
################################################################################################
$textBoxFPS = New-Object System.Windows.Forms.TextBox
$textBoxFPS.Location = '26,75'
$textBoxFPS.Size = '165,20'
$textBoxFPS.Text = 'xx'
$form.Controls.Add($textBoxFPS)
################################################################################################
##### Add Labels for the Combo Boxes
###################################################################################################
$lbADSub = New-Object System.Windows.Forms.Label
$lbADSub.Text = "Select AD Site"; $lbADSub.Top = 5; $lbADSub.Left = 26; $lbADSub.Autosize = $true
$form.Controls.Add($lbADSub)
$lbDeptSub = New-Object System.Windows.Forms.Label
$lbDeptSub.Text = "Select Department"; $lbDeptSub.Top = 5; $lbDeptSub.Left = 143;
$lbDeptSub.Autosize = $true
$form.Controls.Add($lbDeptSub)
$lbFPSSub = New-Object System.Windows.Forms.Label
$lbFPSSub.Text = "Type in the Asset Tag"; $lbFPSSub.Top = 55; $lbFPSSub.Left = 26;
$lbFPSSub.Autosize = $true
$form.Controls.Add($lbFPSSub)
## CheckBox
$chkThis = New-Object Windows.Forms.checkbox
$chkThis.Text = "Verify New Computer Name" ; $chkThis.Left = 26; $chkThis.Top = 105;
$chkThis.AutoSize = $true
$chkThis.Checked = $false # set a default value
$form.Controls.Add($chkThis)
############################
# Label for choice selection
############################
$labelClub = New-Object System.Windows.Forms.Label
$labelClub.Location = '20,200'
$labelClub.Size = '280,20'
$labelClub.Text = "-"
$form.Controls.Add($labelClub)
<#
$lbCompSub = New-Object System.Windows.Forms.Label
$lbCompSub.Text = "Verify Computer Name"; $lbCompSub.Top = 105; $lbCompSub.Left = 26;
$lbCompSub.Autosize = $true
$form.Controls.Add($lbCompSub)
#>
############################################################################################
##### Create Ok and Cancel Buttons
############################################################################################
$buttonPanel = New-Object Windows.Forms.Panel
$buttonPanel.Size = New-Object Drawing.Size #(400,40)
$buttonPanel.Dock = "Bottom"
## Creating the Ok Button
$okButton = New-Object Windows.Forms.Button
$okButton.Top = $cancelButton.Top ; $okButton.Left = $cancelButton.Left - $okButton.Width - 5
$okButton.Text = "Ok"
$okButton.DialogResult = "Ok"
$okButton.Anchor = "Left"
$okButton.Enabled = $false
## Creating the Cancel Button
$cancelButton = New-Object Windows.Forms.Button
$cancelButton.Left = $buttonPanel.Height - $cancelButton.Height - 10; $cancelButton.Left = $buttonPanel.Width - $cancelButton.Width - 10
$cancelButton.Text = "Cancel"
$cancelButton.DialogResult = "Cancel"
$cancelButton.Anchor = "Right"
## Add the buttons to the button panel
$buttonPanel.Controls.Add($okButton)
$buttonPanel.Controls.Add($cancelButton)
## Add the button panel to the form
$form.Controls.Add($buttonPanel)
## Set Default actions for the buttons
$form.AcceptButton = $okButton # ENTER = Ok
$form.CancelButton = $cancelButton # ESCAPE = Cancel
##################################################################################################
##### Now we do stuff
##################################################################################################
# Populate Combobox 2 When Combobox 1 changes
$ComboBox1.add_SelectedIndexChanged({
$combobox2.Items.Clear() # Clear the list
$combobox2.Text = $null # Clear the current entry
Switch ($ComboBox1.Text) {
"S01"{
$ADSiteS01 | ForEach {
$combobox2.Items.Add($_)
}
}
"S02"{
$ADSiteS02 | ForEach {
$combobox2.Items.Add($_)
}
}
"S03"{
$ADSiteS03 | ForEach {
$combobox2.Items.Add($_)
}
}
}
$labelClub.Text = $combobox1.Text + "-" + $combobox2.Text + "-" + $textBoxFPS.Text
})
$ComboBox2.add_SelectedIndexChanged({
$labelClub.Text = $combobox1.Text + "-" + $combobox2.Text + "-" + $textBoxFPS.Text
})
$textBoxFPS.add_TextChanged({
$labelClub.Text = $combobox1.Text + "-" + $combobox2.Text + "-" + $textBoxFPS.Text
})
$chkThis.Add_CheckStateChanged({
If ($chkThis.Checked) {
$okButton.enabled = $true
}
Else {
$okButton.enabled = $false
}
})
$Form.ShowDialog()
If you use an AD cmdlet to retrieve site details into an object you should easily be able to populate lists from that object using the same method you have now.

Related

define create button in powershell gui

i have written a gui in powershell for creating groups in active directory depending on my selections i have made in the gui.
#### Form settings ################################################################
$Form = New-Object System.Windows.Forms.Form
$Form.FormBorderStyle = [System.Windows.Forms.FormBorderStyle]::FixedSingle #modifies the window border
$Form.Text = "AD GROUP CREATER"
$Form.Size = New-Object System.Drawing.Size(750,470)
$Form.StartPosition = "CenterScreen" #loads the window in the center of the screen
$Form.MinimizeBox = $False
$Form.MaximizeBox = $False
$Form.WindowState = "Normal"
$Form.SizeGripStyle = "Hide"
$Icon = [System.Drawing.Icon]::ExtractAssociatedIcon((Join-Path -Path $PSHOME -ChildPath 'powershell.exe'))
$Form.Icon = $Icon
#### Label settings #################################################################
$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.Text = "CREATE IN"
$Label.Font = "8,style=bold"
$Label.Forecolor = "black"
$Form.Controls.Add($Label)
#### Group box ########################################################
$groupBox = New-Object System.Windows.Forms.GroupBox
$groupBox.Location = New-Object System.Drawing.Size(10,30)
$groupBox.size = New-Object System.Drawing.Size(320,50)
$Form.Controls.Add($groupBox)
#### A button ###################################################################
$A= New-Object System.Windows.Forms.RadioButton
$A.Location = New-Object System.Drawing.Size(15,20)
$A.Size = New-Object System.Drawing.Size(100,20)
$A.Text = "A"
$A.Cursor = [System.Windows.Forms.Cursors]::Hand
$A.Add_Click({})
$groupBox.Controls.Add($A)
#### B button ###################################################################
$B= New-Object System.Windows.Forms.RadioButton
$B.Location = New-Object System.Drawing.Size(150,20)
$B.Size = New-Object System.Drawing.Size(150,20)
$B.Text = "B"
$B.Cursor = [System.Windows.Forms.Cursors]::Hand
$B.Add_Click({})
$groupBox.Controls.Add($B)
#### Label 2 settings #################################################################
$Label2 = New-Object System.Windows.Forms.Label
$Label2.Location = New-Object System.Drawing.Size(10,100)
$Label2.Size = New-Object System.Drawing.Size(280,20)
$Label2.Text = "GROUP NAME"
$label2.Font = "8, style=bold"
$Label2.Forecolor = "black"
$Form.Controls.Add($Label2)
#### textbox settings #################################################################
$Textbox = New-Object System.Windows.Forms.TextBox
$Textbox.Location = New-Object System.Drawing.Size(10,120)
$Textbox.Size = New-Object System.Drawing.Size(320,20)
$Form.Controls.Add($Textbox)
#### Label 3 settings #################################################################
$Label3 = New-Object System.Windows.Forms.Label
$Label3.Location = New-Object System.Drawing.Size(10,170)
$Label3.Size = New-Object System.Drawing.Size(310,20)
$Label3.Text = "GROUP SECTION"
$Label3.Forecolor = "black"
$label3.Font = "8, style=bold"
$Form.Controls.Add($Label3)
#### Group box 2 ########################################################
$groupBox2 = New-Object System.Windows.Forms.GroupBox
$groupBox2.Location = New-Object System.Drawing.Size(10,190)
$groupBox2.size = New-Object System.Drawing.Size(320,160)
$Form.Controls.Add($groupBox2)
#### local button ###################################################################
$lokal = New-Object System.Windows.Forms.RadioButton
$lokal.Location = New-Object System.Drawing.Size(15,20)
$lokal.Size = New-Object System.Drawing.Size(150,20)
$lokal.Text = "Local"
$lokal.Cursor = [System.Windows.Forms.Cursors]::Hand
$lokal.Add_Click({})
$groupBox2.Controls.Add($lokal)
#### global button ###################################################################
$global = New-Object System.Windows.Forms.RadioButton
$global.Location = New-Object System.Drawing.Size(15,70)
$global.Size = New-Object System.Drawing.Size(150,20)
$global.Text = "Global"
$global.Cursor = [System.Windows.Forms.Cursors]::Hand
$global.Add_Click({})
$groupBox2.Controls.Add($global)
#### universal button ###################################################################
$universal = New-Object System.Windows.Forms.RadioButton
$universal.Location = New-Object System.Drawing.Size(15,120)
$universal.Size = New-Object System.Drawing.Size(150,20)
$universal.Text = "Universal"
$universal.Cursor = [System.Windows.Forms.Cursors]::Hand
$universal.Add_Click({})
$groupBox2.Controls.Add($universal)
#### Label 4 settings #################################################################
$Label4 = New-Object System.Windows.Forms.Label
$Label4.Location = New-Object System.Drawing.Size(400,170)
$Label4.Size = New-Object System.Drawing.Size(320,20)
$Label4.Text = "GROUP TYPE"
$Label4.Forecolor = "black"
$label4.Font = "8, style=bold"
$Form.Controls.Add($Label4)
#### Group box 3 ########################################################
$groupBox3 = New-Object System.Windows.Forms.GroupBox
$groupBox3.Location = New-Object System.Drawing.Size(400,190)
$groupBox3.size = New-Object System.Drawing.Size(320,160)
$Form.Controls.Add($groupBox3)
#### security button ###################################################################
$security = New-Object System.Windows.Forms.RadioButton
$security.Location = New-Object System.Drawing.Size(15,20)
$security.Size = New-Object System.Drawing.Size(150,20)
$security.Text = "Security"
$security.Cursor = [System.Windows.Forms.Cursors]::Hand
$security.Add_Click({})
$groupBox3.Controls.Add($security)
#### Distribution button ###################################################################
$Distribution = New-Object System.Windows.Forms.RadioButton
$Distribution.Location = New-Object System.Drawing.Size(15,70)
$Distribution.Size = New-Object System.Drawing.Size(150,20)
$Distribution.Text = "Distribution"
$Distribution.Cursor = [System.Windows.Forms.Cursors]::Hand
$Distribution.Add_Click({})
$groupBox3.Controls.Add($Distribution)
#### Create button ###################################################################
$Create = New-Object System.Windows.Forms.Button
$Create.Location = New-Object System.Drawing.Size(10,370)
$Create.Text = "Create"
$Create.Cursor = [System.Windows.Forms.Cursors]::Hand
$Create.Width = 710
$Create.Height = 50
$Create.Font = "12,style=bold"
$Create.Add_Click({pinginfo})
$Form.Controls.Add($Create)
$Form.Add_Shown({$Form.Activate()})
[void] $Form.ShowDialog()
# destroy the form from memory
$Form.Dispose()
The gui is finished but i have problems completing my code. what do i need to add to my code that the create button creates the groups? furthermore i think that an assignment to the selection is missing
#Theo please have a look. I still need to input a GroupScope. After thet, the script says it is a wrong name but my name looks like test-group
#### Form settings ################################################################
$Form = New-Object System.Windows.Forms.Form
$Form.FormBorderStyle = [System.Windows.Forms.FormBorderStyle]::FixedSingle #modifies the window border
$Form.Text = "AD GROUP CREATER"
$Form.Size = New-Object System.Drawing.Size(750,470)
$Form.StartPosition = "CenterScreen" #loads the window in the center of the screen
$Form.MinimizeBox = $False
$Form.MaximizeBox = $False
$Form.WindowState = "Normal"
$Form.SizeGripStyle = "Hide"
$Icon = [System.Drawing.Icon]::ExtractAssociatedIcon((Join-Path -Path $PSHOME -ChildPath 'powershell.exe'))
$Form.Icon = $Icon
#### Label settings #################################################################
$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.Text = "CREATE IN"
$Label.Font = "8,style=bold"
$Label.Forecolor = "black"
$Form.Controls.Add($Label)
#### Group box ########################################################
$groupBox = New-Object System.Windows.Forms.GroupBox
$groupBox.Location = New-Object System.Drawing.Size(10,30)
$groupBox.size = New-Object System.Drawing.Size(320,50)
$Form.Controls.Add($groupBox)
#### A button ###################################################################
$A= New-Object System.Windows.Forms.RadioButton
$A.Location = New-Object System.Drawing.Size(15,20)
$A.Size = New-Object System.Drawing.Size(100,20)
$A.Text = "A"
$A.Cursor = [System.Windows.Forms.Cursors]::Hand
$A.Add_Click({})
$groupBox.Controls.Add($A)
#### B button ###################################################################
$B= New-Object System.Windows.Forms.RadioButton
$B.Location = New-Object System.Drawing.Size(150,20)
$B.Size = New-Object System.Drawing.Size(150,20)
$B.Text = "B"
$B.Cursor = [System.Windows.Forms.Cursors]::Hand
$B.Add_Click({})
$groupBox.Controls.Add($B)
#### Label 2 settings #################################################################
$Label2 = New-Object System.Windows.Forms.Label
$Label2.Location = New-Object System.Drawing.Size(10,100)
$Label2.Size = New-Object System.Drawing.Size(280,20)
$Label2.Text = "GROUP NAME"
$label2.Font = "8, style=bold"
$Label2.Forecolor = "black"
$Form.Controls.Add($Label2)
#### textbox settings #################################################################
$Textbox = New-Object System.Windows.Forms.TextBox
$Textbox.Location = New-Object System.Drawing.Size(10,120)
$Textbox.Size = New-Object System.Drawing.Size(320,20)
$Form.Controls.Add($Textbox)
#### Label 3 settings #################################################################
$Label3 = New-Object System.Windows.Forms.Label
$Label3.Location = New-Object System.Drawing.Size(10,170)
$Label3.Size = New-Object System.Drawing.Size(310,20)
$Label3.Text = "GROUP SECTION"
$Label3.Forecolor = "black"
$label3.Font = "8, style=bold"
$Form.Controls.Add($Label3)
#### Group box 2 ########################################################
$groupBox2 = New-Object System.Windows.Forms.GroupBox
$groupBox2.Location = New-Object System.Drawing.Size(10,190)
$groupBox2.size = New-Object System.Drawing.Size(320,160)
$Form.Controls.Add($groupBox2)
#### local button ###################################################################
$DomainLocal = New-Object System.Windows.Forms.RadioButton
$DomainLocal.Location = New-Object System.Drawing.Size(15,20)
$DomainLocal.Size = New-Object System.Drawing.Size(150,20)
$DomainLocal.Text = "Local"
$DomainLocal.Cursor = [System.Windows.Forms.Cursors]::Hand
$DomainLocal.Add_Click({})
$groupBox2.Controls.Add($DomainLocal)
#### global button ###################################################################
$global = New-Object System.Windows.Forms.RadioButton
$global.Location = New-Object System.Drawing.Size(15,70)
$global.Size = New-Object System.Drawing.Size(150,20)
$global.Text = "Global"
$global.Cursor = [System.Windows.Forms.Cursors]::Hand
$global.Add_Click({})
$groupBox2.Controls.Add($global)
#### universal button ###################################################################
$universal = New-Object System.Windows.Forms.RadioButton
$universal.Location = New-Object System.Drawing.Size(15,120)
$universal.Size = New-Object System.Drawing.Size(150,20)
$universal.Text = "Universal"
$universal.Cursor = [System.Windows.Forms.Cursors]::Hand
$universal.Add_Click({})
$groupBox2.Controls.Add($universal)
#### Label 4 settings #################################################################
$Label4 = New-Object System.Windows.Forms.Label
$Label4.Location = New-Object System.Drawing.Size(400,170)
$Label4.Size = New-Object System.Drawing.Size(320,20)
$Label4.Text = "GROUP TYPE"
$Label4.Forecolor = "black"
$label4.Font = "8, style=bold"
$Form.Controls.Add($Label4)
#### Group box 3 ########################################################
$groupBox3 = New-Object System.Windows.Forms.GroupBox
$groupBox3.Location = New-Object System.Drawing.Size(400,190)
$groupBox3.size = New-Object System.Drawing.Size(320,160)
$Form.Controls.Add($groupBox3)
#### security button ###################################################################
$security = New-Object System.Windows.Forms.RadioButton
$security.Location = New-Object System.Drawing.Size(15,20)
$security.Size = New-Object System.Drawing.Size(150,20)
$security.Text = "Security"
$security.Cursor = [System.Windows.Forms.Cursors]::Hand
$security.Add_Click({})
$groupBox3.Controls.Add($security)
#### Distribution button ###################################################################
$Distribution = New-Object System.Windows.Forms.RadioButton
$Distribution.Location = New-Object System.Drawing.Size(15,70)
$Distribution.Size = New-Object System.Drawing.Size(150,20)
$Distribution.Text = "Distribution"
$Distribution.Cursor = [System.Windows.Forms.Cursors]::Hand
$Distribution.Add_Click({})
$groupBox3.Controls.Add($Distribution)
#### Create button ###################################################################
$Create = New-Object System.Windows.Forms.Button
$Create.Location = New-Object System.Drawing.Size(10,370)
$Create.Text = "Create"
$Create.Cursor = [System.Windows.Forms.Cursors]::Hand
$Create.Width = 710
$Create.Height = 50
$Create.Font = "12,style=bold"
$Create.Add_Click({
# create an empty parameters object for New-ADGroup
$params = '' | Select-Object Name, GroupScope, GroupCategory, Path
$params.Name = $Textbox.Text.Trim()
# test if there is a name given and if so, test if that name does not already exist in AD
if ([string]::IsNullOrWhiteSpace($params.Name) -or (Get-ADGroup -Filter "Name -eq $($params.Name)")) {
# display an error about the name field
# I'll leave this as exercise for you
return
}
if ($DomainLocal.Checked) {$params.GroupScope = $DomainLocal.Text}
elseif ($global.Checked) {$params.GroupScope = $global.Text}
elseif ($universal.Checked) {$params.GroupScope = $universal.Text}
else {
# display an error that there MUST be a choice here
# I'll leave this as exercise for you
return
}
if ($security.Checked) {$params.GroupCategory = $security.Text}
elseif ($Distribution.Checked) {$params.GroupCategory = $Distribution.Text}
else {
# display an error that there MUST be a choice here
# I'll leave this as exercise for you
return
}
if ($A.Checked) { $params.Path = 'TheOU for option A' }
elseif( $B.Checked) { $params.Path = 'TheOU for option B' }
else {
# display an error that there MUST be a choice here
# I'll leave this as exercise for you
return
}
# next create the group
New-ADGroup #params
})
$Form.Controls.Add($Create)
$Form.Add_Shown({$Form.Activate()})
[void] $Form.ShowDialog()
# destroy the form from memory
$Form.Dispose()
Instead of the now undefined function pinginfo, you will need to check what radiobutton is checked in "Create IN" and from that define the -Path property for New-ADGroup.
Then test if a name is filled in, test if this name does not already exist and use that for -Name.
The checked radiobutton in "Group Section" decides the parameter -GroupScope and the one in "Group Type" is for -GroupCategory.
When you have collected those values and tested if they were filled in, create the group.
Something like this perhaps
Note: The order in which you test and fill in the Hashtable's properties is not important for splatting purposes.
It's up to you if you want to follow the forms TAB-order or use a different strategy.
$Create.Add_Click({
# create an empty parameters splatting Hashtable for New-ADGroup
$params = #{Path = $null; Name = $null; GroupScope = $null; GroupCategory = $null}
# test CREATE IN (OU path)
if ($A.Checked) { $params.Path = 'TheOU for option A' }
elseif ($B.Checked) { $params.Path = 'TheOU for option B' }
else {
# display an error that there MUST be a choice here
# I'll leave this as exercise for you
return
}
# test GROUP NAME
if ([string]::IsNullOrWhiteSpace($Textbox.Text)) {
# display an error about the name field being empty
# I'll leave this as exercise for you
return
}
$params.Name = $Textbox.Text.Trim()
# test GROUP SECTION (GroupScope)
if ($DomainLocal.Checked) {$params.GroupScope = 'DomainLocal'} # as opposed to 'Local' as in the radiobutton's Text
elseif ($global.Checked) {$params.GroupScope = $global.Text} # 'Global'
elseif ($universal.Checked) {$params.GroupScope = $universal.Text} # 'Universal'
else {
# display an error that there MUST be a choice here
# I'll leave this as exercise for you
return
}
# test GROUP TYPE (GroupCategory)
if ($security.Checked) {$params.GroupCategory = $security.Text} # 'Security'
elseif ($Distribution.Checked) {$params.GroupCategory = $Distribution.Text} # 'Distribution '
else {
# display an error that there MUST be a choice here
# I'll leave this as exercise for you
return
}
# finally check to test if the given groupname already exists
if (Get-ADGroup -Filter "Name -eq $($params.Name)") {
# display an error that there already is a group by that name
return
}
# next create the group
New-ADGroup #params
})

PowerShell GUI - Function within a Form not working

currently i am working on an little PowerShell GUI Porject.
I have a Form within this Form there's a Button to call another Form that provides an Listbox with some items in it.
So far so good - i'm getting the results as expected.
Now i mark one of the items - click on OK - then it should do something.
And here is my Problem, it does nothing :D No Error, no warning, just nothing.
If i test with write-host "$($listBox.SelectedItem)" - i get the selected Item as i expect it.
But if you want to use it with get-adfsrelyingpartytrust -Name $listBox.SelectedItem - nothing happens.
If i execute the function without the rest (so that only the listbox appears) everything works as intended O_o.
Code:
function single_rpt{ $RPTForm = New-Object System.Windows.Forms.Form
$RPTForm.ClientSize = '400,200'
$RPTForm.Text = "Choose"
$RPTForm.BackColor = "#b2b2b2"
$RPTForm.AutoSize = $false
$RPTForm.StartPosition = 'CenterScreen'
$RPTForm.ControlBox = $true
$rptButton = New-Object System.Windows.Forms.Button
$rptButton.BackColor = "#ffffff"
$rptButton.Text = "OK"
$rptButton.Width = 90
$rptButton.Height = 30
$rptButton.Location = New-Object System.Drawing.Point(20,125)
$rptButton.Font = 'Microsoft Sans Serif,10'
$rptButton.ForeColor = "#000000"
$rptButton.UseVisualStyleBackColor = $true
$rptButton.DialogResult = 1
# Test List Box
$listBox = New-Object System.Windows.Forms.ListBox
$listBox.Location = New-Object System.Drawing.Point(20,50)
$listBox.Size = New-Object System.Drawing.Size(350,200)
$listBox.Height = 80
$RPTForm.Controls.AddRange(#($listBox,$rptButton))
$listboxArray = #()
$ADFS_RPTS = Get-AdfsRelyingPartyTrust
Foreach($rpt in $ADFS_RPTS)
{
#$Object = Add-Member -TypeName NoteProperty -Name Service -Value $service.Name
$listboxArray += $rpt.Name
}
$listBox.Items.AddRange($listboxArray)
[void]$RPTForm.ShowDialog()
if($RPTForm.DialogResult -eq "OK")
{
Get-AdfsReylingPartyTrus -Name $listBox.SelectedItem
}
}
# Init PowerShell Gui
Add-Type -AssemblyName System.Windows.Forms
# Create Form with size, title and background color, etc.
$TESTForm = New-Object System.Windows.Forms.Form
$TESTForm.ClientSize = '250,300'
$TESTForm.Text = "TEST"
$TESTForm.BackColor = "#ffffff"
$TESTForm.AutoSize = $false
$TESTForm.StartPosition = 'CenterScreen'
$TESTForm.ControlBox = $true
$RPTSingle = New-Object System.Windows.Forms.Button
$RPTSingle.BackColor = "#ffffff"
$RPTSingle.Text = "RPTSingle"
$RPTSingle.Width = 90
$RPTSingle.Height = 30
$RPTSingle.Location = New-Object System.Drawing.Point(75,125)
$RPTSingle.Font = 'Microsoft Sans Serif,10'
$RPTSingle.ForeColor = "#000000"
$RPTSingle.UseVisualStyleBackColor = $true
$RPTSingle.Add_Click({single_rpt})
# Add a Cancel Button
$CancelButton = New-Object System.Windows.Forms.Button
$CancelButton.BackColor = "#ffffff"
$CancelButton.Text = "Cancel"
$CancelButton.Width = 90
$CancelButton.Height = 30
$CancelButton.Location = New-Object System.Drawing.Point(75,250)
$CancelButton.Font = 'Microsoft Sans Serif,10'
$CancelButton.ForeColor = "#000000"
$CancelButton.UseVisualStyleBackColor = $true
$CancelButton.DialogResult = 2
$TESTForm.Controls.AddRange(#($RPTSingle,$CancelButton))
# Display the form
[void]$TESTForm.ShowDialog()
#------------------------------------#

Cannot call show dialog() on a null-valued expression

I am trying to open the next page of my Winforms script and atm on first launch the error is get when clicking the next button is "you cannot call a method on a null-valued expression" [void] $Form_UserInformation.ShowDialog() being the issue code here. When I run it the second time it runs fine but this doesn't work for an EXE file as it closes it and doesn't remember the previous session.
# HomePage Form
$Form_HomePage = New-Object System.Windows.Forms.Form
$Form_HomePage.text = "New User Script"
$Form_HomePage.Size = New-Object System.Drawing.Size(400,400)
$Form_HomePage.FormBorderStyle = "FixedDialog"
$Form_HomePage.TopMost = $false
$Form_HomePage.MaximizeBox = $false
$Form_HomePage.MinimizeBox = $true
$Form_HomePage.ControlBox = "true"
$Form_HomePage.StartPosition = "CenterScreen"
$Form_HomePage.Font = "Segoe UI"
# Title label
$label_HomeTitle = New-Object System.Windows.Forms.Label
$label_HomeTitle.Location = New-Object System.Drawing.Size(47,8)
$label_HomeTitle.Size = New-Object System.Drawing.Size(300,42)
$label_HomeTitle.Font = New-Object System.Drawing.Font("Segoe UI",24,[System.Drawing.FontStyle]::Regular)
$label_HomeTitle.TextAlign = "MiddleCenter"
$label_HomeTitle.Text = "Kuehne && Nagel"
$Form_HomePage.Controls.Add($label_HomeTitle)
# Subheading label
$label_HomeSubTitle = New-Object System.Windows.Forms.Label
$label_HomeSubTitle.Location = New-Object System.Drawing.Size(50,60)
$label_HomeSubTitle.Size = New-Object System.Drawing.Size(300,42)
$label_HomeSubTitle.Font = New-Object System.Drawing.Font("Segoe UI",16,[System.Drawing.FontStyle]::Regular)
$label_HomeSubTitle.TextAlign = "MiddleCenter"
$label_HomeSubTitle.Text = "New User Script"
$Form_HomePage.Controls.Add($label_HomeSubTitle)
# Next button
$button_HomeStart = New-Object System.Windows.Forms.Button
$button_HomeStart.Location = New-Object System.Drawing.Size(75,200)
$button_HomeStart.Size = New-Object System.Drawing.Size(240,32)
$button_HomeStart.TextAlign = "MiddleCenter"
$button_HomeStart.Text = "Start"
$button_HomeStart.Add_Click({
[void] $Form_UserInformation.ShowDialog()
$Form_HomePage.Hide()
})
$Form_HomePage.Controls.Add($button_HomeStart)
# About button
$button_About = New-Object System.Windows.Forms.Button
$button_About.Location = New-Object System.Drawing.Size(75,250)
$button_About.Size = New-Object System.Drawing.Size(240,32)
$button_About.TextAlign = "MiddleCenter"
$button_About.Text = "About"
$button_About.Add_Click({
})
$Form_HomePage.Controls.Add($button_About)
# Exit button
$button_HomeExit = New-Object System.Windows.Forms.Button
$button_HomeExit.Location = New-Object System.Drawing.Size(75,300)
$button_HomeExit.Size = New-Object System.Drawing.Size(240,32)
$button_HomeExit.TextAlign = "MiddleCenter"
$button_HomeExit.Text = "Exit"
$button_HomeExit.Add_Click({
$Form_HomePage.Close()
})
$Form_HomePage.Controls.Add($button_HomeExit)
# Show Form
$Form_HomePage.Add_Shown({$Form_HomePage.Activate()})
[void] $Form_HomePage.ShowDialog()
# User Information Form
$Form_UserInformation = New-Object System.Windows.Forms.Form
$Form_UserInformation.text = "New User Script"
$Form_UserInformation.Size = New-Object System.Drawing.Size(400,400)
$Form_UserInformation.FormBorderStyle = "FixedDialog"
$Form_UserInformation.TopMost = $false
$Form_UserInformation.MaximizeBox = $false
$Form_UserInformation.MinimizeBox = $true
$Form_UserInformation.ControlBox = "true"
$Form_UserInformation.StartPosition = "CenterScreen"
$Form_UserInformation.Font = "Segoe UI"

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.

getting database table to show in dataGridView in powershell

I am building a powershell gui application that pulls info from a database table that I need to monitor. I am having an issue getting that data into the datagridview. I can do it manually so it shows at least one entry but I need it to show the full table results. This is my 1st powershell GUI project.
add-type -AssemblyName System.Data.OracleClient
Function ShowJobsInQueue()
{
## To connect by Service Name
$ora_server = "dm01-scan.campsys.com"
$ora_user = "appuser"
$ora_pass = "hillary"
$ora_servicename = "dbcamp1_svc.campsys.com"
## by ServiceName
$connection = new-object system.data.oracleclient.oracleconnection("Data Source=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=$ora_server)(PORT=1521)) (CONNECT_DATA=(SERVICE_NAME=$ora_servicename)));User Id=$ora_user;Password=$ora_pass;")
$connection.open()
$query = "SELECT * FROM ASYNCJOB WHERE TRUNC(START_DATE)= TRUNC(SYSDATE) AND job_type <>15 and STATUS in ('1','2') ORDER BY ASYNCJOB_ID DESC"
$list_set = new-object system.data.dataset
#$array = New-Object System.Collections.ArrayList
#$array.AddRange($list_table)
#$dataGridView.DataSource = $array
$list_adapter = new-object system.data.oracleclient.oracledataadapter($query, $connection)
$list_adapter.Fill($list_set) | Out-Null
$list_table = new-object system.data.datatable
$list_table = $list_set.Tables[0]
$DBValues = $list_table
while($dataGridView.Rows.Count -lt $list_table.Count) {
$dataGridView.Rows.Add() | Out-Null
}
for ($i=0;$i -lt $list_table.Count ;$i++) {
$dataGridView.rows[$i].Cells[0].Value = $list_table[$i].Item("ASYNCJOB_ID")
}
$connection.close()
$form.refresh()
}
$OnLoadForm_UpdateGrid=
{
ShowJobsInQueue
}
########################################
Add-Type -AssemblyName System.Windows.Forms
$Form = New-Object system.Windows.Forms.Form
$Form.Text = "Report Monitor"
$Form.TopMost = $true
$Form.minimumSize = New-Object System.Drawing.Size(1024,750)
$Form.maximumSize = New-Object System.Drawing.Size(1024,750)
$dataGridView = New-Object System.Windows.Forms.DataGridView
$dataGridView.Size=New-Object System.Drawing.Size(1024,570)
$form.Controls.Add($dataGridView)
$buttonRefresh = New-Object system.windows.Forms.Button
$buttonRefresh.Text = "Refresh"
$buttonRefresh.Width = 65
$buttonRefresh.Height = 35
$buttonRefresh.Add_Click({
$Form.close()
})
$buttonRefresh.location = new-object system.drawing.point(338,600)
$buttonRefresh.Font = "Microsoft Sans Serif,10"
$Form.controls.Add($buttonRefresh)
$buttonExit = New-Object system.windows.Forms.Button
$buttonExit.Text = "Exit"
$buttonExit.Width = 65
$buttonExit.Height = 35
$buttonExit.Add_Click({
$Form.close()
})
$buttonExit.location = new-object system.drawing.point(500,600)
$buttonExit.Font = "Microsoft Sans Serif,10"
$Form.controls.Add($buttonExit)
#Create an unbound DataGridView by declaring a column count.
$dataGridView.ColumnCount = 6
$dataGridView.ColumnHeadersVisible = $true
#Set the column header names.
$dataGridView.Columns[0].Name = "Row#"
$dataGridView.Columns[1].Name = "ASYNCJOB_ID"
$dataGridView.Columns[2].Name = "USER_ID"
$dataGridView.Columns[3].Name = "START_DATE"
$dataGridView.Columns[4].Name = "END_DATE"
$dataGridView.Columns[5].Name = "STATUS"
#$dataGridView.Rows[0].Cells[0].Value = "Value1"
#$dataGridView.Rows[0].Cells[1].Value = "Value2"
#$dataGridView.Rows[0].Cells[2].Value = "Value3"
#$dataGridView.Rows.Add();
#############################################################
$buttonOn = New-Object system.windows.Forms.RadioButton
$buttonOn.Text = "On"
$buttonOn.Width = 65
$buttonOn.Height = 35
$buttonOn.Add_Click({
# create function to turn on auto mode
$Form.close()
})
$buttonOn.location = new-object system.drawing.point(920,600)
$buttonOn.Font = "Microsoft Sans Serif,10"
$Form.controls.Add($buttonOn)
## button off
$buttonOff = New-Object system.windows.Forms.RadioButton
$buttonOff.Text = "Off"
$buttonOff.Width = 65
$buttonOff.Height = 35
$buttonOff.Add_Click({
# create function to turn off auto mode
$Form.close()
})
$buttonOff.location = new-object system.drawing.point(870,600)
$buttonOff.Font = "Microsoft Sans Serif,10"
$Form.controls.Add($buttonOff)
## end button off
#### auto mode label
$Automode = New-Object system.windows.Forms.Label
$Automode.Text = "Auto Mode"
$Automode.Width = 25
$Automode.Height = 10
$Automode.AutoSize = $true
$Automode.location = new-object system.drawing.point(875,580)
$Automode.Font = "Microsoft Sans Serif,10"
$Form.controls.Add($Automode)
#### end label
#Add Form event
$form.add_Load($OnLoadForm_UpdateGrid)
[void]$Form.ShowDialog()
$Form.Dispose()
Here is an example how you could do it:
$DBValues = "Value1","Value2","Value3","Value4","Value5","Value6","Value7","Value8","Value9","Value10"
while($dataGridView.Rows.Count -lt $DBValues.Count) {
$dataGridView.Rows.Add() | Out-Null
}
for ($i=0;$i -lt $DBValues.Count ;$i++) {
$dataGridView.rows[$i].Cells[0].Value = $DBValues[$i]
}
In your case you should do something like that:
while($dataGridView.Rows.Count -lt $list_table.Rows.Count) {
$dataGridView.Rows.Add() | Out-Null
}
for ($i=0;$i -lt $list_table.Rows.Count ;$i++) {
$dataGridView.rows[$i].Cells[0].Value = $list_table.Rows[$i].Item("ASYNCJOB_ID")
}