Do textbox while variable is empty in powershell - 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
}

Related

Im having promlen with button functions in gui powershell

I am struggling here. i cannot figure out how to make the button functions read the $listbox1.selectedItems and $listbox2.selectedItem
whats wrong here?
if i try to look at the $listbox items after the OK button been pressed it will show me but not if i call the function button, ive deleted some unneccesery code parts.
$form = New-Object System.Windows.Forms.Form
$form.StartPosition = 'CenterScreen'
$button1 = New-Object System.Windows.Forms.Button
$button1.Text = 'Link'
$button2 = New-Object System.Windows.Forms.Button
$button2.Text = 'UnLink'
$button3 = New-Object System.Windows.Forms.Button
$button3.Text = 'ShowGPOlink'
$button4 = New-Object System.Windows.Forms.Button
$button4.Text = 'ShowOUlink'
#OK Button
$button5 = New-Object System.Windows.Forms.Button
$button5.Text = 'Done'
$button5.DialogResult = [System.Windows.Forms.DialogResult]::OK
$listBox1 = New-Object System.Windows.Forms.ListBox
$listbox1.SelectionMode = 'MultiExtended'
$listBox2 = New-Object System.Windows.Forms.ListBox
[void] $listBox1.Items.addRange($GPOLIST)
[void] $listBox2.Items.Addrange($OUHOLDER.CanonicalName)
$form.Controls.Add(...)
$form.AcceptButton = $button5
$button1.Add_Click({ LinkFn })
$button2.Add_Click({ UnLinkFn })
$button3.Add_Click({ ShowGPO })
$button4.Add_Click({ ShowOU })
function LinkFn {
#for some reason it returns nothing
$listBox1.selecteditems
$listbox2.SelectedItem
Write-Host "function link"
}
$result = $form.ShowDialog()
if ($result -eq [System.Windows.Forms.DialogResult]::OK){
$listBox1.selecteditems
$listbox2.SelectedItem }
Why are you not leveraging the provided PowerShell docs examples and tweaking as needed:
Selecting Items from a List Box
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
$form = New-Object System.Windows.Forms.Form
$form.Text = 'Select a Computer'
$form.Size = New-Object System.Drawing.Size(300,200)
$form.StartPosition = 'CenterScreen'
$okButton = New-Object System.Windows.Forms.Button
$okButton.Location = New-Object System.Drawing.Point(75,120)
$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,120)
$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 = 'Please select a computer:'
$form.Controls.Add($label)
$listBox = New-Object System.Windows.Forms.ListBox
$listBox.Location = New-Object System.Drawing.Point(10,40)
$listBox.Size = New-Object System.Drawing.Size(260,20)
$listBox.Height = 80
[void] $listBox.Items.Add('atl-dc-001')
[void] $listBox.Items.Add('atl-dc-002')
[void] $listBox.Items.Add('atl-dc-003')
[void] $listBox.Items.Add('atl-dc-004')
[void] $listBox.Items.Add('atl-dc-005')
[void] $listBox.Items.Add('atl-dc-006')
[void] $listBox.Items.Add('atl-dc-007')
$form.Controls.Add($listBox)
$form.Topmost = $true
$result = $form.ShowDialog()
if ($result -eq [System.Windows.Forms.DialogResult]::OK)
{
$x = $listBox.SelectedItem
$x
}
If you have the Multiselect defined, then change the block to this...
$listBox.SelectionMode = 'MultiExtended'
if ($result -eq [System.Windows.Forms.DialogResult]::OK)
{
ForEach ($ListItem in $listBox.SelectedItems)
{$ListItem}
}
So, as I said, this required me to physically re-write what you have here to explain and show you what you would be looking at. This will show the formatted GUI, with a prepopulated list, that when you single or multi-select and item from listbox1, and click the Link button, which fires the function LinkFn, it will copy that selection to Listbox2 and will not close the form.
The below code is not doing anything differently than what is shown in the PS help other than sending the results to another GUI element, and not closing the form via the default OK return. All of which is a common thing in GUI design.
#region Begin environment initialization
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()
#endregion Begin environment initialization
#region Begin functions and code behind
function DoneFn { }
function ShowModuleFn { }
function ShowProcessFn { }
function UnLinkFn { }
function LinkFn
{
$listBox1.selecteditems
# $listbox2.SelectedItems
[void] $listBox2.Items.Addrange($listBox1.selecteditems)
}
$List1 = (Get-Process).Name
# $List2 = (Get-Module).Name
#endregion End functions and code behind
#region Begin GUI code
$Form = New-Object system.Windows.Forms.Form
$Form.ClientSize = '634,339'
$Form.text = 'Form'
$Form.TopMost = $false
$ListBox1 = New-Object system.Windows.Forms.ListBox
$ListBox1.text = 'listBox1'
$listBox1.SelectionMode = 'MultiExtended'
$ListBox1.width = 269
$ListBox1.height = 176
$ListBox1.location = New-Object System.Drawing.Point(17,21)
$ListBox2 = New-Object system.Windows.Forms.ListBox
$ListBox2.text = 'listBox2'
$listBox2.SelectionMode = 'MultiExtended'
$ListBox2.width = 300
$ListBox2.height = 175
$ListBox2.location = New-Object System.Drawing.Point(318,21)
$Button1 = New-Object system.Windows.Forms.Button
$Button1.text = 'Link'
$Button1.width = 60
$Button1.height = 30
$Button1.location = New-Object System.Drawing.Point(20,238)
$Button1.Font = 'Microsoft Sans Serif,10'
$Button2 = New-Object system.Windows.Forms.Button
$Button2.text = 'Unlink'
$Button2.width = 60
$Button2.height = 30
$Button2.location = New-Object System.Drawing.Point(127,241)
$Button2.Font = 'Microsoft Sans Serif,10'
$Button3 = New-Object system.Windows.Forms.Button
$Button3.text = 'ShowProcess'
$Button3.width = 88
$Button3.height = 30
$Button3.location = New-Object System.Drawing.Point(20,297)
$Button3.Font = 'Microsoft Sans Serif,10'
$Button4 = New-Object system.Windows.Forms.Button
$Button4.text = 'ShowModules'
$Button4.width = 105
$Button4.height = 30
$Button4.location = New-Object System.Drawing.Point(127,297)
$Button4.Font = 'Microsoft Sans Serif,10'
$Button5 = New-Object system.Windows.Forms.Button
$Button5.text = 'Done'
$Button5.width = 60
$Button5.height = 30
$Button5.location = New-Object System.Drawing.Point(556,298)
$Button5.Font = 'Microsoft Sans Serif,10'
$Form.controls.AddRange(#(
$ListBox1,
$ListBox2,
$Button1,
$Button2,
$Button3,
$Button4,
$Button5
))
$Button1.Add_Click({ LinkFn })
$Button2.Add_Click({ UnLinkFn })
$Button3.Add_Click({ ShowProcessFn })
$Button4.Add_Click({ ShowModuleFn })
$Button5.Add_Click({ DondFn })
[void] $listBox1.Items.addRange($List1)
# [void] $listBox2.Items.Addrange($List2)
#endregion End GUI code
# Call the GUI
[void]$Form.ShowDialog()

List Every Network Printer With ListBox Powershell

I'm new to Powershell and I would like to write a script to make it easier for end users to add a network printer to their system.
I want to list all the network printer in the listbox, but instead of listing all printer names, I get this:
Here's my code
#window
$window = New-Object System.Windows.Forms.Form
$window.Text = 'Select a Printer'
$window.Size = New-Object System.Drawing.Size(500, 400)
$window.StartPosition = 'CenterScreen'
#Button
$okButton = New-Object System.Windows.Forms.Button
$okButton.Location = New-Object System.Drawing.Point(340,130)
$okButton.Size = New-Object System.Drawing.Size(75,23)
$okButton.Text = 'OK'
$okButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
$window.AcceptButton = $okButton
$cancelButton = New-Object System.Windows.Forms.Button
$cancelButton.Location = New-Object System.Drawing.Point(340,240)
$cancelButton.Size = New-Object System.Drawing.Size(75,23)
$cancelButton.Text = 'Cancel'
$cancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
$window.CancelButton = $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 = 'Please select a printer'
$listBox = New-Object System.Windows.Forms.ListBox
$listBox.Location = New-Object System.Drawing.Point(10,60)
$listBox.Size = New-Object System.Drawing.Size(260,20)
$listBox.Height= 280
$listBox.Items.Add((Get-Printer -ComputerName srvpr01| select $_.Name))
$window.Controls.Add($listBox)
$window.controls.Add($label)
$window.Controls.Add($cancelButton)
$window.Controls.Add($okButton)
$result = $window.ShowDialog()
if ($result -eq [System.Windows.Forms.DialogResult]::OK)
{
$x = $listBox.SelectedItem
$x
}
You know any way I can list the printer names instead of this Object?
I appreciate every type of help and feedback!
You need to use a loop to add the items to the listbox.
Change this line
$listBox.Items.Add((Get-Printer -ComputerName srvpr01| select $_.Name))
to this:
Get-Printer -ComputerName srvpr01 | ForEach-Object { $listBox.Items.Add($_.Name) }

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

POWERSHELL ISE Bulk/Batch select for encoding conversion (Get-Content & Set-Content)

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)

Piping information from text box

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