Powershell对话框用于选择ping计算机列表

时间:2012-05-16 21:32:34

标签: powershell ping

我会说这一段时间了,但是我再次写了几个月的脚本,我偶然发现了technet上的对话框脚本(本周提示),我希望改变它,以便我可以从框中选择一个计算机列表单击确定,它将ping该列表中的计算机。这是代码的副本。

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

$objForm = New-Object System.Windows.Forms.Form 
$objForm.Text = "Select List"
$objForm.Size = New-Object System.Drawing.Size(300,200) 
$objForm.StartPosition = "CenterScreen"

$objForm.KeyPreview = $True
$objForm.Add_KeyDown({if ($_.KeyCode -eq "Enter") 
{$x=$objListBox.SelectedItem;$objForm.Close()}})
$objForm.Add_KeyDown({if ($_.KeyCode -eq "Escape") 
{$objForm.Close()}})

$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Size(75,120)
$OKButton.Size = New-Object System.Drawing.Size(75,23)
$OKButton.Text = "OK"
$OKButton.Add_Click({$x=$objListBox.SelectedItem;$objForm.Close()})
$objForm.Controls.Add($OKButton)

$CancelButton = New-Object System.Windows.Forms.Button
$CancelButton.Location = New-Object System.Drawing.Size(150,120)
$CancelButton.Size = New-Object System.Drawing.Size(75,23)
$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 select a List:"
$objForm.Controls.Add($objLabel) 

$objListBox = New-Object System.Windows.Forms.ListBox 
$objListBox.Location = New-Object System.Drawing.Size(10,40) 
$objListBox.Size = New-Object System.Drawing.Size(260,20) 
$objListBox.Height = 80

[void] $objListBox.Items.Add("AXIS BLY")
[void] $objListBox.Items.Add("AXIS LYN")
[void] $objListBox.Items.Add("GS 5 List")
[void] $objListBox.Items.Add("GS 6 List")
[void] $objListBox.Items.Add("Computer List")


$objForm.Controls.Add($objListBox) 

$objForm.Topmost = $True

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

$x

1 个答案:

答案 0 :(得分:0)

switch($x) {
"AXIS BLY"  {Write-Output "do AXIS BLY List" }
"AXIS LYN"  {Write-Output "do AXIS Lyn List" }
"GS 5 List" {Write-Output "do GS 5 List"     }
"GS 6 List" {Write-Output "do GS 6 List" }
"Computer List" {Write-Output "do Computer List" }


    default { }
}

将Write-Output“...”替换为您想要处理的命令。