D'Pack
D'Pack

Reputation: 19

I want to check whether a job with specified name is running in powershell, if so remove the job using Remove-Job

$abc={script}

Start-Job $abc -name MyTask

The script runs in the background for a long time and hence I want to remove it when it's still running. when I try to remove the job using Remove-job -name MyTask -force, it throws an error if the job was already removed. So I need an

If(Check the job name exist)
 {
     Remove-job -name MyTask -force 
 }

Upvotes: 2

Views: 4460

Answers (1)

CB.
CB.

Reputation: 60928

try:

if ( [bool](get-job -Name MyTask -ea silentlycontinue) )
{
  ...
}
else
{
  ...
}

Upvotes: 4

Related Questions