我正在编写一个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()
答案 0 :(得分:0)
您可以修改文本框控件的创建,以将它们存储在类似的数组中
$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
}
})
希望能给你提供想法和帮助