private void WriteReasonError(System.Management.Automation.Job job)
        {
            PSRemotingChildJob job2 = job as PSRemotingChildJob;

            if ((job2 != null) && (job2.FailureErrorRecord != null))
            {
                this._results.Add(new PSStreamObject(PSStreamObjectType.Error, job2.FailureErrorRecord, job2.InstanceId));
            }
            else if (job.JobStateInfo.Reason != null)
            {
                Exception          reason     = job.JobStateInfo.Reason;
                Exception          exception  = reason;
                JobFailedException exception3 = reason as JobFailedException;
                if (exception3 != null)
                {
                    exception = exception3.Reason;
                }
                ErrorRecord record = new ErrorRecord(exception, "JobStateFailed", ErrorCategory.InvalidResult, null);
                if ((exception3 != null) && (exception3.DisplayScriptPosition != null))
                {
                    if (record.InvocationInfo == null)
                    {
                        record.SetInvocationInfo(new InvocationInfo(null, null));
                    }
                    record.InvocationInfo.DisplayScriptPosition = exception3.DisplayScriptPosition;
                }
                this._results.Add(new PSStreamObject(PSStreamObjectType.Error, record, job.InstanceId));
            }
        }
 private static void DoUnblockJob(System.Management.Automation.Job job)
 {
     if (job.ChildJobs.Count == 0)
     {
         PSRemotingChildJob job2 = job as PSRemotingChildJob;
         if (job2 != null)
         {
             job2.UnblockJob();
         }
         else
         {
             job.SetJobState(JobState.Running, null);
         }
     }
 }
 private void WriteJobResults(System.Management.Automation.Job job)
 {
     if (job != null)
     {
         if (job.JobStateInfo.State == JobState.Disconnected)
         {
             PSRemotingChildJob job2 = job as PSRemotingChildJob;
             if ((job2 != null) && job2.DisconnectedAndBlocked)
             {
                 return;
             }
         }
         if (job.JobStateInfo.State == JobState.Blocked)
         {
             DoUnblockJob(job);
         }
         if (!(job is Job2) && job.UsesResultsCollection)
         {
             Collection <PSStreamObject> collection = this.ReadAll <PSStreamObject>(job.Results);
             if (this._wait)
             {
                 foreach (PSStreamObject obj2 in collection)
                 {
                     obj2.WriteStreamObject(this, job.Results.SourceId, false);
                 }
             }
             else
             {
                 foreach (PSStreamObject obj3 in collection)
                 {
                     obj3.WriteStreamObject(this, false);
                 }
             }
         }
         else
         {
             foreach (PSObject obj4 in this.ReadAll <PSObject>(job.Output))
             {
                 if (obj4 != null)
                 {
                     base.WriteObject(obj4);
                 }
             }
             foreach (ErrorRecord record in this.ReadAll <ErrorRecord>(job.Error))
             {
                 if (record != null)
                 {
                     MshCommandRuntime commandRuntime = base.CommandRuntime as MshCommandRuntime;
                     if (commandRuntime != null)
                     {
                         commandRuntime.WriteError(record, true);
                     }
                 }
             }
             foreach (VerboseRecord record2 in this.ReadAll <VerboseRecord>(job.Verbose))
             {
                 if (record2 != null)
                 {
                     MshCommandRuntime runtime2 = base.CommandRuntime as MshCommandRuntime;
                     if (runtime2 != null)
                     {
                         runtime2.WriteVerbose(record2, true);
                     }
                 }
             }
             foreach (DebugRecord record3 in this.ReadAll <DebugRecord>(job.Debug))
             {
                 if (record3 != null)
                 {
                     MshCommandRuntime runtime3 = base.CommandRuntime as MshCommandRuntime;
                     if (runtime3 != null)
                     {
                         runtime3.WriteDebug(record3, true);
                     }
                 }
             }
             foreach (WarningRecord record4 in this.ReadAll <WarningRecord>(job.Warning))
             {
                 if (record4 != null)
                 {
                     MshCommandRuntime runtime4 = base.CommandRuntime as MshCommandRuntime;
                     if (runtime4 != null)
                     {
                         runtime4.WriteWarning(record4, true);
                     }
                 }
             }
             foreach (ProgressRecord record5 in this.ReadAll <ProgressRecord>(job.Progress))
             {
                 if (record5 != null)
                 {
                     MshCommandRuntime runtime5 = base.CommandRuntime as MshCommandRuntime;
                     if (runtime5 != null)
                     {
                         runtime5.WriteProgress(record5, true);
                     }
                 }
             }
         }
         if (job.JobStateInfo.State == JobState.Failed)
         {
             this.WriteReasonError(job);
         }
     }
 }
 private void WriteJobResults(bool nonblocking)
 {
     if (this.job != null)
     {
         PipelineStoppedException exception = null;
         this.job.PropagateThrows = this.propagateErrors;
         do
         {
             if (!nonblocking)
             {
                 if (this.disconnectComplete != null)
                 {
                     WaitHandle.WaitAny(new WaitHandle[] { this.disconnectComplete, this.job.Results.WaitHandle });
                 }
                 else
                 {
                     this.job.Results.WaitHandle.WaitOne();
                 }
             }
             try
             {
                 this.WriteStreamObjectsFromCollection(this.job.ReadAll());
             }
             catch (PipelineStoppedException exception2)
             {
                 exception = exception2;
             }
         }while (!nonblocking && !this.job.IsTerminalState());
         try
         {
             this.WriteStreamObjectsFromCollection(this.job.ReadAll());
         }
         catch (PipelineStoppedException exception3)
         {
             exception = exception3;
         }
         if (exception != null)
         {
             this.HandlePipelinesStopped();
             throw exception;
         }
         if (this.job.JobStateInfo.State == JobState.Disconnected)
         {
             if ((base.ParameterSetName == "Session") || (base.ParameterSetName == "FilePathRunspace"))
             {
                 PSRemotingJob item = this.job.CreateDisconnectedRemotingJob();
                 if (item != null)
                 {
                     item.PSJobTypeName = RemoteJobType;
                     base.JobRepository.Add(item);
                     this.asjob = true;
                     foreach (Job job2 in item.ChildJobs)
                     {
                         PSRemotingChildJob job3 = job2 as PSRemotingChildJob;
                         if (job3 != null)
                         {
                             PSSession pSSession = this.GetPSSession(job3.Runspace.InstanceId);
                             if (pSSession != null)
                             {
                                 this.WriteNetworkFailedError(pSSession);
                                 base.WriteWarning(StringUtil.Format(RemotingErrorIdStrings.RCDisconnectSession, new object[] { pSSession.Name, pSSession.InstanceId, pSSession.ComputerName }));
                             }
                         }
                     }
                     base.WriteWarning(StringUtil.Format(RemotingErrorIdStrings.RCDisconnectedJob, item.Name));
                 }
             }
             else if ((base.ParameterSetName == "ComputerName") || (base.ParameterSetName == "FilePathComputerName"))
             {
                 foreach (PSSession session2 in this.GetDisconnectedSessions(this.job))
                 {
                     base.RunspaceRepository.AddOrReplace(session2);
                     this.WriteNetworkFailedError(session2);
                     base.WriteWarning(StringUtil.Format(RemotingErrorIdStrings.RCDisconnectSession, new object[] { session2.Name, session2.InstanceId, session2.ComputerName }));
                     base.WriteWarning(StringUtil.Format(RemotingErrorIdStrings.RCDisconnectSessionCreated, session2.Name, session2.InstanceId));
                 }
             }
             this.HandleThrottleComplete(null, null);
         }
     }
 }