用于ping计算机的powershell gui表单

用于ping计算机的powershell gui表单,powershell,Powershell,我刚刚制作了一个powershell gui表单,用于ping网络中的计算机。 问题是,“ok”按钮工作不可靠。 有时它起作用,有时它不起作用。 在gui中输入computername后,按下enter按钮始终有效。 脚本有什么问题? 我的代码如下。也许你们有个主意 [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") [void] [System.Reflection

我刚刚制作了一个powershell gui表单,用于ping网络中的计算机。 问题是,“ok”按钮工作不可靠。 有时它起作用,有时它不起作用。 在gui中输入computername后,按下enter按钮始终有效。 脚本有什么问题? 我的代码如下。也许你们有个主意

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

# form specs
$objForm = New-Object System.Windows.Forms.Form
$objForm.Text = "Check Network Status"
$objForm.Size = New-Object System.Drawing.Size(300,170)
$objForm.StartPosition = "CenterScreen"
$objForm.KeyPreview = $True
$objForm.MaximumSize = $objForm.Size
$objForm.MinimumSize = $objForm.Size

# form icon  
$objForm.BackgroundImage = $objImage
$objForm.BackgroundImageLayout = "None"
# None, Tile, Center, Stretch, Zoom

$objForm.Width = $objImage.Width
$objForm.Height = $objImage.Height 

# label
$objLabel = New-Object System.Windows.Forms.label
$objLabel.Location = New-Object System.Drawing.Size(7,10)
$objLabel.Size = New-Object System.Drawing.Size(130,15)
$objLabel.BackColor = "Transparent"
$objLabel.ForeColor = "yellow"
$objLabel.Text = "Enter Computer Name"
$objForm.Controls.Add($objLabel)

# input box
$objTextbox = New-Object System.Windows.Forms.TextBox
$objTextbox.Location = New-Object System.Drawing.Size(10,45)
$objTextbox.Size = New-Object System.Drawing.Size(120,20)
$objForm.Controls.Add($objTextbox)

# ok button
$objButton = New-Object System.Windows.Forms.Button
$objButton.Location = New-Object System.Drawing.Size(140,44)
$objButton.Size = New-Object System.Drawing.Size(75,23)
$objButton.Text = "OK"
$objButton.Add_Click($button_click)
$objForm.Controls.Add($objButton)

# return status
$returnStatus = New-Object System.Windows.Forms.label
$returnStatus.Location = New-Object System.Drawing.Size(8,70)
$returnStatus.Size = New-Object System.Drawing.Size(130,30)
$returnStatus.BackColor = "Transparent"
$returnStatus.Text = ""
$objForm.Controls.Add($returnStatus)

# action item here 
$button_click =
{  
$returnStatus.Text = ""  
$objStatusBar.Text = "Checking status..."
$computerName = $objTextbox.Text

# output - online  
if (Test-Connection $computerName -quiet -Count 2){
Write-Host -ForegroundColor Green "$computerName is online"
$returnStatus.BackColor = "Transparent"
$returnStatus.ForeColor = "lime"
$returnStatus.Text = "Status: Online"
}
Else{
# output - offline
Write-Host -ForegroundColor Red "$computerName is offline"
$returnStatus.ForeColor= "Red"
$returnStatus.Text = "Status: Offline"
}   
$objStatusBar.Text = "Done" 
}  

# form status bar  
$objStatusBar = New-Object System.Windows.Forms.StatusBar
$objStatusBar.Name = "statusBar"
$objStatusBar.Text = "Ready"
$objForm.Controls.Add($objStatusBar)

$objForm.Add_KeyDown({if ($_.KeyCode -eq "Enter"){& $button_click}})
$objForm.Add_KeyDown({if ($_.KeyCode -eq "Escape")
{$objForm.Close()}})

# modal
$objForm.Topmost = $True
$objForm.Add_Shown({$objForm.Activate()})
[void] $objForm.ShowDialog()

定义$button\u在将其分配给按钮之前单击:

# action item here 
$button_click =
{  
$returnStatus.Text = ""  
$objStatusBar.Text = "Checking status..."
$computerName = $objTextbox.Text

# output - online  
if (Test-Connection $computerName -quiet -Count 2){
Write-Host -ForegroundColor Green "$computerName is online"
$returnStatus.BackColor = "Transparent"
$returnStatus.ForeColor = "lime"
$returnStatus.Text = "Status: Online"
}
Else{
# output - offline
Write-Host -ForegroundColor Red "$computerName is offline"
$returnStatus.ForeColor= "Red"
$returnStatus.Text = "Status: Offline"
}   
$objStatusBar.Text = "Done" 
}  
# ok button
$objButton = New-Object System.Windows.Forms.Button
$objButton.Location = New-Object System.Drawing.Size(140,44)
$objButton.Size = New-Object System.Drawing.Size(75,23)
$objButton.Text = "OK"
$objButton.Add_Click($button_click)
$objForm.Controls.Add($objButton)