Write-Error
only writes non-terminating errors, I believe, so perhaps the job is sensing that there's an error, but not necessarily a terminating error.
You might want to try either using throw
instead of Write-Error
, or explicitly exiting with a nonzero return code (though I'm not sure the latter actually makes much of a difference when it comes to reporting of the job state).
For a deeper understanding of terminating and non-terminating errors, and when to use Write-Error
vs. throw
, check out this thread.