Piping information from text box - powershell

I have a script that requires input from the user in a textbox. Once the user hits enter or clicks OK the data in the textbox must be transferred to a command. Here is the textbox portion of the code. I am just a couple months in to PS. Feel free to criticize :)
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
$objForm = New-Object System.Windows.Forms.Form;
$objForm.Text = "Enter Patch Number";
$objForm.Size = New-Object System.Drawing.Size(100,100);
$objForm.StartPosition = "CenterScreen";
$InputBox = New-Object System.Windows.Forms.TextBox
$InputBox.Location = New-Object System.Drawing.Size(20,50)
$InputBox.Size = New-Object System.Drawing.Size(150,20)
$Form.Controls.Add($InputBox)
$Button = New-Object System.Windows.Forms.Button
$Button.Location = New-Object System.Drawing.Size(400,30)
$Button.Size = New-Object System.Drawing.Size(110,80)
$Button.Text = "Send to Patch Command"
$Button.Add_Click({$version})
$Form.Controls.Add($Button)
$wks=$InputBox.text
.\anp deploypatch -patch=$version
$Form.Add_Shown({$Form.Activate()})
[void] $Form.ShowDialog()
This is the command that must have the user input piped to the end. An example of this cmd being executed correctly is...
.\anp deploypatch -patch=437.1237
In other words, the user types in the item in bold above and hits enter and the command is executed based on the version the user inputs. Main thing I want here is how to xfer the users input in to my desired command.

You can try the following code. In your original code $form does not exist, the control position were random, and the enter key was not linked to the button.
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
$objForm = New-Object System.Windows.Forms.Form;
$objForm.Text = "Enter Patch Number";
$objForm.Size = New-Object System.Drawing.Size(200,200);
$objForm.StartPosition = "CenterScreen";
$InputBox = New-Object System.Windows.Forms.TextBox
$InputBox.Location = New-Object System.Drawing.Point(20,20)
$InputBox.Size = New-Object System.Drawing.Size(150,20)
$objForm.Controls.Add($InputBox)
$Button = New-Object System.Windows.Forms.Button
$Button.Location = New-Object System.Drawing.Point(50,50)
$Button.Size = New-Object System.Drawing.Size(110,80)
$Button.Text = "Send to Patch Command"
$Button.Add_Click({$objForm.Close()})
$objForm.AcceptButton = $Button # the button is linked to the enter key
$objForm.Controls.Add($Button)
$wks=$InputBox.text
$objForm.Add_Shown({$objForm.Activate()})
[void] $objForm.ShowDialog()
$version = $InputBox.Text
#$version
.\anp deploypatch -patch=$version

The below seems to do the trick let me know how you get on.
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
$Form = New-Object System.Windows.Forms.Form;
$Form.Text = "Enter Patch Number";
$Form.Size = New-Object System.Drawing.Size(500,200);
$Form.StartPosition = "CenterScreen";
$InputBox = New-Object System.Windows.Forms.TextBox
$InputBox.Location = New-Object System.Drawing.Size(20,50)
$InputBox.Size = New-Object System.Drawing.Size(150,20)
$InputBox.text = ""
$Form.Controls.Add($InputBox)
$Button = New-Object System.Windows.Forms.Button
$Button.Location = New-Object System.Drawing.Size(400,30)
$Button.Size = New-Object System.Drawing.Size(110,80)
$Button.Text = "Send to Patch Command"
$Button.Add_Click({patch})
$Form.Controls.Add($Button)
Function patch {
$wks=$InputBox.text
Invoke-Expression "\anp deploypatch -patch='$wks'"
}
$Form.Add_Shown({$Form.Activate()})
[void] $Form.ShowDialog()

Related

Powershell Form With Two Text Inputs and drop down selection

I'm very rusty in the area of powershell and trying to pick it back up but I have something bothering me that I can't seem to figure out. I've created a form that has two text inputs for file locations and a drop down list. What I'm attempting to do is compare the two files received in a text input. I've got the script already for the compare but what I'm trying to figure out is how I distinguish the script to be run from the selection of the drop down list and also parse the two text inputs from the form to the relevant compare script. Below is the generic form created. Any help would be much appreciated.
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[void] [System.Windows.Forms.Application]::EnableVisualStyles()
$Form = New-Object system.Windows.Forms.Form
$Form.Size = New-Object System.Drawing.Size(700,700)
#You can use the below method as well
#$Form.Width = 400
#$Form.Height = 200
$form.MaximizeBox = $false
$Form.StartPosition = "CenterScreen"
$Form.FormBorderStyle = 'Fixed3D'
$Form.Text = "IAM Application Comparison Tool"
#application Drop down list
$Label = New-Object System.Windows.Forms.Label
$Label.Text = "Please select the application being compared"
$Label.AutoSize = $true
$Label.Location = New-Object System.Drawing.Size(10,10)
$Font = New-Object System.Drawing.Font("Arial",15,[System.Drawing.FontStyle]::Bold)
$form.Font = $Font
$Form.Controls.Add($Label)
#list of applications
$listBox = New-Object System.Windows.Forms.ListBox
$listBox.Location = New-Object System.Drawing.Point(10,50)
$listBox.Size = New-Object System.Drawing.Size(260,20)
$listBox.Height = 80
[void] $listBox.Items.Add('Terradata')
[void] $listBox.Items.Add('')
[void] $listBox.Items.Add('')
[void] $listBox.Items.Add('')
[void] $listBox.Items.Add('')
[void] $listBox.Items.Add('')
[void] $listBox.Items.Add('')
$form.Controls.Add($listBox)
#File path of previous months file
$Label = New-Object System.Windows.Forms.Label
$Label.Text = "Enter file path of previous months file"
$Label.AutoSize = $true
$Label.Location = New-Object System.Drawing.Size(20,200)
$Font = New-Object System.Drawing.Font("Arial",15,[System.Drawing.FontStyle]::Bold)
$form.Font = $Font
$Form.Controls.Add($Label)
#Input file path
$textBox = New-Object System.Windows.Forms.TextBox
$textBox.Location = New-Object System.Drawing.Point(20,300)
$textBox.Size = New-Object System.Drawing.Size(450,20)
$form.Controls.Add($textBox)
#$formIcon = New-Object system.drawing.icon ("$env:USERPROFILE\desktop\Blog\v.ico")
#$form.Icon = $formicon
#File path of current month
$Label = New-Object System.Windows.Forms.Label
$Label.Text = "Enter file path of current months file"
$Label.AutoSize = $true
$Label.Location = New-Object System.Drawing.Size(20,400)
$Font = New-Object System.Drawing.Font("Arial",15,[System.Drawing.FontStyle]::Bold)
$form.Font = $Font
$Form.Controls.Add($Label)
#Input file path
$textBox = New-Object System.Windows.Forms.TextBox
$textBox.Location = New-Object System.Drawing.Point(20,500)
$textBox.Size = New-Object System.Drawing.Size(450,20)
$form.Controls.Add($textBox)
#Run Required Compare script
$Okbutton = New-Object System.Windows.Forms.Button
$Okbutton.Location = New-Object System.Drawing.Size(170,600)
$Okbutton.Size = New-Object System.Drawing.Size(200,30)
$Okbutton.Text = "Compare Files"
$Okbutton.Add_Click()
$Form.Controls.Add($Okbutton)
$Form.ShowDialog()
Create a function that will run whenever the button is clicked.
for instance, your button event will be:
$Okbutton.Add_Click({ BtnClick })
then add the function that will do the compare, whenever the button is clicked the function will be called:
function BtnClick{
# do something / compare your texts
#compare $TextBox1.Text $TextBox2.Text
}
you have to give a different variable name for each textbox, this how you'll be able to access the values:
$input1 = $TextBox1.Text
$input2 = $TextBox2.Text
$input3 = $ComboBox1.Text
The easiest way to have your code distinguish between the different objects is to name them upon creation, so use different variable names for the textboxes and the like.
To act on the button click, you already started by writing $Okbutton.Add_Click(), but since there is no scriptblock to actually do something there, nothing would happen.
The listbox can also react on an event, in this case that would be the SelectedIndexChanged event.
Please see the revised code below that shows how to implement these event handlers
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
[void] [System.Windows.Forms.Application]::EnableVisualStyles()
$Form = New-Object system.Windows.Forms.Form
$Form.Size = New-Object System.Drawing.Size(700,700)
$Form.MaximizeBox = $false
$Form.StartPosition = "CenterScreen"
$Form.FormBorderStyle = 'Fixed3D'
$Form.Text = "IAM Application Comparison Tool"
$Font = New-Object System.Drawing.Font("Arial",15,[System.Drawing.FontStyle]::Bold)
$Form.Font = $Font
#application Drop down list
$LabelDropDown = New-Object System.Windows.Forms.Label
$LabelDropDown.Text = "Please select the application being compared"
$LabelDropDown.AutoSize = $true
$LabelDropDown.Location = New-Object System.Drawing.Size(10,10)
$Form.Controls.Add($LabelDropDown)
# list of applications
$listBox = New-Object System.Windows.Forms.ListBox
$listBox.Location = New-Object System.Drawing.Point(10,50)
$listBox.Size = New-Object System.Drawing.Size(260,20)
$listBox.Height = 80
# add the items to the listbox
$null = $listBox.Items.AddRange(#('Terradata','SomeApp','MyApp','ThirdPartyApp'))
# set the current selected item to be the first
$listBox.SelectedIndex = 0
# add functionality to react on index changed
$listBox.Add_SelectedIndexChanged({
# do something when the user chose a different app in the listbox
# for demo, just write the currently selected item in the console
Write-Host "Currently selected app in the listbox: $($this.SelectedItem)"
})
$Form.Controls.Add($listBox)
# File path of previous months file
$LabelPrevious = New-Object System.Windows.Forms.Label
$LabelPrevious.Text = "Enter file path of previous months file"
$LabelPrevious.AutoSize = $true
$LabelPrevious.Location = New-Object System.Drawing.Size(20,200)
$Form.Controls.Add($LabelPrevious)
# first Input file path
$textBoxFile1 = New-Object System.Windows.Forms.TextBox
$textBoxFile1.Location = New-Object System.Drawing.Point(20,300)
$textBoxFile1.Size = New-Object System.Drawing.Size(450,20)
$Form.Controls.Add($textBoxFile1)
# File path of current month
$LabelCurrent = New-Object System.Windows.Forms.Label
$LabelCurrent.Text = "Enter file path of current months file"
$LabelCurrent.AutoSize = $true
$LabelCurrent.Location = New-Object System.Drawing.Size(20,400)
$Form.Controls.Add($LabelCurrent)
# second Input file path
$textBoxFile2 = New-Object System.Windows.Forms.TextBox
$textBoxFile2.Location = New-Object System.Drawing.Point(20,500)
$textBoxFile2.Size = New-Object System.Drawing.Size(450,20)
$Form.Controls.Add($textBoxFile2)
# Run Required Compare script
$Okbutton = New-Object System.Windows.Forms.Button
$Okbutton.Location = New-Object System.Drawing.Size(170,600)
$Okbutton.Size = New-Object System.Drawing.Size(200,30)
$Okbutton.Text = "Compare Files"
# add functionality for comparing files
$Okbutton.Add_Click({
# your compare function comparing the file in $textbox1 against the file in $textbox2
# IF both fields contain a valid file path and name of course ;)
# for demo just output in console
Write-Host "Compare file '$($textBoxFile1.Text)' to '$($textBoxFile2.Text)'"
})
$Form.Controls.Add($Okbutton)
$Form.ShowDialog()
# important! dispose of the form when done
$Form.Dispose()
P.S. Inside the scriptblocks for the Click and the SelectedIndexChanged events, you can use automatic variable $this, which refers to the current form control object.

Do textbox while variable is empty in powershell

I'm new to powershell and I need some help with a script.
I have a simple code which loops while the user doesn't type a name :
do {$name = Read-Host "Choose a name "}
while (!$name) {}
I try to use it for the GUI version but the loop doesn't stop :
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
$box = {
$Form = New-Object System.Windows.Forms.Form
$Form.Text = "Hostname"
$Form.Size = New-Object System.Drawing.Size(270,150)
$Form.StartPosition = "CenterScreen"
$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Size(165,75)
$OKButton.Size = New-Object System.Drawing.Size(75,23)
$OKButton.Text = "OK"
$OKButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
$Form.AcceptButton = $OKButton
$Form.Controls.Add($OKButton)
$Label = New-Object System.Windows.Forms.Label
$Label.Location = New-Object System.Drawing.Size(10,15)
$Label.Size = New-Object System.Drawing.Size(280,20)
$Label.Text = "Choose a name :"
$Form.Controls.Add($Label)
$TextBox = New-Object System.Windows.Forms.TextBox
$TextBox.Location = New-Object System.Drawing.Size(10,40)
$TextBox.Size = New-Object System.Drawing.Size(230,20)
$Form.Controls.Add($TextBox)
$Form.Topmost = $True
$Form.Add_Shown({$TextBox.Select()})
$result = $Form.ShowDialog()
return $TextBox.Text
}
do {&$box}
while (!$TextBox.Text) {}
I think I'm missing something, but I don't know what...
Sorry for my poor english, thanks in advance.
Your textbox, being invoked, is never transmitted to its parent. Therefore, you need to assign your return value and works from there.
do {$value = &$box }
while ([String]::IsNullOrWhiteSpace($value))
Write-Host $value -ForegroundColor Cyan
Try this out.
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
# Null out name value in case you need to call the script multiple times in the same PS session.
$name = $null
$box = {
$Form = New-Object System.Windows.Forms.Form
$Form.Text = "Hostname"
$Form.Size = New-Object System.Drawing.Size(270,150)
$Form.StartPosition = "CenterScreen"
$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Size(165,75)
$OKButton.Size = New-Object System.Drawing.Size(75,23)
$OKButton.Text = "OK"
$OKButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
$Form.AcceptButton = $OKButton
$Form.Controls.Add($OKButton)
$Label = New-Object System.Windows.Forms.Label
$Label.Location = New-Object System.Drawing.Size(10,15)
$Label.Size = New-Object System.Drawing.Size(280,20)
$Label.Text = "Choose a name :"
$Form.Controls.Add($Label)
$TextBox = New-Object System.Windows.Forms.TextBox
$TextBox.Location = New-Object System.Drawing.Size(10,40)
$TextBox.Size = New-Object System.Drawing.Size(230,20)
$Form.Controls.Add($TextBox)
$Form.Topmost = $True
$Form.Add_Shown({$TextBox.Select()})
$result = $Form.ShowDialog()
# Only return if the TextBox.Text is set to stop it from exiting immediately after rendering the form.
if ($TextBox.Text) {return $TextBox.Text}
}
# While the name variable is null, show the form again.
while (-not $name) {
$name = & $box
}

How to program a command button to use a string in powershell

I am writing a Powershell script to automate the install of printers on our network. I have overthinking in place however, I cant seem to get my command button to allow the user to select the printer from a list and set it as default.
I have a string setup to define the printers (4 of them) but no matter what way I code the $OKButton.Add_Click it wont go with the users selection.
Here is the code I have. Can someone please tell me what I am missung?
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
$objForm = New-Object System.Windows.Forms.Form
$objForm.Text = "Select a Printer"
$objForm.Size = New-Object System.Drawing.Size(400,200)
$objForm.StartPosition = "CenterScreen"
$objForm.KeyPreview = $True
$objForm.Add_KeyDown({if ($_.KeyCode -eq "Enter")
{$x=$objListBox.SelectedItem;$objForm.Close()}})
$objForm.Add_KeyDown({if ($_.KeyCode -eq "Escape")
{$objForm.Close()}})
#Ok Button
$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Size(75,120)
$OKButton.Size = New-Object System.Drawing.Size(75,23)
$OKButton.Text = "OK"
$OKButton.Add_Click({$x=$objListBox.SelectedItem;$strPrinter,$objForm.Close()})
$objForm.Controls.Add($OKButton)
#Cancel Button
$CancelButton = New-Object System.Windows.Forms.Button
$CancelButton.Location = New-Object System.Drawing.Size(150,120)
$CancelButton.Size = New-Object System.Drawing.Size(75,23)
$CancelButton.Text = "Cancel"
$CancelButton.Add_Click({$objForm.Close()})
$objForm.Controls.Add($CancelButton)
$objLabel = New-Object System.Windows.Forms.Label
$objLabel.Location = New-Object System.Drawing.Size(10,20)
$objLabel.Size = New-Object System.Drawing.Size(280,20)
$objLabel.Text = "Please select a printer:"
$objForm.Controls.Add($objLabel)
#List box showing printer options
$objListBox = New-Object System.Windows.Forms.ListBox
$objListBox.Location = New-Object System.Drawing.Size(10,40)
$objListBox.Size = New-Object System.Drawing.Size(360,20)
$objListBox.Height = 80
[void] $objListBox.Items.Add("HP Color LaserJet CP2020")
[void] $objListBox.Items.Add("Brother DCP-8065DN")
[void] $objListBox.Items.Add("Canon iR-ADV C2220/2230")
[void] $objListBox.Items.Add("HP LJ300-400 color M351-M451")
$objForm.Controls.Add($objListBox)
$objForm.Topmost = $True
$objForm.Add_Shown({$objForm.Activate()})
[void] $objForm.ShowDialog()
#String to call printers, each printer is assigned a value (1,2,3,4)
$strPrinter = 1, "HP Color LaserJet CP2020", ((New-Object -ComObject WScript.Network).SetDefaultPrinter('\\PS\PT01'))
$strPrinter = 2, "Brother DCP-8065DN", ((New-Object -ComObject WScript.Network).SetDefaultPrinter('\\PS\PT02'))
$strPrinter = 3, "Canon iR-ADV C2220/2230", ((New-Object -ComObject WScript.Network).SetDefaultPrinter('\\PS\PT03'))
$strPrinter = 4, "HP LJ300-400 color M351-M451", ((New-Object -ComObject WScript.Network).SetDefaultPrinter('\PS\PT04'))
$x
There are two problems with your current script.
The first one, that $x appears empty is due to a scoping issue. When inside the scope of the add_Click() event handler, $x is a local variable and its value won't be accessible outside of the event handler.
You could work around this by specifying a parent scope, like (notice the global: scope qualifier):
$global:x = $objListBox.SelectedItem
But still, nothing would happen, leading me to the second issue:
I'm not sure what you mean by "string setup", but your script basically ends up setting the last printer as the default whenever it runs.
You'll want to define the printers up front, before showing the dialog, and wrap the ((New-Object... statements in a scriptblock, something like:
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
# New array of "printer objects", rather than $strPrinter
$Printers = #(
New-Object psobject -Property #{
Name = "HP Color LaserJet CP2020"
SetCommand = { ((New-Object -ComObject WScript.Network).SetDefaultPrinter('\\PS\PT01')) }
},
New-Object psobject -Property #{
Name = "Brother DCP-8065DN"
SetCommand = { ((New-Object -ComObject WScript.Network).SetDefaultPrinter('\\PS\PT02')) }
},
New-Object psobject -Property #{
Name = "Canon iR-ADV C2220/2230"
SetCommand = { ((New-Object -ComObject WScript.Network).SetDefaultPrinter('\\PS\PT03')) }
},
New-Object psobject -Property #{
Name = "HP LJ300-400 color M351-M451"
SetCommand = { ((New-Object -ComObject WScript.Network).SetDefaultPrinter('\PS\PT04')) }
}
)
$objForm = New-Object System.Windows.Forms.Form
$objForm.Text = "Select a Printer"
$objForm.Size = New-Object System.Drawing.Size(400,200)
$objForm.StartPosition = "CenterScreen"
$objForm.KeyPreview = $True
$objForm.Add_KeyDown({if ($_.KeyCode -eq "Enter")
{$x=$objListBox.SelectedItem;$objForm.Close()}})
$objForm.Add_KeyDown({if ($_.KeyCode -eq "Escape")
{$objForm.Close()}})
#Ok Button
$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Size(75,120)
$OKButton.Size = New-Object System.Drawing.Size(75,23)
$OKButton.Text = "OK"
$OKButton.Add_Click({
# Grab the printer array index
$index = $objListBox.SelectedIndex
# Execute the appropriate command
& $Printers[$index].SetCommand
# Exit
$objForm.Close()
})
$objForm.Controls.Add($OKButton)
#Cancel Button
$CancelButton = New-Object System.Windows.Forms.Button
$CancelButton.Location = New-Object System.Drawing.Size(150,120)
$CancelButton.Size = New-Object System.Drawing.Size(75,23)
$CancelButton.Text = "Cancel"
$CancelButton.Add_Click({$objForm.Close()})
$objForm.Controls.Add($CancelButton)
$objLabel = New-Object System.Windows.Forms.Label
$objLabel.Location = New-Object System.Drawing.Size(10,20)
$objLabel.Size = New-Object System.Drawing.Size(280,20)
$objLabel.Text = "Please select a printer:"
$objForm.Controls.Add($objLabel)
#List box showing printer options
$objListBox = New-Object System.Windows.Forms.ListBox
$objListBox.Location = New-Object System.Drawing.Size(10,40)
$objListBox.Size = New-Object System.Drawing.Size(360,20)
$objListBox.Height = 80
foreach($Printer in $Printers){
[void] $objListBox.Items.Add($Printer.Name)
}
$objForm.Controls.Add($objListBox)
$objForm.Topmost = $True
$objForm.Add_Shown({$objForm.Activate()})
[void] $objForm.ShowDialog()
Since the printers are now added to the listbox in correct order, we can simply use the SelectedIndex property to find the original printer object and invoke the scriptblock that sets it as default

Assign keyboard shortcuts to buttons in Powershell script

What I would like to do is to assign a button to a shortcut key, for example $TurnOnButton as key "Q", and $TurnOffButton as key "W" on my keyboard.
So basically, in my example below. When the script is running and the form is present, I would be able to push button "Q" on my keyboard to run calculator, and press button "W" to terminate it.
Is this possible with PowerShell?
Code example:
Add-Type -AssemblyName System.Windows.Forms
function Return-TurnOff
{
$x = Stop-Process -ProcessName calc
$x
}
function Return-TurnOn
{
$x = Start-Process calc
$x
}
$form = New-Object System.Windows.Forms.Form
$form.Text = "Title of the form"
$form.Size = New-Object System.Drawing.Size(300,200)
$form.minimumSize = New-Object System.Drawing.Size(300,200)
$form.maximumSize = New-Object System.Drawing.Size(300,200)
$form.StartPosition = "CenterScreen"
$TurnOffButton = New-Object System.Windows.Forms.Button
$TurnOffButton.Location = New-Object System.Drawing.Point(10,125)
$TurnOffButton.Size = New-Object System.Drawing.Size(55,25)
$TurnOffButton.Text = "Turn Off"
$TurnOffButton.Add_Click({Return-TurnOff})
$form.AcceptButton = $TurnOffButton
$form.Controls.Add($TurnOffButton)
$TurnOnButton = New-Object System.Windows.Forms.Button
$TurnOnButton.Location = New-Object System.Drawing.Point(10,65)
$TurnOnButton.Size = New-Object System.Drawing.Size(55,25)
$TurnOnButton.Text = "Turn On"
$TurnOnButton.Add_Click({Return-TurnOn})
$form.AcceptButton = $TurnOnButton
$form.Controls.Add($TurnOnButton)
$CancelButton = New-Object System.Windows.Forms.Button
$CancelButton.Location = New-Object System.Drawing.Point(215,125)
$CancelButton.Size = New-Object System.Drawing.Size(55,25)
$CancelButton.Text = "Cancel"
$CancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
$form.CancelButton = $CancelButton
$form.Controls.Add($CancelButton)
$label = New-Object System.Windows.Forms.Label
$label.Location = New-Object System.Drawing.Point(10,15)
$label.Size = New-Object System.Drawing.Size(280,20)
$label.Text = "Label for textbox:"
$form.Controls.Add($label)
$textBox = New-Object System.Windows.Forms.TextBox
$textBox.Location = New-Object System.Drawing.Point(10,35)
$textBox.Size = New-Object System.Drawing.Size(260,20)
$form.Controls.Add($textBox)
$form.Topmost = $True
$form.Add_Shown({$textBox.Select()})
$result = $form.ShowDialog()
If you don't mind having the label on the button contain the letter that corresponds to the button AND pressing the Alt key, then it's very easy, just put an '&' before the letter in the button text you want to be the accelerator:
$TurnOnButton.Text = "Turn O&n"
$TurnOffButton.Text = "Turn O&ff"
would make Alt-N perform Turn On and Alt-F perform Turn Off.
A more complicated solution is to register for keyboard presses, but it will let you handle any keystroke whether or not Alt is pressed. $form|gm -MemberType event key* will show you the events whose name starts with "Key". You can then google for how to handle events from Powershell with WinForms.

Outputting to Convertto-html Cmdlet Powershell

Evening everyone,
I've spent a while having a play on google learning how to create a basic form with powershell. Currently I'm having a problem coming to grips with how to get this basic form to output to a html file at the click of a button I have managed to get it to work with the services ie -
Get-Service | ConvertTo-Html | Out-File "Location"
However i'm wanting to input data on my form and then once the submit button is pressed, it puts the data into a table and then outputs it to the html file.
Add-Type -AssemblyName System.Drawing
Add-Type -AssemblyName System.Windows.Forms
$Form = New-Object System.Windows.Forms.Form
$Form.Text = "BasicForm"
$Form.Width = 800
$Form.AutoScroll = $True
#VRN label
$objLabel = New-Object System.Windows.Forms.Label
$objLabel.Location = New-Object System.Drawing.Size(50,75)
$objLabel.Text = "Num:"
$objLabel.AutoSize = $True
$Form.Controls.Add($objLabel)
#VRN Textbox
$objtextbox = New-Object System.Windows.Forms.TextBox
$objtextbox.Location = New-Object System.Drawing.Size(100,75)
$objtextbox.Size = New-Object System.Drawing.Size(100,150)
$Form.Controls.Add($objtextbox)
#Title by Label
$objLabel = New-Object System.Windows.Forms.Label
$objLabel.Location = New-Object System.Drawing.Size(250,75)
$objLabel.Text = "Title:"
$objLabel.AutoSize = $True
$Form.Controls.Add($objLabel)
#Title by Dropdownbox
$objtitlelist = New-Object System.Windows.Forms.ComboBox
$objtitlelist.Location = New-Object System.Drawing.Size(350,75)
$objtitlelist.Size = New-Object System.Drawing.Size(125,50)
$objtitlelist.Items.Add("Mr")
$objtitlelist.Items.Add("Mrs")
$objtitlelist.Items.Add("Ms")
$Form.Controls.Add($objtitlelist)
#Title by Textbox
$objtextbox = New-Object System.Windows.Forms.TextBox
$objtextbox.Location = New-Object System.Drawing.Size(500,75)
$objtextbox.Size = New-Object System.Drawing.Size(100,150)
$Form.Controls.Add($objtextbox)
#Title by Label
$objLabel = New-Object System.Windows.Forms.Label
$objLabel.Location = New-Object System.Drawing.Size(250,125)
$objLabel.Text = "Title:"
$objLabel.AutoSize = $True
$Form.Controls.Add($objLabel)
#Title by Dropdownbox
$objtitlelist = New-Object System.Windows.Forms.ComboBox
$objtitlelist.Location = New-Object System.Drawing.Size(350,125)
$objtitlelist.Size = New-Object System.Drawing.Size(125,50)
$objtitlelist.Items.Add("Mr")
$objtitlelist.Items.Add("Mrs")
$objtitlelist.Items.Add("Ms")
$Form.Controls.Add($objtitlelist)
#Title by Textbox
$objtextbox = New-Object System.Windows.Forms.TextBox
$objtextbox.Location = New-Object System.Drawing.Size(500,125)
$objtextbox.Size = New-Object System.Drawing.Size(100,150)
$Form.Controls.Add($objtextbox)
#Date label
$objLabel = New-Object System.Windows.Forms.Label
$objLabel.Location = New-Object System.Drawing.Size(50,125)
$objLabel.Text = "Date:"
$objLabel.AutoSize = $True
$Form.Controls.Add($objLabel)
#Date Textbox
$objtextbox = New-Object System.Windows.Forms.TextBox
$objtextbox.Location = New-Object System.Drawing.Size(100,125)
$objtextbox.Size = New-Object System.Drawing.Size(100,150)
$Form.Controls.Add($objtextbox)
#Add Button docs
$Buttondocs = New-Object System.Windows.Forms.Button
$Buttondocs.Location = New-Object System.Drawing.Size(175,35)
$Buttondocs.Size = New-Object System.Drawing.Size(120,23)
$Buttondocs.Text = "Documentation"
$Form.Controls.Add($Buttondocs)
#Add Button docs event
$Buttondocs.Add_Click({Button_Click})
Function Button_Click()
{
Invoke-Item "C:\Users\Michael\Documents\PowershellProjects\EIMI\*.txt"
}
#Add Button html
$Buttonhtml = New-Object System.Windows.Forms.Button
$Buttonhtml.Location = New-Object System.Drawing.Size(35,35)
$Buttonhtml.Size = New-Object System.Drawing.Size(120,23)
$Buttonhtml.Text = "Submit Form"
$Form.Controls.Add($Buttonhtml)
#Add Button html event
$Buttonhtml.Add_Click({Button_Click})
Function Button_Click()
{
ConvertTo-Html | Out-File "C:\Users\Michael\Documents\PowershellProjects\EIMI\index.html"
}
#Add Question 1
$Question1 = New-Object System.Windows.Forms.Label
$Question1.Location = New-Object System.Drawing.Size(50,175)
$Question1.Size = New-Object System.Drawing.Size(500,50)
$Question1.Text = "Random Form Question."
$Form.Controls.Add($Question1)
$Form.ShowDialog()
The documentation invoke-item section is another problem altogether but I beleive I have found a solution to that one. Unless someone has a simple method. Apologise if this is just trivial nonsense but I'm learning from playing with things. Any suggestions on good authors would be greatly appreciated, or even tips on how to improve this in general.
Thanks Mike.