Home > Net >  PowerShell GUI - Move line from TextBox 1 to TextBox 2
PowerShell GUI - Move line from TextBox 1 to TextBox 2

Time:05-24

I am developing a script that will ping multiple computers and move the ones that were online to another inputbox. The script below works to ping multiple computers, but I am not sure how to move the computer name to the next inputbox($Inputbox_success).

Example:

Computers Successfully Ping
Computer 1
Computer 2
Computer 3

After I click the INSTALL (or PING) button. If the computer was online and ping was successful, it will move to the Successful InputBox.

Computers Successfully Ping
Computer 1
Computer 2
Computer 3

Thank you

# 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) ', '$1'
        $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()

CodePudding user response:

As stated in comments, even though this is doable with a demo

CodePudding user response:

Santiago, this is exactly what I was looking for!! Thank you so much! But I cannot replicate it on my own script. I think I am missing something, but not sure what. The dropdown menu was just an idea I had previously, but I do not think I will use it. I just want the buttons. Could you please take a look at my script? If the ping is successful, it does not move it to the Success Inputbox.

Thank you soo much for all your help. I really appreciate it!

# 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()
  • Related