Beispiel #1
0
 private Collection<PSObject> ExecuteScriptInSessionState(string path, SessionState sessionState)
 {
     var scriptBlock = new ExternalScriptInfo(path, ScopeUsages.CurrentScope).ScriptBlock;
     var originalContext = _executionContext;
     var scopedContext = _executionContext.Clone(sessionState, ScopeUsages.CurrentScope);
     try
     {
         // actually change the scope by changing the execution context
         // there should definitely be a nicer way for this #ExecutionContextChange
         _executionContext = scopedContext;
         _executionContext.CurrentRunspace.ExecutionContext = scopedContext;
         return scriptBlock.Invoke(); // TODO: pass parameters if set
     }
     catch (ExitException e)
     {
         var exitCode = LanguagePrimitives.ConvertTo<int>(e.Argument);
         _executionContext.SetLastExitCodeVariable(exitCode);
         _executionContext.SetSuccessVariable(exitCode == 0);
         return new Collection<PSObject>() { PSObject.AsPSObject(e.Argument) };
     }
     finally
     {
         // restore original context / scope
         _executionContext.CurrentRunspace.ExecutionContext = originalContext;
         _executionContext = originalContext;
     }
 }