tags:

views:

21

answers:

1

I have a script module whose .psm1 file runs a command that will fail if a certain registry value is not present. I want to ignore this failure, so the .psm1 script runs the command as:

CommandThatMayFail -ea SilentlyContinue

This works just fine when I import the module in a PowerShell session. But one of the functions in this module starts some PS Jobs that also require the module to be imported. When I import the module in the job's InitializationScript the failures from CommandThatMayFail are not ignored and the job terminates. I tried wrapping CommandThatMayFail in a Try/Catch block, but that didn't help any.

I have avoided the problem by importing the module in the job's ScriptBlock instead of the InitializationScript. But I am still curious about what is going on here. Can anyone shed any light?

A: 

Try putting a trap in the same scope as the InitializationScript but PRECEDING it in the code:

Trap{
    <commands you want it to run in case of error>;
    continue
    }

The important part there is the semicolon and the continue. I had the same issue today, ironically enough, and this fixed it for me. I think it's an issue with terminating/non terminating errors and scopes but this is the only way I could resolve it in my own script.

JNK