Harry Goodall
Harry Goodall

Reputation: 11

Stop-Job Global:job isn't defined as an ID or value

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.

Upvotes: 0

Views: 56

Answers (1)

Santiago Squarzon
Santiago Squarzon

Reputation: 60718

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()

Upvotes: 1

Related Questions