tags:

views:

150

answers:

1

I have a PowerShell script that needs to run normally, and also when invoked via RunspaceInvoke, e.g.

using (RunspaceInvoke invoker = new RunspaceInvoke())
{
    invoker.Invoke(powerShellScript);
}

Part of this script doesn't need to run when being called with RunspaceInvoke, and moreover, it fails when being called with RunspaceInvoke.

Is there a way to detect (from within PowerShell) whether the script is being called with RunspaceInvoke?

Thanks

+1  A: 

I don't know how you can tell if you are being called from RunspaceInvoke. However, I suspect this will only happen in your own C#-based program that hosts the code you show above. If that is the case, then have your script test the value of $host.Name. In the C# execution context, this will return "Default Host".

Keith Hill
FYI, I thought you might be able to use StrackTrace and StackFrame but the script gets invoked on a different thread than the one that does the RunspaceInvoke. The resultant call stack appears to be identical.
Keith Hill