Winforms Ping多个IP/主机名

Winforms Ping多个IP/主机名,winforms,powershell,Winforms,Powershell,我只是想做一个简单的IP/主机名检查器。我正在努力解决的一个问题是让它ping列表中的每个项目。它只显示第一个或最后一个的结果 我尝试过将文本框中的项目添加到数组中,并以这种方式ping它们,结果是一样的 这是我目前的代码 [System.Windows.Forms.Application]::EnableVisualStyles() function ping-list{ $IPStatus.Text = "" $names = @($IPList.text) $Names | ForEa

我只是想做一个简单的IP/主机名检查器。我正在努力解决的一个问题是让它ping列表中的每个项目。它只显示第一个或最后一个的结果

我尝试过将文本框中的项目添加到数组中,并以这种方式ping它们,结果是一样的

这是我目前的代码

[System.Windows.Forms.Application]::EnableVisualStyles()

function ping-list{
$IPStatus.Text = ""

$names = @($IPList.text)
$Names | ForEach-Object{
  if (Test-Connection -ComputerName $_ -Count 1){
   $IPStatus.Text += "$_ is Online"
   Write-Host "$_ is Online"
  }
  else{
    $IPStatus.Text += "$_ is Offline"
    Write-Host "$_ is Offline"
  }
  }
}

$Pinger                          = New-Object system.Windows.Forms.Form
$Pinger.ClientSize               = '657,557'
$Pinger.text                     = "Pinger"
$Pinger.TopMost                  = $false

$IPStatus                        = New-Object system.Windows.Forms.TextBox
$IPStatus.multiline              = $true
$IPStatus.width                  = 234
$IPStatus.height                 = 498
$IPStatus.enabled                = $true
$IPStatus.location               = New-Object System.Drawing.Point(408,45)
$IPStatus.Font                   = 'Microsoft Sans Serif,10'
$IPStatus.Text                   = "Ready"

$IPList                          = New-Object system.Windows.Forms.TextBox
$IPList.multiline                = $true
$IPList.width                    = 234
$IPList.height                   = 498
$IPList.enabled                  = $true
$IPList.location                 = New-Object System.Drawing.Point(15,45)
$IPList.Font                     = 'Microsoft Sans Serif,10'
$IPList.Text                     = @("127.0.0.1")

$PingButton                      = New-Object system.Windows.Forms.Button
$PingButton.text                 = "button"
$PingButton.width                = 60
$PingButton.height               = 30
$PingButton.location             = New-Object System.Drawing.Point(298,240)
$PingButton.Font                 = 'Microsoft Sans Serif,10'
$PingButton.Add_Click({ping-list})

$Pinger.controls.AddRange(@($IPStatus,$IPList,$PingButton))

$Pinger.Add_Shown(            {$Pinger.Activate()})
$Pinger.ShowDialog()


$IPList.text获取文本框中的所有文本

而是使用

function ping-list{
    $IPStatus.Text = ""

    $Names = @($IPList.Lines)
    $Names | ForEach-Object{
        if (Test-Connection -ComputerName $_ -Count 1){
            $IPStatus.Text += "$_ is Online`r`n"
            Write-Host "$_ is Online"
        }
        else{
            $IPStatus.Text += "$_ is Offline`r`n"
            Write-Host "$_ is Offline"
        }
    }
}

我的理解是,每次迭代都需要将数据输入GUI一次。。。而且您只在数据集完成后才输入数据。从未见过.Lines术语!这在将来会很有帮助。非常感谢你。