Exchange PowerShell dropdown issue - powershell

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.

Related

PowerShell - Get value from combobox, selected value is empty

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()

IF in a Function not working (PowerShell)

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

Cancel Button in a PowerShell Script

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

Problems with Powershell and imageboxes

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

Powershell If else error

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?