Reputation: 19
$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
Reputation: 60928
try:
if ( [bool](get-job -Name MyTask -ea silentlycontinue) )
{
...
}
else
{
...
}
Upvotes: 4