I have a script that does a few things before starting a job and then executing:
get-content $OutFile -Tail 100 -Wait
Resulting in a background job producing an output to a file that is tailed and displayed.
Pressing Ctrl-C will exit the script altogether. All this works fine.
But, I would want the script to kill the job before exiting, after canceling the get-content command.
Yes, that is right. Right now I have a remove-job at the beginning of the script, removing any old instances of the job. But I really would like the job to be removed before the script ends.
If you end the script with <Ctrl>+<C> there is no way for your script to do anything else because it does not run anymore. You would need to implement a loop inside your script checking for a cancel condition and clean up before ending itself.