I am using this function to invoke Popup to insert information:
function Set-Popup ([String]$Title, [String]$Label) {
###################Load Assembly for creating form & button
[void][System.Reflection.Assembly]::LoadWithPartialName( “System.Windows.Forms”)
[void][System.Reflection.Assembly]::LoadWithPartialName( “Microsoft.VisualBasic”)
#####Define the form size & placement
$form = New-Object “System.Windows.Forms.Form”;
$form.Width = 500;
$form.Height = 150;
$form.Text = $title;
$form.StartPosition = [System.Windows.Forms.FormStartPosition]::CenterScreen;
##############Define text label1
$TextLabel1 = New-Object “System.Windows.Forms.Label”;
$TextLabel1.Left = 25;
$TextLabel1.Top = 15;
$TextLabel1.Width = 200;
$TextLabel1.Text = "$Label";
##############Define error label
$ErrorLabel = New-Object “System.Windows.Forms.Label”;
$ErrorLabel.Left = 25;
$ErrorLabel.Top = 65;
$ErrorLabel.Width = 450;
############Define text box1 for input
$TextBox1 = New-Object “System.Windows.Forms.TextBox”;
$TextBox1.Left = 250;
$TextBox1.Top = 10;
$TextBox1.width = 200;
#############define Confirm button
$ConfirmButton = New-Object “System.Windows.Forms.Button”;
$ConfirmButton.Left = 360;
$ConfirmButton.Top = 85;
$ConfirmButton.Width = 100;
$ConfirmButton.Text = “Confirm”;
#############define Cancel button
$CancelButton = New-Object “System.Windows.Forms.Button”;
$CancelButton.Left = 250;
$CancelButton.Top = 85;
$CancelButton.Width = 100;
$CancelButton.Text = “Cancel”;
############# This is when you have to close the form after getting values
$ConfirmBFunc = [System.EventHandler]{
$TextBox1.Text;
$form.Close();
};
$ConfirmButton.Add_Click($ConfirmBFunc);
#############Add controls to all the above objects defined
$form.Controls.Add($ConfirmButton);
$form.Controls.Add($TextLabel1);
$form.Controls.Add($ErrorLabel);
$form.Controls.Add($TextBox1);
$form.ShowDialog();
#################return values
return $TextBox1.Text
}
This works great as I was expecting but when it finishes and using the "return" cmdlet, it returns "Cancel" and after that the info I inserted in the text box.
Kill me but i really don't understand what returns the "cancel" string.
What can be the problem?
The Cancel is coming from the ShowDialog method, which automatically returns the result to the caller. In the case of PowerShell, that means it will automatically be displayed in the console.
To suppress it, do this:
$form.ShowDialog() | Out-Null
If you actually want the result, but want to set it to something else, you can do it in your event handler before closing (and omit the Out-Null from above):
$ConfirmBFunc = [System.EventHandler]{
$TextBox1.Text
$form.DialogResult = [System.Windows.Forms.DialogResult]::OK
$form.Close()
}
Related
This code open GUI to Input Time & Date:
Add-Type -AssemblyName System.Windows.Forms
# Main Form
$mainForm = New-Object System.Windows.Forms.Form
$font = New-Object System.Drawing.Font(“Consolas”, 13)
$mainForm.Text = ” Pick Time Frame”
$mainForm.Font = $font
$mainForm.ForeColor = “White”
$mainForm.BackColor = “DarkOliveGreen”
$mainForm.Width = 300
$mainForm.Height = 200
# DatePicker Label
$datePickerLabel = New-Object System.Windows.Forms.Label
$datePickerLabel.Text = “date”
$datePickerLabel.Location = “15, 10”
$datePickerLabel.Height = 22
$datePickerLabel.Width = 90
$mainForm.Controls.Add($datePickerLabel)
# MinTimePicker Label
$minTimePickerLabel = New-Object System.Windows.Forms.Label
$minTimePickerLabel.Text = “min-time”
$minTimePickerLabel.Location = “15, 45”
$minTimePickerLabel.Height = 22
$minTimePickerLabel.Width = 90
$mainForm.Controls.Add($minTimePickerLabel)
# DatePicker
$datePicker = New-Object System.Windows.Forms.DateTimePicker
$datePicker.Location = “110, 7”
$datePicker.Width = “150”
$datePicker.Format = [windows.forms.datetimepickerFormat]::custom
$datePicker.CustomFormat = “dd/MM/yyyy”
$mainForm.Controls.Add($datePicker)
# MinTimePicker
$minTimePicker = New-Object System.Windows.Forms.DateTimePicker
$minTimePicker.Location = “110, 42”
$minTimePicker.Width = “150”
$minTimePicker.Format = [windows.forms.datetimepickerFormat]::custom
$minTimePicker.CustomFormat = "HH:mm:ss"
$minTimePicker.ShowUpDown = $TRUE
$mainForm.Controls.Add($minTimePicker)
# OD Button
$okButton = New-Object System.Windows.Forms.Button
$okButton.Location = “15, 130”
$okButton.ForeColor = “Black”
$okButton.BackColor = “White”
$okButton.Text = “OK”
$okButton.add_Click({$mainForm.close()})
$mainForm.Controls.Add($okButton)
[void] $mainForm.ShowDialog()
$datePicker | select value
Current Output:
9/30/2021 12:59:34 PM
Desired Output:
After Selecting New date in GUI, it outputs New date only but not new Time.
-> Require help to get Both GUI selected Date & Time .
1.csv:
ServerName
Server1
Server2
Server3
I want to import csv file into combobox and get selected value into variable.
I can load above file to combobox, but output variable is null
function button ($WF) {
###################Load Assembly for creating form & button######
[void][System.Reflection.Assembly]::LoadWithPartialName( “System.Windows.Forms”)
[void][System.Reflection.Assembly]::LoadWithPartialName( “Microsoft.VisualBasic”)
#####Define the form size & placement
$form = New-Object “System.Windows.Forms.Form”;
$form.Width = 500;
$form.Height = 190;
$form.Text = $title;
$form.StartPosition = [System.Windows.Forms.FormStartPosition]::CenterScreen;
$form.ControlBox = $True
##############Define text label2
$textLabel2 = New-Object “System.Windows.Forms.Label”;
$textLabel2.Left = 25;
$textLabel2.Top = 80;
$textLabel2.Text = $WF;
############Define text box2 for input
$cBox2 = New-Object “System.Windows.Forms.combobox”;
$cBox2.Left = 150;
$cBox2.Top = 80;
$cBox2.width = 200;
###############"Add descriptions to combo box"##############
$NameHash = #{}
import-csv "C:\Users\Komp\Desktop\1.csv" | ForEach-Object {
$cBox2.Items.Add($_.ServerName)
}
#############define OK button
$button = New-Object “System.Windows.Forms.Button”;
$button.Left = 360;
$button.Top = 45;
$button.Width = 100;
$button.Text = “Ok”;
$Button.Cursor = [System.Windows.Forms.Cursors]::Hand
$Button.Font = New-Object System.Drawing.Font("Times New Roman",12,[System.Drawing.FontStyle]::BOLD)
############# This is when you have to close the form after getting values
$eventHandler = [System.EventHandler]{
$cBox2.Text;
$form.Close();};
#############Add controls to all the above objects defined
$form.Controls.Add($button);
$form.Controls.Add($textLabel2);
$form.Controls.Add($cBox2);
$ret = $form.ShowDialog();
#################return values
$output = $cBox2.SelectedItem.ToString()
}
$return = button “Job Descriptions"
The problem is that $output variable is empty, how to export selected value into $output variable ?
Got this code from this question
Thanks to Santiago Squarzon's link, managed to solve it
###################Load Assembly for creating form & button######
[void][System.Reflection.Assembly]::LoadWithPartialName( “System.Windows.Forms”)
[void][System.Reflection.Assembly]::LoadWithPartialName( “Microsoft.VisualBasic”)
#####Define the form size & placement
$form = New-Object “System.Windows.Forms.Form”;
$form.Width = 500;
$form.Height = 190;
$form.Text = $title;
$form.StartPosition = [System.Windows.Forms.FormStartPosition]::CenterScreen;
$form.ControlBox = $True
##############Define text label2
$textLabel2 = New-Object “System.Windows.Forms.Label”;
$textLabel2.Left = 25;
$textLabel2.Top = 80;
$textLabel2.Text = $WF;
############Define text box2 for input
$cBox2 = New-Object “System.Windows.Forms.combobox”;
$cBox2.Left = 150;
$cBox2.Top = 80;
$cBox2.width = 200;
###############"Add descriptions to combo box"##############
$NameHash = #{}
import-csv "C:\Users\Komp\Desktop\1.csv" | ForEach-Object {
$cBox2.Items.Add($_.ServerName)
}
#############define OK button
$button = New-Object “System.Windows.Forms.Button”;
$button.Left = 360;
$button.Top = 45;
$button.Width = 100;
$button.Text = “Ok”;
$Button.Cursor = [System.Windows.Forms.Cursors]::Hand
$Button.Font = New-Object System.Drawing.Font("Times New Roman",12,[System.Drawing.FontStyle]::BOLD)
############# This is when you have to close the form after getting values
$eventHandler = [System.EventHandler]{
$cBox2.Text;
$form.Close();};
$button.Add_Click($eventHandler) ;
#############Add controls to all the above objects defined
$form.Controls.Add($button);
$form.Controls.Add($textLabel2);
$form.Controls.Add($cBox2);
#$ret = $form.ShowDialog();
#################return values
$button.add_Click({
#$output = $cBox2.SelectedItem.Text #Your answer here
Set-Variable -Name locationResult -Value $combobox1.selectedItem -Force -Scope Script # Use this
$script:locationResult = $cBox2.selectedItem # or this to retrieve the user selection
})
$form.Controls.Add($button)
$form.Controls.Add($cBox2)
$form.ShowDialog()
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.
I have the below PowerShell Script that takes a users input and injects it into an automated SSRS install. Everything is working as intended except for the Cancel button. I would like the user to be able to click Cancel and stop the script from proceeding.
I am very new to PowerShell and self teaching so looking for some help.
function button ($title,$instance,$acct,$pass) {
###################Load Assembly for creating form & button######
[void][System.Reflection.Assembly]::LoadWithPartialName( “System.Windows.Forms”)
[void][System.Reflection.Assembly]::LoadWithPartialName( “Microsoft.VisualBasic”)
#####Define the form size & placement
$form = New-Object “System.Windows.Forms.Form”;
$form.Width = 500;
$form.Height = 160;
$form.Text = $title;
$form.StartPosition = [System.Windows.Forms.FormStartPosition]::CenterScreen;
##############Define text label1
$textLabel1 = New-Object “System.Windows.Forms.Label”;
$textLabel1.Left = 25;
$textLabel1.Top = 15;
$textLabel1.Text = $instance;
##############Define text label2
$textLabel2 = New-Object “System.Windows.Forms.Label”;
$textLabel2.Left = 25;
$textLabel2.Top = 50;
$textLabel2.Text = $acct;
##############Define text label3
$textLabel3 = New-Object “System.Windows.Forms.Label”;
$textLabel3.Left = 25;
$textLabel3.Top = 85;
$textLabel3.Text = $pass;
############Define text box1 for input
$textBox1 = New-Object “System.Windows.Forms.TextBox”;
$textBox1.Left = 150;
$textBox1.Top = 10;
$textBox1.width = 200;
############Define text box2 for input
$textBox2 = New-Object “System.Windows.Forms.TextBox”;
$textBox2.Left = 150;
$textBox2.Top = 50;
$textBox2.width = 200;
############Define text box3 for input
$textBox3 = New-Object “System.Windows.Forms.TextBox”;
$TextBox3.Passwordchar = "*"
$textBox3.Left = 150;
$textBox3.Top = 90;
$textBox3.width = 200;
#############Define default values for the input boxes
$defaultValue = “”
$textBox1.Text = $defaultValue;
$textBox2.Text = $defaultValue;
$textBox3.Text = $defaultValue;
#############define OK button
$button = New-Object “System.Windows.Forms.Button”;
$button.Left = 360;
$button.Top = 85;
$button.Width = 100;
$button.Text = “Submit”;
#############define CANCEL button
$button2 = New-Object “System.Windows.Forms.Button”;
$button2.Left = 360;
$button2.Top = 45;
$button2.Width = 100;
$button2.Text = “Cancel”;
############# This is when you have to close the form after getting values
$eventHandler = [System.EventHandler]{
$textBox1.Text;
$textBox2.Text;
$textBox3.Text;
$form.Close();};
$button.Add_Click($eventHandler) ;
#############Add controls to all the above objects defined
$form.Controls.Add($button);
$form.Controls.Add($button2);
$form.Controls.Add($textLabel1);
$form.Controls.Add($textLabel2);
$form.Controls.Add($textLabel3);
$form.Controls.Add($textBox1);
$form.Controls.Add($textBox2);
$form.Controls.Add($textBox3);
$ret = $form.ShowDialog();
#################return values
return $textBox1.Text, $textBox2.Text, $textBox3.Text
}
$return= button “SSRS Configuration” “Instance Name” “Domain\ServiceID” “Password”
#Below variables will get the values that had been entered by the user
$return[0]
$return[1]
$return[2]
D:\SQL2016\"SQL Server 2016 SP1"\Setup.exe /q /IACCEPTSQLSERVERLICENSETERMS /ACTION="install" /USEMICROSOFTUPDATE="False" /INDICATEPROGRESS /INSTANCENAME="$($return[0])" /FEATURES="RS" /RSINSTALLMODE="FilesOnlyMode" /INSTANCEDIR="D:\Program Files\Microsoft SQL Server" /RSSVCACCOUNT="$($return[1])" /RSSVCPASSWORD="$($return[2])"
Below a rewrite of your code. I left most of it intact, except for:
I removed the semi-colons because they are not needed in PowerShell
I replaced all curly quotes with straight ones
Both buttons now have a defined DialogResult
I changed the deprecated LoadWithPartialName lines
Added a $form.Dispose()
Checked if the dialog exited with OK
I also use the Start-Process cmdlet to finally execute the exe to make the code more readable and possible for you to examine the exit code from it, and.. INDENTED the code.
function button ($title,$instance,$acct,$pass) {
###################Load Assembly for creating form & button######
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName Microsoft.VisualBasic
#####Define the form size & placement
$form = New-Object "System.Windows.Forms.Form"
$form.Width = 500
$form.Height = 160
$form.Text = $title
$form.StartPosition = [System.Windows.Forms.FormStartPosition]::CenterScreen
##############Define text label1
$textLabel1 = New-Object "System.Windows.Forms.Label"
$textLabel1.Left = 25
$textLabel1.Top = 15
$textLabel1.Text = $instance
##############Define text label2
$textLabel2 = New-Object "System.Windows.Forms.Label"
$textLabel2.Left = 25
$textLabel2.Top = 50
$textLabel2.Text = $acct
##############Define text label3
$textLabel3 = New-Object "System.Windows.Forms.Label"
$textLabel3.Left = 25
$textLabel3.Top = 85
$textLabel3.Text = $pass
############Define text box1 for input
$textBox1 = New-Object "System.Windows.Forms.TextBox"
$textBox1.Left = 150
$textBox1.Top = 10
$textBox1.width = 200
############Define text box2 for input
$textBox2 = New-Object "System.Windows.Forms.TextBox"
$textBox2.Left = 150
$textBox2.Top = 50
$textBox2.width = 200
############Define text box3 for input
$textBox3 = New-Object "System.Windows.Forms.TextBox"
$TextBox3.Passwordchar = "*"
$textBox3.Left = 150
$textBox3.Top = 90
$textBox3.width = 200
#############Define default values for the input boxes
$defaultValue = ""
$textBox1.Text = $defaultValue
$textBox2.Text = $defaultValue
$textBox3.Text = $defaultValue
#############define OK button
$button = New-Object "System.Windows.Forms.Button"
$button.Left = 360
$button.Top = 85
$button.Width = 100
$button.Text = "Submit"
$button.DialogResult = [System.Windows.Forms.DialogResult]::OK
#############define CANCEL button
$button2 = New-Object "System.Windows.Forms.Button"
$button2.Left = 360
$button2.Top = 45
$button2.Width = 100
$button2.Text = "Cancel"
$button2.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
############# This is when you have to close the form after getting values
$eventHandler = [System.EventHandler]{
$textBox1.Text
$textBox2.Text
$textBox3.Text
$form.Close()
}
$button.Add_Click($eventHandler)
#############Add controls to all the above objects defined
$form.Controls.Add($button)
$form.Controls.Add($button2)
$form.Controls.Add($textLabel1)
$form.Controls.Add($textLabel2)
$form.Controls.Add($textLabel3)
$form.Controls.Add($textBox1)
$form.Controls.Add($textBox2)
$form.Controls.Add($textBox3)
$ret = $form.ShowDialog()
$result = $null
#################return values
if ($ret -eq 'OK') {
# if NOT cancelled, return whatever is in the 3 text boxes
$result = $textBox1.Text, $textBox2.Text, $textBox3.Text
}
# Dispose of the form
$form.Dispose()
return $result
}
$return= button "SSRS Configuration" "Instance Name" "Domain\ServiceID" "Password"
# test if the function returned anything, otherwise it was cancelled
if ($return) {
#Below variables will get the values that had been entered by the user
$return[0]
$return[1]
$return[2]
# for better readability, create the arguments for the Setup.exe as array
$arguments = '/q',
'/IACCEPTSQLSERVERLICENSETERMS',
'/ACTION="install"',
'/USEMICROSOFTUPDATE="False"',
'/INDICATEPROGRESS',
('/INSTANCENAME="{0}"' -f $return[0]),
'/FEATURES="RS"',
'/RSINSTALLMODE="FilesOnlyMode"',
'/INSTANCEDIR="D:\Program Files\Microsoft SQL Server"',
('/RSSVCACCOUNT="{0}"' -f $return[1]),
('/RSSVCPASSWORD="{0}"' -f $return[2])
$proc = Start-Process -FilePath "D:\SQL2016\SQL Server 2016 SP1\Setup.exe" -ArgumentList $arguments -PassThru -Wait
# you can examine the processes ExitCode using:
Write-Host "The process returned ExitCode: $($proc.ExitCode)"
}
else {
Write-Host "User cancelled the dialog.."
}
Hope that helps
I Am trying to create a drop down box in my form that is set up with two text fields. However when i try to add the dropdown to the form it changes the end values, even though I am not changing the type or answer, it changes the value it returns. The original code I wrote that runs through perfect is:
function button ($title,$mailbx, $WF, $TF) {
###################Load Assembly for creating form & button######
[void][System.Reflection.Assembly]::LoadWithPartialName( “System.Windows.Forms”)
[void][System.Reflection.Assembly]::LoadWithPartialName( “Microsoft.VisualBasic”)
#####Define the form size & placement
$form = New-Object “System.Windows.Forms.Form”;
$form.Width = 750;
$form.Height = 500;
$form.Text = $title;
$form.StartPosition = [System.Windows.Forms.FormStartPosition]::CenterScreen;
##############Define text label1
$textLabel1 = New-Object “System.Windows.Forms.Label”;
$textLabel1.Left = 25;
$textLabel1.Top = 15;
$textLabel1.Text = $mailbx;
##############Define text label2
$textLabel2 = New-Object “System.Windows.Forms.Label”;
$textLabel2.Left = 25;
$textLabel2.Top = 50;
$textLabel2.Text = $WF;
##############Define text label3
$textLabel3 = New-Object “System.Windows.Forms.Label”;
$textLabel3.Left = 25;
$textLabel3.Top = 85;
$textLabel3.Text = $TF;
############Define text box1 for input
$textBox1 = New-Object “System.Windows.Forms.TextBox”;
$textBox1.Left = 150;
$textBox1.Top = 10;
$textBox1.width = 200;
############Define text box2 for input
$textBox2 = New-Object “System.Windows.Forms.TextBox”;
$textBox2.Left = 150;
$textBox2.Top = 50;
$textBox2.width = 200;
############Define text box3 for input
$textBox3 = New-Object “System.Windows.Forms.TextBox”;
$textBox3.Left = 150;
$textBox3.Top = 90;
$textBox3.width = 200;
#############Define default values for the input boxes
$defaultValue = “”
$textBox1.Text = $defaultValue;
$textBox2.Text = $defaultValue;
$textBox3.Text = $defaultValue;
#############define OK button
$button = New-Object “System.Windows.Forms.Button”;
$button.Left = 360;
$button.Top = 85;
$button.Width = 100;
$button.Text = “Ok”;
############# This is when you have to close the form after getting values
$eventHandler = [System.EventHandler]{
$textBox1.Text;
$textBox2.Text;
$textBox3.Text;
$form.Close();};
$button.Add_Click($eventHandler) ;
#############Add controls to all the above objects defined
$form.Controls.Add($button);
$form.Controls.Add($textLabel1);
$form.Controls.Add($textLabel2);
$form.Controls.Add($textLabel3);
$form.Controls.Add($textBox1);
$form.Controls.Add($textBox2);
$form.Controls.Add($textBox3);
$ret = $form.ShowDialog();
#################return values
return $textBox1.Text, $textBox2.Text, $textBox3.Text
}
$return= button “Enter Info” “First Name” “Last Name” “Email Address”
$return2 = ($return[0] + " " + $return[1])
$return3 = ($return[0] + "." + $return[1])
$return4 = $return[0] + "." + $return[1] + "$return[2]"
New-Mailbox -Alias $return3 -Name $return2 -FirstName $return[0] -LastName $return[1] -UserPrincipalName $return4 -Password (ConvertTo-SecureString -String 'P#ssw0rd' -AsPlainText -Force) -ResetPasswordOnNextLogon $true
Set-User -Identity $return3 -StreetAddress '1600 Pennsylvania Ave NW' -City 'Washington' -StateOrProvince 'D.C.' -PostalCode '20500' -Phone '202-456-1111' -Fax '202-456-2461'
The drop down code I have is
########################
# Edit This item to change the DropDown Values
[array]$DropDownArray = "#yahoo.com", "#gmail.com", "#lewisJ.com"
# This Function Returns the Selected Value and Closes the Form
function Return-DropDown {
$Choice = $DropDown.SelectedItem.ToString()
}
[System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
$DropDown = new-object System.Windows.Forms.ComboBox
$DropDown.Location = new-object System.Drawing.Size(400,10)
$DropDown.Size = new-object System.Drawing.Size(130,30)
ForEach ($Item in $DropDownArray) {
$DropDown.Items.Add($Item)
}
$Form.Controls.Add($DropDown)
$DropDownLabel = new-object System.Windows.Forms.Label
$DropDownLabel.Location = new-object System.Drawing.Size(10,10)
$DropDownLabel.size = new-object System.Drawing.Size(100,20)
$DropDownLabel.Text = "Items"
$Form.Controls.Add($DropDownLabel)
$Button = new-object System.Windows.Forms.Button
$Button.Location = new-object System.Drawing.Size(100,50)
$Button.Size = new-object System.Drawing.Size(100,20)
$Button.Text = "OK"
$Button.Add_Click({Return-DropDown})
$form.Controls.Add($Button)
I am trying to have it so if I enter the first name as Ben, Last name as Don, and use the drop down feature and select #gmail.com. it will return those values. When I tried to combine the two codes it changed all the values to :
System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d5 0a3a System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089.System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d5 0a3a System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089.System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d5 0a3a0
Anyone know how to get this drop down correct?
It seems once again that you are in need of voiding some return values from the methods in your code. If you look at TechNet for a list box (Yes I know we have a drop down) you will see that on top of voiding
[void][System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[void][System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
You also have to address the Add method as well.
[void]$DropDown.Items.Add($Item)
That should ensure your returns are the ones you want. You might still have an issue with the value from the drop down but this will get you in the right direction.