2

I had wrote a powershell script to ping a ip address and I want to display a ping result to powershell form textboxt. I tried the below scripts below script. Script is working but not clear output.

Herewith i had attached the powershell output and powershell form textbox outputs.

[PowerShell output][1]

    # Load required assemblies
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing

# Create Form to contain elements
$Form = New-Object System.Windows.Forms.Form

# Set Form Titlebar text
$Form.Text = "PING"

# Set size of form
# Size(<length>,<height>) in pixels
$Form.Size = New-Object System.Drawing.Size(600,600)


# Create an Input textbox
$inputBoxui = New-Object System.Windows.Forms.TextBox
$inputBoxui.Location = New-Object System.Drawing.Size(20,50)
$inputBoxui.Size = New-Object System.Drawing.Size(100,20)

# Initialize the textbox inside the Form
$Form.Controls.Add($inputBoxui)


# Create Instruction Label for inputBox
$Labelui = New-Object System.Windows.Forms.Label
$Labelui.Text = "Enter IP Address"
$Labelui.Location = New-Object System.Drawing.Size(20,30)
$Labelui.BackColor = "Transparent"
$Labelui.AutoSize = $true

# Initialize Label
$Form.Controls.Add($Labelui)

# Create an Output textbox, 10 pixels in from Form Boundary and 150 pixels down
# As we want a multiline output set textbox size to 565 px x 200 px
# .Multiline declares the textbox is multi-line
$outputBox = New-Object System.Windows.Forms.TextBox
$outputBox.Location = New-Object System.Drawing.Size(10,250)
$outputBox.Size = New-Object System.Drawing.Size(565,300)
$outputBox.MultiLine = $True
$outputBox.Scrollbars = "Vertical"

# Initialize the textbox inside the Form
$Form.Controls.Add($OutputBox)




# Add a Button which can be used to generate an action from our textboxes
$Button = New-Object System.Windows.Forms.Button
$Button.Location = New-Object System.Drawing.Size(20,80)
$Button.Size = New-Object System.Drawing.Size(100,40)
$Button.Text = "PING Start"

# Declare the action to occur when button clicked
$Button.Add_Click( { pingstart } )

# Initialize the button inside the Form
$Form.Controls.Add($Button)

# Create a Function
function pingstart {

  $outputBox.Clear()

  # Variable to store what user types into Input textbox
  $Inputui = $inputBoxui.Text

  while ($true) {

        $Computer = $Inputui
        $Ping = Test-Connection -Count 3 -ComputerName $Computer
    ForEach ($Result in $Ping) {
        If ($Result.ResponseTime -lt 100) {
        $Result | Select-Object -Property Address,BufferSize,ResponseTime | Write-Host -BackgroundColor Green
        }
    If ( ($Result.ResponseTime -ge 100) -and ($Result.ResponseTime -lt 200) ) {
         $Result | Select-Object -Property Address,BufferSize,ResponseTime | Write-Host -BackgroundColor Yellow
        }
  If ($Result.ResponseTime -ge 200) {
        $Result | Select-Object -Property Address,BufferSize,ResponseTime | Write-Host -BackgroundColor Red
  }

}
# Assign Result to OutputBox
  $outputBox.Text = $Ping
}

}

# Initialize form and show it
# [void] used to suppress other messages generated by Form actions
[void] $Form.ShowDialog()  
1
  • Looks like $outputBox.Text = $Ping is unreachable because of the while($true) loop. Commented Mar 21, 2022 at 10:13

1 Answer 1

1

unrelated but it seems there is a mistake with this

Initialize the textbox inside the Form

$Form.Controls.Add($OutputBox)

caps lock.

$outputBox = New-Object System.Windows.Forms.TextBox
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.