SetLastExitCodeVariable() private method

private SetLastExitCodeVariable ( int exitCode ) : void
exitCode int
return void
Ejemplo n.º 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;
            }
        }
Ejemplo n.º 2
0
 /// <summary>
 /// In a script block, we only provide the pipeline as the automatic $input variable, but call it only once
 /// </summary>
 public override void ProcessRecords()
 {
     // TODO: provide the automatic $input variable
     // TODO: currently we don't support "cmdlet scripts", i.e. functions that behave like cmdlets.
     // TODO: Investigate the usage of different function blocks properly before implementing them
     //set the execution context of the runspace to the new context for execution in it
     SwitchToOwnScope();
     try
     {
         _scriptBlockInfo.ScriptBlock.Ast.Visit(_scopedExecutionVisitor);
     }
     catch (FlowControlException e)
     {
         if (!_fromFile || e is LoopFlowException)
         {
             throw; // gets propagated if the script block is not an external script or it's a break/continue
         }
         if (e is ExitException)
         {
             int exitCode = 0;
             LanguagePrimitives.TryConvertTo <int>(((ExitException)e).Argument, out exitCode);
             _exitCode = exitCode;
             ExecutionContext.SetLastExitCodeVariable(exitCode);
         }
         // otherwise (return), we simply stop execution of the script (that's why we're here) and do nothing
     }
     finally //make sure we switch back to the original execution context, no matter what happened
     {
         RestoreOriginalScope();
     }
 }
Ejemplo n.º 3
0
        private void ProcessExited(object sender, System.EventArgs e)
        {
            ExecutionContext.SetLastExitCodeVariable(_process.ExitCode);
            ExecutionContext.SetSuccessVariable(_process.ExitCode == 0); // PS also just compares against null

            _process.Dispose();
            _process = null;
        }
Ejemplo n.º 4
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;
     }
 }