0

This is a basic ping tool that has gone through many changes, but as it stands the stop function under the first button for some reason isn't defined and doesn't allow the process to stop.

  Add-Type -AssemblyName System.Windows.Forms, System.Drawing
$job = $null
$isRunning = $false


$form = New-Object System.Windows.Forms.Form
$form.Text = 'Clevagroup Pinger'
$form.Size = New-Object System.Drawing.Size(350,150)
$form.StartPosition = 'CenterScreen'
$form.Topmost = $true


$Button = New-Object System.Windows.Forms.Button
$Button.Location = New-Object System.Drawing.Size(35,35)
$Button.Size = New-Object System.Drawing.Size(120,23)
$Button.Text = "Ping"
$Button.Add_Click({
    if($global:isRunning -eq $false){
       $global:job = Start-Job -ScriptBlock {Ping 8.8.8.8 -t > $env:userprofile\desktop\PingResults}
       $Button.Text = "Running"
       $global:isRunning = $true
    } else {
            $Button.Text = "Stop Pinging"
            Stop-Job $global:job 
            $global:isRunning = $false
    }



   
})
$Form.Controls.Add($Button)


$Button1 = New-Object System.Windows.Forms.Button
$Button1.Location = New-Object System.Drawing.Size(195,35)
$Button1.Size = New-Object System.Drawing.Size(120,23)
$Button1.Text = "Close"
$Button1.Add_Click({

    if($global:job -ne $null){
        Stop-Job $global:job
    }

    
                      
})

$Form.Controls.Add($Button1)

$form.Add_Shown({$Button.Select()})
$result = $form.ShowDialog()

Thank you for any help you could give.

  • 2
    so ... at what point does that error show up? what is the EXACT text of that error? **_please add the full error text to your Question so folks can find it easily._** – Lee_Dailey Oct 27 '21 at 16:36
  • 1
    As an aside: It's better to use `$script:` variables, because `$global:` ones are _session_-global, i.e. they linger after your script exits. – mklement0 Oct 27 '21 at 16:53

1 Answers1

1

I added some comments to help you understand the thought process.

Add-Type -AssemblyName System.Windows.Forms, System.Drawing

# Set a reference hashtable where you can store the Job's object
$jobRef = @{ Job = '' }

$form = New-Object System.Windows.Forms.Form
$form.Text = 'Clevagroup Pinger'
$form.Size = New-Object System.Drawing.Size(350,150)
$form.StartPosition = 'CenterScreen'

$Button = New-Object System.Windows.Forms.Button
$Button.Location = New-Object System.Drawing.Size(35,35)
$Button.Size = New-Object System.Drawing.Size(120,23)
$Button.Text = "Ping"
$Button.Add_Click({

    # Save the Job on the reference hashtable
    $jobRef.Job = Start-Job -ScriptBlock {
        Ping 8.8.8.8 -t
    }

    # Disable the Ping Button
    $this.Enabled = $false

    # Enable the Stop Button
    $button1.Enabled = $true
})
$Form.Controls.Add($Button)

$Button1 = New-Object System.Windows.Forms.Button
$Button1.Location = New-Object System.Drawing.Size(195,35)
$Button1.Size = New-Object System.Drawing.Size(120,23)
$Button1.Text = "Stop"

# This button should be disabled by Default an only become 
# Enabled after 'Ping' button is Clicked
$Button1.Enabled = $false

$Button1.Add_Click({
    
    # Stop the Job
    Stop-Job $jobRef.Job

    # Receive the Job's result and store them in a Txt file
    Receive-Job $jobRef.Job | Out-File $env:userprofile\desktop\PingResults.txt

    # Remove the Job
    Remove-Job $jobRef.Job

    # Enable the Ping Button
    $button.Enabled = $true

    # Disable this Button
    $this.Enabled = $false
})

$form.Controls.Add($Button1)
$form.Add_Shown({
    $this.Activate()
    $Button.Select()
})
$form.ShowDialog()
Santiago Squarzon
  • 41,465
  • 5
  • 14
  • 37
  • 1
    Nicely done; interesting approach to use a hashtable instead of a script-level variable. It doesn't save much here, but `Receive-Job -Wait -AutoRemoveJob $jobRef.Job` could be used to receive the job's output and remove it in a single operation. – mklement0 Oct 27 '21 at 20:36
  • Thanks @mklement0, long time no talk :) got the `hashtable` workaround from [this answer](https://stackoverflow.com/a/39286782/15339544), I personally think is better approach than a `script` scoped variable I may be wrong tho. – Santiago Squarzon Oct 27 '21 at 20:48
  • 1
    :) It's definitely a valid alternative to `$script:` variable references, and arguably conceptually cleaner, but I'd say it's a matter of taste. ([This answer](https://stackoverflow.com/a/57649395/45375) explains _why_ either approach is necessary.) – mklement0 Oct 27 '21 at 20:54