Hi Folks;
This is the first time I have used Windows Forms in Powershell. I picked up some boilerplate code at TechNet, and modified it as below, which finally worked:
# Get Name of Remote Computer
$Computer="" # Initialize Computer Name to nothing
# Build Dialog Box, and Get Answer
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[void] [System.REflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
# Build form, and Center it.
$objForm = new-object System.Windows.Forms.Form
$objForm.text="Remote System Name - Single Computer Java Version"
$objForm.size = new-object System.Drawing.Size (400,200)
$objForm.StartPosition = "CenterScreen"
# Trap Commonly used Key Strokes
$objForm.KeyPreview=$true
$objForm.Add_KeyDown({ if ($_.KeyCode -eq "Enter") {$objForm.Close() }})
$objForm.Add_KeyDown({ if ($_.KeyCode -eq "Escape") {$objForm.Close()}})
# Add an Instruction Label for the Text Dialog Box
$objLabel = New-Object System.Windows.Forms.Label
$objLabel.Location = New-Object System.Drawing.Size(60,20)
$objLabel.Size = New-Object System.Drawing.Size(300,50)
$objLabel.Text = "Please enter the name of the Remote Computer in the space Below."
$objForm.Controls.Add($objLabel)
# Add the Text Input Dialog Box
$objTextBox = New-Object System.Windows.Forms.TextBox
$objTextBox.Location = New-Object System.Drawing.size(50,70)
$objTextBox.Size = New-Object System.Drawing.Size(300,30)
$objForm.Controls.Add($objTextBox)
# Trap Commonly used Key Strokes
$objForm.KeyPreview=$true
$objForm.Add_KeyDown({ if ($_.KeyCode -eq "Enter") {$objForm.Close() }})
$objForm.Add_KeyDown({ if ($_.KeyCode -eq "Escape") {$objForm.Close()}})
# Put an "OK" Button in the Lower Center of the Dialog Box
$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Size(150,120)
$OKButton.Size = New-Object System.Drawing.Size(95,23)
$OKButton.text = "OK"
$OKButton.Add_Click({$objForm.Close()})
$objForm.Controls.Add($OKButton)
# Set the form to the Top-Most Layer, and Show it
$objForm.Topmost = $True
$objForm.Add_shown({$objForm.Activate()})
[void] $objForm.ShowDialog()
Set-variable -name "Computer" -value $objTextBox.text
write-host $Computer
Now in the original "boilerplate" code that I picked off of TechNet, there were slight differences. The following lines
$OKButton.Add_Click({$objForm.Close()})
$objForm.Add_KeyDown({ if ($_.KeyCode -eq "Enter") {$objForm.Close() }})
Looked something like:
$OKButton.Add_Click({$Computer=$objTextBox.Text;$objForm.Close()})
$objForm.Add_KeyDown({ if ($_.KeyCode -eq "Enter") {$Computer=$objTextBox.Text;$objForm.Close() }})
Now this did not work at all. I got a clue by substituting write-host $objTextBox.Text where $Computer=.... was, and then came down to the form that worked.
The simple assignment did not work and the set-variable did. That is messed up, and I do not understand why the boilerplate code from TechNet did not function. Thoughts? (should it have been something like:
$Computer=$objTexBox.text.value ???)
Thanks
SWGeek
- Edited by southwestgeek 9 hours 33 minutes ago