Monkey Monkey - 3 months ago 14
PowerShell Question

Powershell textbox only reading last element

I am writing a powershell program at work where I am supposed to take in a computername and resolves it into its hostname and IP address. I am using a popup window and textboxes to get the information from the user. The problem I am running into is that my array is only taking information from the bottom most textbox.

I know I can do this if I manually create x amount textboxes, but I was wondering if it would be possible to do it with a for loop? This way I could later add in functionality for the user to enter in more computernames to be resolved.

function CheckHostName {
param(
[Parameter(Mandatory = $true)]
[String[]] $hostList
)
foreach ($hostName in $hostList) {
try {
[System.Net.Dns]::Resolve($hostName) | ft -Property HostName,AddressList -AutoSize | Out-String
} catch {
"$hostName does not exist" | Out-String
}
}
}

# Test data, for some reason this data is never overridden, even if you change the values in the textbox.
$userInput = @()
$textBoxNumber = 3 # Possible use for a more boxes button in the future
$height = $textBoxNumber * 25

[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[void] [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.VisualBasic")

# Create the form, set its title, size, and screen position.
$objForm = New-Object System.Windows.Forms.Form
$objForm.Text = "Data Entry Form"
$objForm.Size = New-Object System.Drawing.Size(300,(125 + $height))
$objForm.StartPosition = "CenterScreen"

# Allows the user to click the ok button by using enter instead of click. Also close the window
$objForm.KeyPreview = $True
$objForm.Add_KeyDown({if ($_.KeyCode -eq "Enter") {
$objtextbox | % {$userInput += $_.text; Write-Host $_.text}
[System.Windows.Forms.MessageBox]::Show((CheckHostName -hostList $userInput) ,"IP Addresses")}})
$objForm.Add_KeyDown({if ($_.KeyCode -eq "Escape")
{$objForm.Close()}})

# Create the ok button, then set its location, size, text display and function
# Clicking the ok button displays a message box and calls the CheckHostName function.
$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Size(75,(50 + $height))
$OKButton.Size = New-Object System.Drawing.Size(75,25)
$OKButton.Text = "Resolve"
$OKButton.Add_Click({
foreach($line in $objtextbox.text) {$userInput += $line; Write-Host $line}
[System.Windows.Forms.MessageBox]::Show((CheckHostName -hostList $userInput), "IP Addresses")})
$objForm.Controls.Add($OKButton)

# Create the cancel button, then set its location, size, text display and function.
$CancelButton = New-Object System.Windows.Forms.Button
$CancelButton.Location = New-Object System.Drawing.Size(150,(50 + $height))
$CancelButton.Size = New-Object System.Drawing.Size(75,25)
$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 enter the information in the space below:"
$objForm.Controls.Add($objLabel)

# Use a loop to create the textboxes, and make sure they all end up in their own unique location.
1..$textBoxNumber | % {
$objTextBox = New-Object System.Windows.Forms.TextBox
$objTextBox.Location = New-Object System.Drawing.Size(10,(20 + $_ * 25))
$objTextBox.Size = New-Object System.Drawing.Size(260,20)
$objTextBox.Name = "TextBox$_" # Textbox's Name
$objTextBox.Text = "wsfsfc.net$_"
$objForm.Controls.Add($objTextBox) # Create the textbox itself
}

$objForm.Topmost = $True

$objForm.Add_Shown({$objForm.Activate()})
[void] $objForm.ShowDialog()

Answer

You could modify the creation of the text box controls to store them in an array like so

$objTextBoxes = 1..$textBoxNumber | % {
    $objTextBox = New-Object System.Windows.Forms.TextBox
    $objTextBox.Location = New-Object System.Drawing.Size(10,(20 + $_ * 25))
    $objTextBox.Size = New-Object System.Drawing.Size(260,20)
    $objTextBox.Name = "TextBox$_" # Textbox's Name
    $objTextBox.Text = "wsfsfc.net$_"
    $objForm.Controls.Add($objTextBox) # Create the textbox itself
    $objTextBox
}

So that you then can loop through them with foreach in your click event handler, e.g.

$OKButton.Add_Click({
    foreach($txtbox in $objTextBoxes) {
       # Do your stuff
    }
})

Hope that gives you the idea and helps