Powershell文本框只读取最后一个元素



我正在工作中编写一个powershell程序,我应该在其中输入计算机名并将其解析为其主机名和IP地址。我正在使用一个弹出窗口和文本框从用户获取信息。我遇到的问题是,我的数组只从最底部的文本框获取信息。

我知道我可以做到这一点,如果我手动创建x数量的文本框,但我想知道是否有可能做到这一点与for循环?这样以后我就可以添加功能,让用户输入更多要解析的计算机名。

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

您可以修改文本框控件的创建,将它们存储在一个数组中,如下所示

$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
}

这样你就可以在点击事件处理程序中用foreach循环它们,例如

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

希望能给大家一些启发和帮助

最新更新