Beispiel #1
0
 public static Collection <PSObject> RawExecuteInLastRunspace(string[] commands, bool throwOnError = true)
 {
     foreach (var command in commands)
     {
         using (var pipeline = LastUsedRunspace.CreatePipeline())
         {
             pipeline.Commands.AddScript(command, false);
             try
             {
                 LastRawResults = pipeline.Invoke();
             }
             catch (Exception)
             {
                 // we need to store the results
                 LastRawResults = pipeline.Output.ReadToEnd();;
                 throw;
             }
             finally
             {
                 LastRawErrorResults = pipeline.Error.ReadToEnd();
                 MergeLastRawResultsToString();
             }
             if (throwOnError && LastRawErrorResults.Count > 0)
             {
                 throw new ExecutionWithErrorsException((from err in LastRawErrorResults
                                                         select((PSObject)err).BaseObject as ErrorRecord).ToArray());
             }
         }
     }
     return(LastRawResults);
 }
Beispiel #2
0
        public static Collection <PSObject> RawExecute(string[] commands)
        {
            LastRawResults   = null;
            LastUsedRunspace = InitialSessionState == null?
                               RunspaceFactory.CreateRunspace() : RunspaceFactory.CreateRunspace(InitialSessionState);

            LastUsedRunspace.Open();
            foreach (var command in commands)
            {
                using (var pipeline = LastUsedRunspace.CreatePipeline())
                {
                    pipeline.Commands.AddScript(command, true);
                    try
                    {
                        LastRawResults = pipeline.Invoke();
                    }
                    catch (Exception)
                    {
                        LastRawResults = pipeline.Output.ReadToEnd();
                        throw;
                    }
                    if (pipeline.Error.Count > 0)
                    {
                        throw new MethodInvocationException(String.Join(Environment.NewLine, pipeline.Error.ReadToEnd()));
                    }
                }
            }
            return(LastRawResults);
        }
Beispiel #3
0
        public static Collection <PSObject> RawExecute(string[] commands, bool throwOnError = true)
        {
            LastRawResults      = null;
            LastRawErrorResults = null;
            LastUsedRunspace    = InitialSessionState == null?
                                  RunspaceFactory.CreateRunspace() : RunspaceFactory.CreateRunspace(InitialSessionState);

            LastUsedRunspace.Open();
            return(RawExecuteInLastRunspace(commands, throwOnError));
        }