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
Related
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 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()
}
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.
I currently have a script that is adding licenses to a specific user. It uses an If Else statement to add the appropriate license but for some reason the if else statement does not work. it always picks the first license. here is my script:
function button ($title,$FN, $LN, $EM, $PO, $SO, $EI, $LI, $DP) {
###################Load Assembly for creating form & button######
[void][System.Reflection.Assembly]::LoadWithPartialName( “System.Windows.Forms”)
[void][System.Reflection.Assembly]::LoadWithPartialName( “Microsoft.VisualBasic”)
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
#####Define the form size & placement
$form = New-Object “System.Windows.Forms.Form”;
$form.Width = 500;
$form.Height = 400;
$form.Text = $title;
$form.StartPosition = [System.Windows.Forms.FormStartPosition]::CenterScreen;
##############Define firstNameLabel
$firstNameLabel = New-Object “System.Windows.Forms.Label”;
$firstNameLabel.Left = 25;
$firstNameLabel.Top = 15;
$firstNameLabel.Text = $FN;
##############Define text lastNameLabel
$lastNameLabel = New-Object “System.Windows.Forms.Label”;
$lastNameLabel.Left = 25;
$lastNameLabel.Top = 52;
$lastNameLabel.Text = $LN;
##############Define text eMail
$eMail = New-Object “System.Windows.Forms.Label”;
$eMail.Left = 25;
$eMail.Top = 95;
$eMail.Text = $EM;
##############Define text position
$position = New-Object “System.Windows.Forms.Label”;
$position.Left = 25;
$position.Top = 132;
$position.Text = $PO;
##############Define text store
$store = New-Object “System.Windows.Forms.Label”;
$store.Left = 25;
$store.Top = 175;
$store.Text = $SO;
##############Define employeeID
$employeeID = New-Object “System.Windows.Forms.Label”;
$employeeID.Left = 25;
$employeeID.Top = 215;
$employeeID.Text = $EI;
##############Define text License
$License = New-Object “System.Windows.Forms.Label”;
$License.Left = 25;
$License.Top = 295;
$License.Text = $LI;
##############Define text Department
$Department = New-Object “System.Windows.Forms.Label”;
$Department.Left = 25;
$Department.Top = 255;
$Department.Text = $DP;
############Define text firstNameBox for input
$firstNameBox = New-Object “System.Windows.Forms.TextBox”;
$firstNameBox.Left = 150;
$firstNameBox.Top = 10;
$firstNameBox.width = 200;
############Define text lastNameBox for input
$lastNameBox = New-Object “System.Windows.Forms.TextBox”;
$lastNameBox.Left = 150;
$lastNameBox.Top = 50;
$lastNameBox.width = 200;
############Define text Dropdown(Email) for input
[array]$eMailArray = "#yahoo.com", "#gmail.com", "#live.com"
function Return-DropDown {
$Choice = $DropDown.SelectedItem.ToString()
}
$DropDown = new-object System.Windows.Forms.ComboBox
$DropDown.Location = new-object System.Drawing.Size(150,90)
$DropDown.Size = new-object System.Drawing.Size(200,30)
ForEach ($Item in $eMailArray) {
[void] $DropDown.Items.Add($Item)
}
$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"
############Define text (position) for input
[array]$positionArray = "Assistant Manager", "Assistant Operations Manager", "N/A"
function Return-DropDown1 {
$Choice1 = $DropDown1.SelectedItem.ToString()
}
$DropDown1 = new-object System.Windows.Forms.ComboBox
$DropDown1.Location = new-object System.Drawing.Size(150,130)
$DropDown1.Size = new-object System.Drawing.Size(200,30)
ForEach ($Item in $positionArray) {
[void] $DropDown1.Items.Add($Item)
}
$DropDownLabel1 = new-object System.Windows.Forms.Label
$DropDownLabel1.Location = new-object System.Drawing.Size(10,10)
$DropDownLabel1.size = new-object System.Drawing.Size(100,20)
$DropDownLabel1.Text = "Items"
############Define text (store) for input
[array]$storeArray = "15 Duluth, GA", "16 Indianapolis, IN", "17 Louisville, KY"
function Return-DropDown2 {
$Choice2 = $DropDown2.SelectedItem.ToString()
}
$DropDown2 = new-object System.Windows.Forms.ComboBox
$DropDown2.Location = new-object System.Drawing.Size(150,170)
$DropDown2.Size = new-object System.Drawing.Size(200,30)
ForEach ($Item in $storeArray) {
[void] $DropDown2.Items.Add($Item)
}
$DropDownLabel2 = new-object System.Windows.Forms.Label
$DropDownLabel2.Location = new-object System.Drawing.Size(10,10)
$DropDownLabel2.size = new-object System.Drawing.Size(100,20)
$DropDownLabel2.Text = "Items"
############Define text employeeIDBox for input
$employeeIDBox = New-Object “System.Windows.Forms.TextBox”;
$employeeIDBox.Left = 150;
$employeeIDBox.Top = 215;
$employeeIDBox.width = 200;
############Define text Dropdown(Department) for input
[array]$DepartmentArray = "Accounting", "Admin", "HR" , "IS", "Store Operations"
function Return-DropDown3 {
$Choice3 = $DropDown3.SelectedItem.ToString()
}
$DropDown3 = new-object System.Windows.Forms.ComboBox
$DropDown3.Location = new-object System.Drawing.Size(150,255)
$DropDown3.Size = new-object System.Drawing.Size(200,30)
ForEach ($Item in $DepartmentArray) {
[void] $DropDown3.Items.Add($Item)
}
$DropDownLabel3 = new-object System.Windows.Forms.Label
$DropDownLabel3.Location = new-object System.Drawing.Size(10,10)
$DropDownLabel3.size = new-object System.Drawing.Size(100,20)
$DropDownLabel3.Text = "Items"
############Define text Dropdown(License) for input
[array]$LicenseArray = "K1" , "E4"
function Return-DropDown4 {
$Choice4 = $DropDown4.SelectedItem.ToString()
}
$DropDown4 = new-object System.Windows.Forms.ComboBox
$DropDown4.Location = new-object System.Drawing.Size(150,295)
$DropDown4.Size = new-object System.Drawing.Size(200,30)
ForEach ($Item in $LicenseArray) {
[void] $DropDown4.Items.Add($Item)
}
$DropDownLabel4 = new-object System.Windows.Forms.Label
$DropDownLabel4.Location = new-object System.Drawing.Size(10,10)
$DropDownLabel4.size = new-object System.Drawing.Size(100,20)
$DropDownLabel4.Text = "Items"
#############Define default values for the input boxes
$defaultValue = “”
$firstNameBox.Text = $defaultValue;
$lastNameBox.Text = $defaultValue;
$DropDownLabel.Text = $defaultValue;
$DropDownLabel1.Text = $defaultValue;
$DropDownLabel2.Text = $defaultValue;
$DropDownLabel2.Text = $defaultValue;
$DropDownLabel3.Text = $defaultValue;
$DropDownLabel4.Text = $defaultValue;
$employeeIDBox.Text = $defaultValue;
#############define OK button
$button = New-Object “System.Windows.Forms.Button”;
$button.Left = 360;
$button.Top = 295;
$button.Width = 100;
$button.Text = “Ok”;
############# This is when you have to close the form after getting values
$eventHandler = [System.EventHandler]{
$firstNameBox.Text;
$lastNameBox.Text;
$Choice.Text;
$script:Choice = $DropDown.SelectedItem.ToString()
$script:Choice1 = $DropDown1.SelectedItem.ToString()
$script:Choice2 = $DropDown2.SelectedItem.ToString()
$script:Choice3 = $DropDown3.SelectedItem.ToString()
$script:Choice4 = $DropDown4.SelectedItem.ToString()
$form.Close();};
$button.Add_Click($eventHandler) ;
$button.Add_Click({Return-DropDown})
$button.Add_Click({Return-DropDown1})
$button.Add_Click({Return-DropDown2})
$button.Add_Click({Return-DropDown3})
$button.Add_Click({Return-DropDown4})
#############Add controls to all the above objects defined
$form.Controls.Add($button);
$form.Controls.Add($firstNameLabel);
$form.Controls.Add($lastNameLabel);
$form.Controls.Add($eMail);
$form.Controls.Add($position);
$form.Controls.Add($store);
$form.Controls.Add($employeeID);
$form.Controls.Add($License);
$form.Controls.Add($Department);
$form.Controls.Add($firstNameBox);
$form.Controls.Add($lastNameBox);
$Form.Controls.Add($DropDownLabel);
$Form.Controls.Add($DropDown);
$Form.Controls.Add($DropDownLabel1);
$Form.Controls.Add($DropDown1);
$Form.Controls.Add($DropDownLabel2);
$Form.Controls.Add($DropDown2);
$Form.Controls.Add($employeeIDBox);
$Form.Controls.Add($DropDownLabel3);
$Form.Controls.Add($DropDown3);
$Form.Controls.Add($DropDownLabel4);
$Form.Controls.Add($DropDown4);
$ret = $form.ShowDialog();
#################return values
return $firstNameBox.Text, $lastNameBox.Text, $script:choice, $script:choice1, $script:choice2, $employeeIDBox.Text, $script:choice3, $script:choice4
}
$return= button “Enter Info” “First Name” “Last Name” “Email Address” "Position" "Store" "Employee ID" "License" "Department"
$name = $return[0] + " " + $return[1]
$identAlias = $return[0] + "." + $return[1]
$eAddress = $return[0] + "." + $return[1] + $return[2]
$fillEmployeeID = "Employee ID: " + $return[5]
$store = $return[4]
$storePosition = $return[3]
$e = $store.Substring(0,2)
$ADdescription = "Store " + $e + ' - ' + $storePosition
$f = $store.Substring(3)
$g = $store.Substring(0,3)
$pathname = $e + " - " + $f
New-RemoteMailBox -Alias $identAlias -Name $name -FirstName $return[0] -LastName $return[1] -UserPrincipalName $eAddress -Password (ConvertTo-SecureString -String 'password' -AsPlainText -Force) -ResetPasswordOnNextLogon $true -OnPremisesOrganizationalUnit ("xxx.com/users/" + $return[6])
}
####LOGS INTO 0365 #####
$User = “xxxxxxxxxxxxx”
$Pass = “xxxxxxxxxxxxx”
$Cred = New-Object System.Management.Automation.PsCredential($User,(ConvertTo-SecureString $Pass -AsPlainText -Force))
Import-Module MSOnline
Connect-MsolService -Credential $Cred
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell/ -Credential $Cred -Authentication Basic -AllowRedirection
Import-PSSession $Session
###Holds of till DIRSYNC takes Effect ###
do {
sleep -seconds 1
$mailboxExists = get-msoluser -UserPrincipalName $eAddress -ErrorAction SilentlyContinue
write-host "." -nonewline
}
while (!$mailboxExists)
#### ADDS LICENSE TO USER IN 0365####
write-host $return[7]
if($return[7] = "K1"){
Set-MsolUser -UserPrincipalName $eAddress -UsageLocation US
Set-MsolUserLicense -UserPrincipalName $eAddress -AddLicenses Company:DESKLESSPACK_YAMMER
}
else{
Set-MsolUser -UserPrincipalName $eAddress -UsageLocation US
Set-MsolUserLicense -UserPrincipalName $eAddress -AddLicenses Company:ENTERPRISEWITHSCAL
}
Return[7] is either "K1" or "E4"
I have even done a write-host and it spits out E4 but still selects the K1 part of the statement. any ideas?