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.
Related
I have a PS script written to apply some basic configuration to a device over a COM connection. After some user input is recorded, it is used to format a minified java string to send to the device over USB. My issue is that powershell is applying syntax rules to this minified javascript string when I don't want it. I can sucefully send command to the device if they don't contain the javascript, stuff like device Restart etc work fine.
I'm unsure how to work around this, does anyone have a simple solution?
Here's the errors when I try to run it:
At line:146 char:22
+ $port.Write("3,{"bankName":"$bankName","bankId":"0","footswitches ...
+ ~
Missing ')' in method call.
At line:146 char:22
+ ... .Write("3,{"bankName":"$bankName","bankId":"0","footswitches":[{"name ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Unexpected token 'bankName":"$bankName","bankId":"0","footswitches":[{"name":"$FS1
"},{"name":"$FS2"},{"name":"$FS3"},{"name":"$FS4"},{"name":"$FS5"},{"name":"$FS6"}
]}~"' in expression or statement.
At line:126 char:1
+ {
+ ~
Missing closing '}' in statement block or type definition.
At line:146 char:172
+ ... ,{"name":"$FS3"},{"name":"$FS4"},{"name":"$FS5"},{"name":"$FS6"}]}~")
+ ~
Unexpected token ')' in expression or statement.
At line:149 char:1
+ }
+ ~
Unexpected token '}' in expression or statement.
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordExcep
tion
+ FullyQualifiedErrorId : MissingEndParenthesisInMethodCall
Here's the full script:
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
$form = New-Object System.Windows.Forms.Form
$form.Text = 'Bridge Namer'
$form.Size = New-Object System.Drawing.Size(300,400)
$form.StartPosition = 'CenterScreen'
$okButton = New-Object System.Windows.Forms.Button
$okButton.Location = New-Object System.Drawing.Point(75,320)
$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)
$cancelButton = New-Object System.Windows.Forms.Button
$cancelButton.Location = New-Object System.Drawing.Point(150,320)
$cancelButton.Size = New-Object System.Drawing.Size(75,23)
$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,20)
$label.Size = New-Object System.Drawing.Size(280,20)
$label.Text = 'What Bank Number do you want to Re-Name?'
$form.Controls.Add($label)
$textBoxbankNumber = New-Object System.Windows.Forms.TextBox
$textBoxbankNumber.Location = New-Object System.Drawing.Point(10,40)
$textBoxbankNumber.Size = New-Object System.Drawing.Size(260,20)
$form.Controls.Add($textBoxbankNumber)
$label = New-Object System.Windows.Forms.Label
$label.Location = New-Object System.Drawing.Point(10,70)
$label.Size = New-Object System.Drawing.Size(280,20)
$label.Text = 'New Bank Name:'
$form.Controls.Add($label)
$textBoxbankName = New-Object System.Windows.Forms.TextBox
$textBoxbankName.Location = New-Object System.Drawing.Point(10,90)
$textBoxbankName.Size = New-Object System.Drawing.Size(260,20)
$form.Controls.Add($textBoxbankName)
$label = New-Object System.Windows.Forms.Label
$label.Location = New-Object System.Drawing.Point(10,120)
$label.Size = New-Object System.Drawing.Size(120,20)
$label.Text = 'Footswitch 1 Name:'
$form.Controls.Add($label)
$textBoxFS1 = New-Object System.Windows.Forms.TextBox
$textBoxFS1.Location = New-Object System.Drawing.Point(10,140)
$textBoxFS1.Size = New-Object System.Drawing.Size(120,20)
$form.Controls.Add($textBoxFS1)
$label = New-Object System.Windows.Forms.Label
$label.Location = New-Object System.Drawing.Point(150,120)
$label.Size = New-Object System.Drawing.Size(120,20)
$label.Text = 'Footswitch 2 Name:'
$form.Controls.Add($label)
$textBoxFS2 = New-Object System.Windows.Forms.TextBox
$textBoxFS2.Location = New-Object System.Drawing.Point(150,140)
$textBoxFS2.Size = New-Object System.Drawing.Size(120,20)
$form.Controls.Add($textBoxFS2)
$label = New-Object System.Windows.Forms.Label
$label.Location = New-Object System.Drawing.Point(10,170)
$label.Size = New-Object System.Drawing.Size(120,20)
$label.Text = 'Footswitch 3 Name:'
$form.Controls.Add($label)
$textBoxFS3 = New-Object System.Windows.Forms.TextBox
$textBoxFS3.Location = New-Object System.Drawing.Point(10,190)
$textBoxFS3.Size = New-Object System.Drawing.Size(120,20)
$form.Controls.Add($textBoxFS3)
$label = New-Object System.Windows.Forms.Label
$label.Location = New-Object System.Drawing.Point(150,170)
$label.Size = New-Object System.Drawing.Size(120,20)
$label.Text = 'Footswitch 4 Name:'
$form.Controls.Add($label)
$textBoxFS4 = New-Object System.Windows.Forms.TextBox
$textBoxFS4.Location = New-Object System.Drawing.Point(150,190)
$textBoxFS4.Size = New-Object System.Drawing.Size(120,20)
$form.Controls.Add($textBoxFS4)
$label = New-Object System.Windows.Forms.Label
$label.Location = New-Object System.Drawing.Point(10,220)
$label.Size = New-Object System.Drawing.Size(120,20)
$label.Text = 'Footswitch 5 Name:'
$form.Controls.Add($label)
$textBoxFS5 = New-Object System.Windows.Forms.TextBox
$textBoxFS5.Location = New-Object System.Drawing.Point(10,240)
$textBoxFS5.Size = New-Object System.Drawing.Size(120,20)
$form.Controls.Add($textBoxFS5)
$label = New-Object System.Windows.Forms.Label
$label.Location = New-Object System.Drawing.Point(150,220)
$label.Size = New-Object System.Drawing.Size(120,20)
$label.Text = 'Footswitch 6 Name:'
$form.Controls.Add($label)
$textBoxFS6 = New-Object System.Windows.Forms.TextBox
$textBoxFS6.Location = New-Object System.Drawing.Point(150,240)
$textBoxFS6.Size = New-Object System.Drawing.Size(120,20)
$form.Controls.Add($textBoxFS6)
$form.Topmost = $true
$form.Add_Shown({$textBoxbankNumber.Select()})
$form.Add_Shown({$textBoxbankName.Select()})
$form.Add_Shown({$textBoxFS1.Select()})
$form.Add_Shown({$textBoxFS2.Select()})
$form.Add_Shown({$textBoxFS3.Select()})
$form.Add_Shown({$textBoxFS4.Select()})
$form.Add_Shown({$textBoxFS5.Select()})
$form.Add_Shown({$textBoxFS6.Select()})
$result = $form.ShowDialog()
if ($result -eq [System.Windows.Forms.DialogResult]::OK)
{
$bankNumber = $textBoxbankNumber.Text
$bankName = $textBoxbankName.Text
$FS1 = $textBoxFS1.Text
$FS2 = $textBoxFS2.Text
$FS3 = $textBoxFS3.Text
$FS4 = $textBoxFS4.Text
$FS5 = $textBoxFS5.Text
$FS6 = $textBoxFS6.Text
$port= new-Object System.IO.Ports.SerialPort COM6,9600,None,8,one
$port.DtrEnable = "true"
$port.open()
Start-Sleep -Milliseconds 50
$port.Write("0,RSET~")
Start-Sleep -Milliseconds 50
$port.Write("1,DTXR~")
Start-Sleep -Milliseconds 50
$port.Write("2,bankSettings,$bankNumber~")
Start-Sleep -Milliseconds 50
$port.Write("3,{"bankName":"$bankName","bankId":"0","footswitches":[{"name":"$FS1"},{"name":"$FS2"},{"name":"$FS3"},{"name":"$FS4"},{"name":"$FS5"},{"name":"$FS6"}]}~")
Start-Sleep -Milliseconds 50
$port.close()
}
Well without a suggested serial port string a correct answer is hard to construct. I just double quoted your quotes in my example (That will fix the PS part). Since your implied output statement is valid java string, i somehow guessed your desired output.
$FS1 = "a"
$FS2 = "b"
$FS3 = "c"
$FS4 = "d"
$FS5 = "e"
$FS6 = "f"
$bankName = "bar"
$port.Write("3,{""bankName"":""$bankName"",""bankId"":""0"",""footswitches"":[{""name"":""$FS1""},{""name"":""$FS2""},{""name"":""$FS3""},{""name"":""$FS4""},{""name"":""$FS5""},{""name"":""$FS6""}]}~")
OutPut
3,{"bankName":"bar","bankId":"0","footswitches":[{"name":"a"},{"name":"b"},{"name":"c"},{"name":"d"},{"name":"e"},{"name":"f"}]}~
Sorry, I thought I'd already answered this. I got around this problem by masking the quotations in this way:
$port.Write("3,{`"bankName`":`"$bankName`",`"bankId`":`"0`",`"footswitches`":[{`"name`":`"$FS1`"},{`"name`":`"$FS2`"},{`"name`":`"$FS3`"},{`"name`":`"$FS4`"},{`"name`":`"$FS5`"},{`"name`":`"$FS6`"}]}~")
I wrote six small gui scripts that I'd like to place in a single file, for the sake of converting said file into rxecutable with ps2exe.
I've found a script ,here on stack that is perfect for what I want. Unfortunatelly I cann't find any info on script placement within tabs and MS documentation leads me to ISE tabs, which is not helpfull.
Say I'd like to place this
Add-Type -AssemblyName PresentationFramework
$button_click = {
$folder = $textBox1.Text;
$pytanie = [System.Windows.MessageBox]::Show('Czy chcesz usunac folder?', '', '4');
If($pytanie -eq 'Yes')
{Remove-Item –path $folder –recurse -Force};
$test = Test-Path $folder;
if ($test -eq $false){[System.Windows.MessageBox]::Show('Folder Usuniety', '', '0')}}
$label2 = New-Object System.Windows.Forms.Label
$label2.AutoSize = $True
$label2.Text = ("Scieżka")
$label2.Location = New-Object System.Drawing.Point (10,30)
$label2.Size = New-Object System.Drawing.Size (25,70)
$label2.Font = [System.Drawing.Font]::new("Arial", 10, [System.Drawing.FontStyle]::Bold)
$textBox1 = New-Object System.Windows.Forms.TextBox
$textBox1.Location = New-Object System.Drawing.Point(10,70) ### Location of the text box
$textBox1.Size = New-Object System.Drawing.Size(200,50) ### Size of the text box
$textBox1.Multiline = $false ### Allows multiple lines of data
$textBox1.Font = New-Object System.Drawing.Font("Consolas",10,[System.Drawing.FontStyle]::Regular)
$textBox1.ReadOnly=$false
$button = New-Object System.Windows.Forms.Button
$button.Location = New-Object System.Drawing.Point(10,120)
$button.Size = New-Object System.Drawing.Size (200,30)
$button.Text = "Usun Folder"
$button.Add_Click($button_click)
$form = New-Object System.Windows.Forms.Form
$form.Text = 'Mapowanie' ### Text to be displayed in the title
$form.Size = New-Object System.Drawing.Size(240,200) ### Size of the window
$form.StartPosition = 'Manual'
$form.Location = '10,10'
$form.Topmost = $true ### Optional - Opens on top of other windows
$form.Controls.AddRange(#($textBox1,$button, $label2))
$form.ShowDialog()
within a tab. How to do it?
I think that it the
$Tab1.Controls.Add($button)
You are looking for.
For example
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
$ApplicationForm = New-Object System.Windows.Forms.Form
$ApplicationForm.StartPosition = "CenterScreen"
$ApplicationForm.Topmost = $false
$ApplicationForm.Size = "800,600"
$FormTabControl = New-object System.Windows.Forms.TabControl
$FormTabControl.Size = "755,475"
$FormTabControl.Location = "25,75"
$ApplicationForm.Controls.Add($FormTabControl)
$Tab1 = New-object System.Windows.Forms.Tabpage
$Tab1.DataBindings.DefaultDataSourceUpdateMode = 0
$Tab1.UseVisualStyleBackColor = $True
$Tab1.Name = "Tab1"
$Tab1.Text = "Tab1”
$FormTabControl.Controls.Add($Tab1)
$textBox1 = New-Object System.Windows.Forms.TextBox
$textBox1.Location = New-Object System.Drawing.Point(10,70) ### Location of the text box
$textBox1.Size = New-Object System.Drawing.Size(200,50) ### Size of the text box
$textBox1.Multiline = $false ### Allows multiple lines of data
$textBox1.Font = New-Object System.Drawing.Font("Consolas",10,[System.Drawing.FontStyle]::Regular)
$textBox1.ReadOnly=$false
$Tab1.Controls.Add($textBox1)
$Tab2 = New-object System.Windows.Forms.Tabpage
$Tab2.DataBindings.DefaultDataSourceUpdateMode = 0
$Tab2.UseVisualStyleBackColor = $True
$Tab2.Name = "Tab2"
$Tab2.Text = "Tab2”
$FormTabControl.Controls.Add($Tab2)
$button = New-Object System.Windows.Forms.Button
$button.Location = New-Object System.Drawing.Point(10,120)
$button.Size = New-Object System.Drawing.Size (200,30)
$button.Text = "Usun Folder"
$button.Add_Click($button_click)
$Tab2.Controls.Add($button)
# Initlize the form
$ApplicationForm.Add_Shown({$ApplicationForm.Activate()})
[void] $ApplicationForm.ShowDialog()
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.
I am writing a script to get the data from user input as date, when I select another date it is not displaying in text box, please suggest where is the issue
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
$Form = New-Object System.Windows.Forms.Form
$Form.Size = New-Object System.Drawing.Size(800,600)
$Form.Text = "test"
$Form.FormBorderStyle = "FixedDialog"
$TextBoxlabel = New-Object System.Windows.Forms.Label
$TextBoxlabel.Location = New-Object System.Drawing.Point(10,50)
$TextBoxlabel.Size = New-Object System.Drawing.Size(100,20)
$TextBoxlabel.Text = 'Input No:'
$form.Controls.Add($TextBoxlabel)
$TextBox = New-Object System.Windows.Forms.TextBox
$TextBox.Location = New-Object System.Drawing.Size(120,50)
$TextBox.Size = New-Object System.Drawing.Size(180,20)
$TextBox.MultiLine = $false
$TextBox.ScrollBars = "Vertical"
$Form.Controls.Add($TextBox)
$StartDatelabel = New-Object System.Windows.Forms.Label
$StartDatelabel.Location = New-Object System.Drawing.Point(10,100)
$StartDatelabel.Size = New-Object System.Drawing.Size(100,20)
$StartDatelabel.Text = 'Start Date:'
$form.Controls.Add($StartDatelabel)
$StartDateTextBox = New-Object System.Windows.Forms.TextBox
$StartDateTextBox.Location = New-Object System.Drawing.Size(120,100)
$StartDateTextBox.Size = New-Object System.Drawing.Size(100,20)
$StartDateTextBox.MultiLine = $false
$StartDateTextBox.ScrollBars = "Vertical"
$Form.Controls.Add($StartDateTextBox)
$Button1 = New-Object System.Windows.Forms.Button
$Button1.Location = New-Object System.Drawing.Size(220,100)
$Button1.Size = New-Object System.Drawing.Size(50,20)
$Button1.Text = "Date1"
$Button1.Add_Click({
$StartDateInput = New-Object System.Windows.Forms.MonthCalendar
$StartDateInput.ShowTodayCircle = $false
$StartDateInput.MaxSelectionCount = 1
$StartDateInput.Location = New-Object System.Drawing.Size(120,120)
$StartDateTextBox.Text = $StartDateInput.SelectionStart
$form.Controls.Add($StartDateInput)
})
$Form.Controls.Add($Button1)
$OutputTextBox = New-Object System.Windows.Forms.TextBox
$OutputTextBox.Location = New-Object System.Drawing.Size(10,320)
$OutputTextBox.Size = New-Object System.Drawing.Size(760,200)
$OutputTextBox.MultiLine = $true
$OutputTextBox.ScrollBars = "Vertical"
$OutputTextBox.ReadOnly = $true
$Form.Controls.Add($OutputTextBox)
$Form.Add_Shown({$Form.Activate()})
[void] $Form.ShowDialog()
when I am selecting the date (other than default date) it is not displaying in text box
POWERSHELL ISE
Hi, I'm trying to write a program that will grab all .txt files in an input path, change the encoding, and send the new .txt files to an output location.
I have it working based on single text boxes. I'm not exactly sure how to set it where it grabs all .txt files instead of one. (maybe an array?) And output with same file name in a new location.
My question is how can I write this to grab all .txt files and output them all to a new location instead of doing a single file at a time?
Here is my current code:
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
$form = New-Object System.Windows.Forms.Form
$form.Text = "Text Converter"
$form.Size = New-Object System.Drawing.Size(300,300)
$form.StartPosition = "CenterScreen"
$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Point(55,230)
$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)
$CancelButton = New-Object System.Windows.Forms.Button
$CancelButton.Location = New-Object System.Drawing.Point(150,230)
$CancelButton.Size = New-Object System.Drawing.Size(75,23)
$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,20)
$label.Size = New-Object System.Drawing.Size(280,20)
$label.Text = "Input Path:"
$form.Controls.Add($label)
$textBox = New-Object System.Windows.Forms.TextBox
$textBox.Location = New-Object System.Drawing.Point(10,40)
$textBox.Size = New-Object System.Drawing.Size(260,20)
$form.Controls.Add($textBox)
$label = New-Object System.Windows.Forms.Label
$label.Location = New-Object System.Drawing.Point(10,65)
$label.Size = New-Object System.Drawing.Size(280,20)
$label.Text = "Input Encoding:"
$form.Controls.Add($label)
$textBox2 = New-Object System.Windows.Forms.TextBox
$textBox2.Location = New-Object System.Drawing.Point(10,85)
$textBox2.Size = New-Object System.Drawing.Size(260,20)
$form.Controls.Add($textBox2)
$label = New-Object System.Windows.Forms.Label
$label.Location = New-Object System.Drawing.Point(10,115)
$label.Size = New-Object System.Drawing.Size(280,20)
$label.Text = "Output Path:"
$form.Controls.Add($label)
$textBox3 = New-Object System.Windows.Forms.TextBox
$textBox3.Location = New-Object System.Drawing.Point(10,140)
$textBox3.Size = New-Object System.Drawing.Size(260,20)
$form.Controls.Add($textBox3)
$label = New-Object System.Windows.Forms.Label
$label.Location = New-Object System.Drawing.Point(10,170)
$label.Size = New-Object System.Drawing.Size(280,20)
$label.Text = "Output Encoding"
$form.Controls.Add($label)
$textBox4 = New-Object System.Windows.Forms.TextBox
$textBox4.Location = New-Object System.Drawing.Point(10,190)
$textBox4.Size = New-Object System.Drawing.Size(260,20)
$form.Controls.Add($textBox4)
$form.Topmost = $True
$form.Add_Shown({$textBox.Select()})
$result = $form.ShowDialog()
if ($result -eq [System.Windows.Forms.DialogResult]::OK)
{
$x = $textBox.Text
$x
$x2 = $textBox2.Text
$x2
$x3 = $textBox3.Text
$x3
$x4 = $textBox4.Text
$x4
}
Get-Content $x -encoding $x2 |
Set-Content $x3 -encoding $x4
If you want the user to select multiple files, you could use an OpenFileDialog control to allow the user to select multiple input files, and then add them to a ListBox rather than a TextBox:
# Use a ListBox, since we're going to keep track of multiple strings
$InputFileBox = New-Object System.Windows.Forms.ListBox
$InputFileBox.Location = New-Object System.Drawing.Point -ArgumentList 10,10
$InputFileBox.Size = New-Object System.Drawing.Size -ArgumentList 265,240
$Form.Controls.Add($InputFileBox)
# Set up the "OpenFile" dialog, set the Multiselect property
$OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog
$OpenFileDialog.InitialDirectory = 'D:\test\forms'
$OpenFileDialog.Multiselect = $true
$OpenFileButton = New-Object System.Windows.Forms.Button
$OpenFileButton.Location = New-Object System.Drawing.Point -ArgumentList 220,265
$OpenFileButton.Size = New-Object System.Drawing.Size -ArgumentList 55,20
$OpenFileButton.Text = 'Browse!'
$OpenFileButton.add_Click({
if($OpenFileDialog.ShowDialog() -eq [System.Windows.Forms.DialogResult]::OK)
{
foreach($FileName in $OpenFileDialog.FileNames)
{
# only add files not already in the list
if(-not $InputFileBox.Items.Contains($FileName))
{
$InputFileBox.Items.Add($FileName)
}
}
}
})
$Form.Controls.Add($OpenFileButton)
You can then grab the files names either directly from the $OpenFileDialog.FileNames or from the ListBox if you want to allow the user to edit the list:
$FileNames = [string[]]$InputFileBox.Items
For the output directory, use a FolderBrowserDialog control:
# TextBox is fine here, but disable it so users can't type directly in it
$OutputFolderTextBox = New-Object System.Windows.Forms.TextBox
$OutputFolderTextBox.Location = New-Object System.Drawing.Point -ArgumentList 10,10
$OutputFolderTextBox.Size = New-Object System.Drawing.Size -ArgumentList 265,20
$OutputFolderTextBox.Enabled = $false
$OutputFolderTextBox.BackColor = [System.Drawing.Color]::White
$Form.Controls.Add($OutputFolderTextBox)
# set up FolderBrowserDialog control
$OutputFolderBrowserDialog = New-Object System.Windows.Forms.FolderBrowserDialog
$OutputFolderBrowserDialog.RootFolder = [System.Environment+SpecialFolder]::MyComputer
$OutputFolderBrowseButton = New-Object System.Windows.Forms.Button
$OutputFolderBrowseButton.Location = New-Object System.Drawing.Point -ArgumentList 220,40
$OutputFolderBrowseButton.Size = New-Object System.Drawing.Size -ArgumentList 55,20
$OutputFolderBrowseButton.Text = 'Browse!'
$OutputFolderBrowseButton.add_Click({
if($OutputFolderBrowserDialog.ShowDialog() -eq [System.Windows.Forms.DialogResult]::OK)
{
# grab the selected folder path
$OutputFolderTextBox.Text = $OutputFolderBrowserDialog.SelectedPath
}
})
$Form.Controls.Add($OutputFolderBrowseButton)