Beispiel #1
0
        private static void InvokeCmdletMethodAndWaitForResults <T>(CmdletMethodInvoker <T> cmdletMethodInvoker, Cmdlet cmdlet)
        {
            Dbg.Assert(cmdletMethodInvoker != null, "Caller should verify cmdletMethodInvoker != null");

            cmdletMethodInvoker.MethodResult = default(T);
            try
            {
                T tmpMethodResult = cmdletMethodInvoker.Action(cmdlet);
                lock (cmdletMethodInvoker.SyncObject)
                {
                    cmdletMethodInvoker.MethodResult = tmpMethodResult;
                }
            }
            catch (Exception e)
            {
                lock (cmdletMethodInvoker.SyncObject)
                {
                    cmdletMethodInvoker.ExceptionThrownOnCmdletThread = e;
                }

                throw;
            }
            finally
            {
                if (cmdletMethodInvoker.Finished != null)
                {
                    cmdletMethodInvoker.Finished.Set();
                }
            }
        }
Beispiel #2
0
        private void InvokeCmdletMethodAndIgnoreResults(Action <Cmdlet> invokeCmdletMethod)
        {
            object obj2 = new object();
            CmdletMethodInvoker <object> invoker = new CmdletMethodInvoker <object> {
                Action = delegate(Cmdlet cmdlet) {
                    invokeCmdletMethod(cmdlet);
                    return(null);
                },
                Finished   = null,
                SyncObject = obj2
            };

            this.Results.Add(new PSStreamObject(PSStreamObjectType.BlockingError, invoker));
        }
Beispiel #3
0
        /// <summary>
        /// Handle the object obtained from an ObjectStream's reader
        /// based on its type.
        /// </summary>
        /// <param name="cmdlet">Cmdlet to use for outputting the object.</param>
        /// <param name="overrideInquire">Used by Receive-Job to suppress inquire preference.</param>
        public void WriteStreamObject(Cmdlet cmdlet, bool overrideInquire = false)
        {
            if (cmdlet != null)
            {
                switch (this.ObjectType)
                {
                case PSStreamObjectType.Output:
                {
                    cmdlet.WriteObject(this.Value);
                }

                break;

                case PSStreamObjectType.Error:
                {
                    ErrorRecord errorRecord = (ErrorRecord)this.Value;
                    errorRecord.PreserveInvocationInfoOnce = true;
                    MshCommandRuntime mshCommandRuntime = cmdlet.CommandRuntime as MshCommandRuntime;
                    if (mshCommandRuntime != null)
                    {
                        mshCommandRuntime.WriteError(errorRecord, overrideInquire);
                    }
                }

                break;

                case PSStreamObjectType.Debug:
                {
                    string            debug             = (string)Value;
                    DebugRecord       debugRecord       = new DebugRecord(debug);
                    MshCommandRuntime mshCommandRuntime = cmdlet.CommandRuntime as MshCommandRuntime;
                    if (mshCommandRuntime != null)
                    {
                        mshCommandRuntime.WriteDebug(debugRecord, overrideInquire);
                    }
                }

                break;

                case PSStreamObjectType.Warning:
                {
                    string            warning           = (string)Value;
                    WarningRecord     warningRecord     = new WarningRecord(warning);
                    MshCommandRuntime mshCommandRuntime = cmdlet.CommandRuntime as MshCommandRuntime;
                    if (mshCommandRuntime != null)
                    {
                        mshCommandRuntime.WriteWarning(warningRecord, overrideInquire);
                    }
                }

                break;

                case PSStreamObjectType.Verbose:
                {
                    string            verbose           = (string)Value;
                    VerboseRecord     verboseRecord     = new VerboseRecord(verbose);
                    MshCommandRuntime mshCommandRuntime = cmdlet.CommandRuntime as MshCommandRuntime;
                    if (mshCommandRuntime != null)
                    {
                        mshCommandRuntime.WriteVerbose(verboseRecord, overrideInquire);
                    }
                }

                break;

                case PSStreamObjectType.Progress:
                {
                    MshCommandRuntime mshCommandRuntime = cmdlet.CommandRuntime as MshCommandRuntime;
                    if (mshCommandRuntime != null)
                    {
                        mshCommandRuntime.WriteProgress((ProgressRecord)Value, overrideInquire);
                    }
                }

                break;

                case PSStreamObjectType.Information:
                {
                    MshCommandRuntime mshCommandRuntime = cmdlet.CommandRuntime as MshCommandRuntime;
                    if (mshCommandRuntime != null)
                    {
                        mshCommandRuntime.WriteInformation((InformationRecord)Value, overrideInquire);
                    }
                }

                break;

                case PSStreamObjectType.WarningRecord:
                {
                    WarningRecord     warningRecord     = (WarningRecord)Value;
                    MshCommandRuntime mshCommandRuntime = cmdlet.CommandRuntime as MshCommandRuntime;
                    if (mshCommandRuntime != null)
                    {
                        mshCommandRuntime.AppendWarningVarList(warningRecord);
                    }
                }

                break;

                case PSStreamObjectType.MethodExecutor:
                {
                    Dbg.Assert(this.Value is ClientMethodExecutor,
                               "Expected psstreamObject.value is ClientMethodExecutor");
                    ClientMethodExecutor methodExecutor = (ClientMethodExecutor)Value;
                    methodExecutor.Execute(cmdlet);
                }

                break;

                case PSStreamObjectType.BlockingError:
                {
                    CmdletMethodInvoker <object> methodInvoker = (CmdletMethodInvoker <object>)Value;
                    InvokeCmdletMethodAndWaitForResults(methodInvoker, cmdlet);
                }

                break;

                case PSStreamObjectType.ShouldMethod:
                {
                    CmdletMethodInvoker <bool> methodInvoker = (CmdletMethodInvoker <bool>)Value;
                    InvokeCmdletMethodAndWaitForResults(methodInvoker, cmdlet);
                }

                break;

                case PSStreamObjectType.Exception:
                {
                    Exception e = (Exception)Value;
                    throw e;
                }
                }
            }
            else if (ObjectType == PSStreamObjectType.Exception)
            {
                Exception e = (Exception)Value;
                throw e;
            }
        }
Beispiel #4
0
        internal void ReportJobFailure(IContainsErrorRecord exception)
        {
            TerminatingErrorTracker terminatingErrorTracker = TerminatingErrorTracker.GetTracker(this.JobContext.CmdletInvocationInfo);

            bool      sessionWasAlreadyTerminated = false;
            bool      isThisTerminatingError      = false;
            Exception brokenSessionException      = null;

            lock (_jobStateLock)
            {
                if (!_jobWasStopped)
                {
                    brokenSessionException = terminatingErrorTracker.GetExceptionIfBrokenSession(
                        this.JobContext.Session,
                        this.JobContext.CmdletInvocationContext.CmdletDefinitionContext.SkipTestConnection,
                        out sessionWasAlreadyTerminated);
                }
            }

            if (brokenSessionException != null)
            {
                string brokenSessionMessage = string.Format(
                    CultureInfo.InvariantCulture,
                    CmdletizationResources.CimJob_BrokenSession,
                    brokenSessionException.Message);
                exception = CimJobException.CreateWithFullControl(
                    this.JobContext,
                    brokenSessionMessage,
                    "CimJob_BrokenCimSession",
                    ErrorCategory.ResourceUnavailable,
                    brokenSessionException);
                isThisTerminatingError = true;
            }
            else
            {
                CimJobException cje = exception as CimJobException;
                if ((cje != null) && (cje.IsTerminatingError))
                {
                    terminatingErrorTracker.MarkSessionAsTerminated(this.JobContext.Session, out sessionWasAlreadyTerminated);
                    isThisTerminatingError = true;
                }
            }

            bool writeError = !sessionWasAlreadyTerminated;

            if (writeError)
            {
                lock (_jobStateLock)
                {
                    if (_jobWasStopped)
                    {
                        writeError = false;
                    }
                }
            }

            ErrorRecord errorRecord = exception.ErrorRecord;

            errorRecord.SetInvocationInfo(this.JobContext.CmdletInvocationInfo);
            errorRecord.PreserveInvocationInfoOnce = true;

            if (writeError)
            {
                lock (_jobStateLock)
                {
                    if (!_alreadyReachedCompletedState)
                    {
                        if (isThisTerminatingError)
                        {
                            this.Error.Add(errorRecord);
                            CmdletMethodInvoker <bool> methodInvoker = terminatingErrorTracker.GetErrorReportingDelegate(errorRecord);
                            this.Results.Add(new PSStreamObject(PSStreamObjectType.ShouldMethod, methodInvoker));
                        }
                        else
                        {
                            this.WriteError(errorRecord);
                        }
                    }
                }
            }

            this.SetCompletedJobState(JobState.Failed, errorRecord.Exception);
        }
Beispiel #5
0
        private T InvokeCmdletMethodAndWaitForResults <T>(Func <Cmdlet, T> invokeCmdletMethodAndReturnResult, out Exception exceptionThrownOnCmdletThread)
        {
            T         methodResult = default(T);
            Exception closureSafeExceptionThrownOnCmdletThread = null;
            object    resultsLock = new object();
            EventHandler <JobStateEventArgs> handler2 = null;

            using (ManualResetEventSlim gotResultEvent = new ManualResetEventSlim(false))
            {
                if (handler2 == null)
                {
                    handler2 = delegate(object sender, JobStateEventArgs eventArgs) {
                        if (this.IsFinishedState(eventArgs.JobStateInfo.State) || (eventArgs.JobStateInfo.State == JobState.Stopping))
                        {
                            lock (resultsLock)
                            {
                                closureSafeExceptionThrownOnCmdletThread = new OperationCanceledException();
                            }
                            gotResultEvent.Set();
                        }
                    };
                }
                EventHandler <JobStateEventArgs> handler = handler2;
                this.StateChanged += handler;
                Thread.MemoryBarrier();
                try
                {
                    handler(null, new JobStateEventArgs(this.JobStateInfo));
                    if (!gotResultEvent.IsSet)
                    {
                        this.SetJobState(JobState.Blocked);
                        CmdletMethodInvoker <T> invoker = new CmdletMethodInvoker <T> {
                            Action     = invokeCmdletMethodAndReturnResult,
                            Finished   = gotResultEvent,
                            SyncObject = resultsLock
                        };
                        PSStreamObjectType shouldMethod = PSStreamObjectType.ShouldMethod;
                        if (typeof(T) == typeof(object))
                        {
                            shouldMethod = PSStreamObjectType.BlockingError;
                        }
                        this.Results.Add(new PSStreamObject(shouldMethod, invoker));
                        gotResultEvent.Wait();
                        this.SetJobState(JobState.Running);
                        lock (resultsLock)
                        {
                            if (closureSafeExceptionThrownOnCmdletThread == null)
                            {
                                closureSafeExceptionThrownOnCmdletThread = invoker.ExceptionThrownOnCmdletThread;
                                methodResult = invoker.MethodResult;
                            }
                        }
                    }
                }
                finally
                {
                    this.StateChanged -= handler;
                }
            }
            lock (resultsLock)
            {
                exceptionThrownOnCmdletThread = closureSafeExceptionThrownOnCmdletThread;
                return(methodResult);
            }
        }
Beispiel #6
0
        internal void ReportJobFailure(IContainsErrorRecord exception)
        {
            TerminatingErrorTracker tracker = TerminatingErrorTracker.GetTracker(this.JobContext.CmdletInvocationInfo);
            bool      flag  = false;
            bool      flag1 = false;
            Exception exceptionIfBrokenSession = null;

            lock (this._jobStateLock)
            {
                if (!this._jobWasStopped)
                {
                    exceptionIfBrokenSession = tracker.GetExceptionIfBrokenSession(this.JobContext.Session, this.JobContext.CmdletInvocationContext.CmdletDefinitionContext.SkipTestConnection, out flag);
                }
            }
            if (exceptionIfBrokenSession == null)
            {
                CimJobException cimJobException = exception as CimJobException;
                if (cimJobException != null && cimJobException.IsTerminatingError)
                {
                    tracker.MarkSessionAsTerminated(this.JobContext.Session, out flag);
                    flag1 = true;
                }
            }
            else
            {
                object[] message = new object[1];
                message[0] = exceptionIfBrokenSession.Message;
                string str = string.Format(CultureInfo.InvariantCulture, CmdletizationResources.CimJob_BrokenSession, message);
                exception = CimJobException.CreateWithFullControl(this.JobContext, str, "CimJob_BrokenCimSession", ErrorCategory.ResourceUnavailable, exceptionIfBrokenSession);
                flag1     = true;
            }
            bool flag2 = !flag;

            if (flag2)
            {
                lock (this._jobStateLock)
                {
                    if (this._jobWasStopped)
                    {
                        flag2 = false;
                    }
                }
            }
            ErrorRecord errorRecord = exception.ErrorRecord;

            errorRecord.SetInvocationInfo(this.JobContext.CmdletInvocationInfo);
            errorRecord.PreserveInvocationInfoOnce = true;
            if (flag2)
            {
                lock (this._jobStateLock)
                {
                    if (!this._alreadyReachedCompletedState)
                    {
                        if (!flag1)
                        {
                            this.WriteError(errorRecord);
                        }
                        else
                        {
                            base.Error.Add(errorRecord);
                            CmdletMethodInvoker <bool> errorReportingDelegate = tracker.GetErrorReportingDelegate(errorRecord);
                            base.Results.Add(new PSStreamObject(PSStreamObjectType.ShouldMethod, errorReportingDelegate));
                        }
                    }
                }
            }
            this.SetCompletedJobState(JobState.Failed, errorRecord.Exception);
        }