PowerShell GUI - 将行从 TextBox 1 移动到 TextBox 2

PowerShell GUI - Move line from TextBox 1 to TextBox 2

我正在开发一个脚本,它将 ping 多台计算机并将在线的计算机移动到另一个输入框。下面的脚本可以 ping 多台计算机,但我不确定如何将计算机名称移动到下一个输入框 ($Inputbox_success).

示例:

Computers Successfully Ping
Computer 1
Computer 2
Computer 3

在我点击安装(或 PING)按钮之后。 如果计算机在线并且 ping 成功,它将移动到成功的输入框。

Computers Successfully Ping
Computer 1
Computer 2
Computer 3

谢谢

# Load required assemblies
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")

# Drawing form and controls
$Harvester = New-Object System.Windows.Forms.Form
    $Harvester.Text = "Harvester Installer"
    $Harvester.Size = New-Object System.Drawing.Size(490,415)
    $Harvester.FormBorderStyle = "FixedDialog"
    $Harvester.TopMost = $true
    $Harvester.MaximizeBox = $false
    $Harvester.MinimizeBox = $false
    $Harvester.ControlBox = $true
    $Harvester.StartPosition = "CenterScreen"
    $Harvester.Font = "Segoe UI"

#======================== DROPBOX ========================#
$label_messageCombobox = New-Object System.Windows.Forms.Label
    $label_messageCombobox.Location = New-Object System.Drawing.Size(20,20)
    $label_messageCombobox.Size = New-Object System.Drawing.Size(200,15)
    $label_messageCombobox.Text = "What would you like to do?"
    $Harvester.Controls.Add($label_messageCombobox)    

$DropdownBox = New-Object System.Windows.Forms.ComboBox
    $DropdownBox.Location = New-Object System.Drawing.Size(20,40)
    $DropdownBox.Size = New-Object System.Drawing.Size(300,15)
    $DropdownBox.Height = 200
    $Dropdownbox.DropDownStyle = "DropDownList"
    $Harvester.Controls.Add($DropdownBox)
    
    $Selections = @("Install Harvester","Uninstall Harvester","Check if Harvester is installed")

    foreach($Selection in $Selections){
        $DropdownBox.Items.Add($Selection) | Out-Null
    }

#======================== INPUTBOX - Computers ========================#
$label_message2 = New-Object System.Windows.Forms.Label
    $label_message2.Location = New-Object System.Drawing.Size(20,70)
    $label_message2.Size = New-Object System.Drawing.Size(100,15)
    $label_message2.Text = "Computers"
    $Harvester.Controls.Add($label_message2)    
    
# Inputbox    
    $Inputbox = New-Object System.Windows.Forms.TextBox
    $Inputbox.Multiline = $True;
    $Inputbox.Location = New-Object System.Drawing.Size(20,90)
    $Inputbox.Size = New-Object System.Drawing.Size(200,200)
    $Inputbox.ScrollBars = "Vertical"
    $Harvester.Controls.Add($Inputbox)


#======================== INPUTBOX - Completed ========================#
$label_message_success = New-Object System.Windows.Forms.Label
    $label_message_success.Location = New-Object System.Drawing.Size(250,70)
    $label_message_success.Size = New-Object System.Drawing.Size(100,15)
    $label_message_success.Text = "Successful"
    $Harvester.Controls.Add($label_message_success)    
    
# Inputbox    
    $Inputbox_success = New-Object System.Windows.Forms.TextBox
    $Inputbox_success.Multiline = $True;
    $Inputbox_success.Location = New-Object System.Drawing.Size(250,90)
    $Inputbox_success.Size = New-Object System.Drawing.Size(200,200)
    $Inputbox_success.ScrollBars = "Vertical"
    $Harvester.Controls.Add($Inputbox_success)

#======================== BUTTON ===========================#   
#======================== Installer ========================#   

    $button_Installer = New-Object System.Windows.Forms.Button
    $button_Installer.Location = New-Object System.Drawing.Size(20,300)
    $button_Installer.Size = New-Object System.Drawing.Size(100,32)
    $button_Installer.TextAlign = "MiddleCenter"
    $button_Installer.Text = "Install"
    $button_Installer.Add_Click({
        
        $Inputbox.Text = $Inputbox.Text -replace '(\r\n)(\r\n)+', ''
        $Inputbox.Text = $Inputbox.Text -replace '(?:(\r\n)+$|^(\r\n)+)', ''

          ForEach ($PCname in $Inputbox.lines) {

            $pingPC = Test-Connection -ComputerName $PCName -Quiet -Count 1 -ErrorAction SilentlyContinue
            If ($pingPC -eq $True){
                Add-Type -AssemblyName System.Windows.Forms
                [System.Windows.Forms.MessageBox]::Show("$PCName is Online")}
            #else {
             #   Add-Type -AssemblyName System.Windows.Forms
              #  [System.Windows.Forms.MessageBox]::Show("$PCName is offline")}
            #return
            


        }

       
    })
    $Harvester.Controls.Add($button_Installer)

#======================== Uninstaller ========================#
    $button_UnInstaller = New-Object System.Windows.Forms.Button
    $button_UnInstaller.Location = New-Object System.Drawing.Size(140,300)
    $button_UnInstaller.Size = New-Object System.Drawing.Size(100,32)
    $button_UnInstaller.TextAlign = "MiddleCenter"
    $button_UnInstaller.Text = "Uninstall"
    $button_UnInstaller.Add_Click({
        
        
    })
    $Harvester.Controls.Add($button_UnInstaller)


#======================== Verify ========================#
    $button_Check = New-Object System.Windows.Forms.Button
    $button_Check.Location = New-Object System.Drawing.Size(260,300)
    $button_Check.Size = New-Object System.Drawing.Size(100,32)
    $button_Check.TextAlign = "MiddleCenter"
    $button_Check.Text = "Check"
    $button_Check.Add_Click({
        
        
    })
    $Harvester.Controls.Add($button_Check)

# show form
$Harvester.Add_Shown({$Harvester.Activate()})
[void] $Harvester.ShowDialog()

如评论中所述,尽管这可以用 TextBox, this would be easier to accomplish using a DataGridView 代替。

但是,为了给您提示如何处理这个问题,这里提供了一种方法。

此答案仅涵盖 Check 按钮的事件

首先,我建议您在启动表单时让所有按钮处于 Disabled 状态,并动态 Enable 它们基于ComboBox SelectedItem,下面我将介绍 Check 按钮的启用\禁用,剩下的由您研究:

$DropdownBox.Add_SelectedValueChanged({
    # disable all buttons each time this event is triggered
    $Harvester.Controls.foreach{
        if($_ -is [System.Windows.Forms.Button]) {
            $_.Enabled = $false
        }
    }
    # and enable only one of them based on the selected item of the ComboBox
    switch($this.SelectedItem) {
        'Check if Harvester is installed' { $button_Check.Enabled = $true }
        # Same logic for the other buttons
    }
})
$Harvester.Controls.Add($DropdownBox)

然后,对于 Check 按钮上的 Add_Click 事件:

$checkEvent = {
    $this.Enabled = $false # Disable this button while processing
    [collections.arraylist] $ref = @(($Inputbox.Text -split '\r?\n').Trim() -ne '')
    if(-not $ref) { return } # if the textbox is empty, don't do anything
    $i = 0
    1..$ref.Count | ForEach-Object {
        if(Test-Connection $ref[$i] -Quiet) {
            $Inputbox_success.Text += $ref[$i] + [environment]::NewLine
            $ref.RemoveAt($i)
        }
        else {
            $ref[$i] = $ref[$i] + ' (DID NOT RESPOND)'
            $i++
        }
        $Inputbox.Text = $ref | Out-String
        $Inputbox, $Inputbox_success | ForEach-Object Refresh
    }
    $this.Enabled = $true # Enable it back after checking
}

$button_Check = [System.Windows.Forms.Button]@{
    Location  = [System.Drawing.Size]::new(260, 300)
    Size      = [System.Drawing.Size]::new(100, 32)
    TextAlign = "MiddleCenter"
    Text      = "Check"
    Enabled   = $false
}
$button_Check.Add_Click($checkEvent)
$Harvester.Controls.Add($button_Check)

对于演示,它看起来是这样的:

圣地亚哥,这正是我要找的!!太感谢了! 但我无法在自己的脚本中复制它。我想我错过了什么,但不确定是什么。下拉菜单只是我以前的一个想法,但我不认为我会使用它。我只想要按钮。你能看看我的脚本吗?如果 ping 成功,则不会将其移动到成功输入框。

非常感谢您的帮助。非常感谢!

# Load required assemblies
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")

# Drawing form and controls
$Harvester = New-Object System.Windows.Forms.Form
    $Harvester.Text = "Harvester Installer"
    $Harvester.Size = New-Object System.Drawing.Size(490,300)
    $Harvester.FormBorderStyle = "FixedDialog"
    $Harvester.TopMost = $true
    $Harvester.MaximizeBox = $false
    $Harvester.MinimizeBox = $false
    $Harvester.ControlBox = $true
    $Harvester.StartPosition = "CenterScreen"
    $Harvester.Font = "Segoe UI"


#======================== INPUTBOX - Computers ========================#
$label_message2 = New-Object System.Windows.Forms.Label
    $label_message2.Location = New-Object System.Drawing.Size(20,10)
    $label_message2.Size = New-Object System.Drawing.Size(100,15)
    $label_message2.Text = "Computers"
    $Harvester.Controls.Add($label_message2)    
    
# Inputbox    
    $Inputbox = New-Object System.Windows.Forms.TextBox
    $Inputbox.Multiline = $True;
    $Inputbox.Location = New-Object System.Drawing.Size(20,30)
    $Inputbox.Size = New-Object System.Drawing.Size(200,150)
    $Inputbox.ScrollBars = "Vertical"
    $Harvester.Controls.Add($Inputbox)


#======================== INPUTBOX - Completed ========================#
$label_message_success = New-Object System.Windows.Forms.Label
    $label_message_success.Location = New-Object System.Drawing.Size(250,10)
    $label_message_success.Size = New-Object System.Drawing.Size(100,15)
    $label_message_success.Text = "Successful"
    $Harvester.Controls.Add($label_message_success)    
    
# Inputbox    
    $Inputbox_success = New-Object System.Windows.Forms.TextBox
    $Inputbox_success.Multiline = $True;
    $Inputbox_success.Location = New-Object System.Drawing.Size(250,30)
    $Inputbox_success.Size = New-Object System.Drawing.Size(200,150)
    $Inputbox_success.ScrollBars = "Vertical"
    $Harvester.Controls.Add($Inputbox_success)

#======================== BUTTON ===========================#   
#======================== Installer ========================#   

$pingEvent = {
    [collections.arraylist] $ref = @(($Inputbox.Text -split '\r?\n').Trim() -ne '')
    if(-not $ref) { return } # if the textbox is empty, don't do anything
    $i = 0
    1..$ref.Count | ForEach-Object {
        if(Test-Connection $ref[$i] -Quiet) {
            $Inputbox_success.Text += $ref[$i] + [environment]::NewLine
            $ref.RemoveAt($i)
        }
        $Inputbox, $Inputbox_success | ForEach-Object Refresh
    }
}

$button_ping = [System.Windows.Forms.Button]@{
    Location  = [System.Drawing.Size]::new(180, 200)
    Size      = [System.Drawing.Size]::new(100, 32)
    TextAlign = "MiddleCenter"
    Text      = "Ping"
}
    $button_ping.Add_Click($pingEvent)
    $Harvester.Controls.Add($button_ping)


# show form
$Harvester.Add_Shown({$Harvester.Activate()})
[void] $Harvester.ShowDialog()