示例#1
0
        /// <summary>
        /// Creates a <see cref="System.Management.Automation.Job"/> object that invokes a static method
        /// (of the class named by <see cref="Microsoft.PowerShell.Cmdletization.CmdletAdapter&lt;TObjectInstance&gt;.ClassName"/>)
        /// in the wrapped object model.
        /// </summary>
        /// <param name="session">Remote session to invoke the method in.</param>
        /// <param name="methodInvocationInfo">Method invocation details.</param>
        internal override StartableJob CreateStaticMethodInvocationJob(CimSession session, MethodInvocationInfo methodInvocationInfo)
        {
            TerminatingErrorTracker tracker = TerminatingErrorTracker.GetTracker(this.CmdletInvocationInfo, isStaticCmdlet: true);

            if (tracker.IsSessionTerminated(session))
            {
                return(null);
            }

            if (!IsSupportedSession(session, tracker))
            {
                return(null);
            }

            CimJobContext jobContext = this.CreateJobContext(session, targetObject: null);

            StartableJob result;

            if (methodInvocationInfo.MethodName.Equals(CimCmdletAdapter.CreateInstance_MethodName, StringComparison.OrdinalIgnoreCase))
            {
                result = new CreateInstanceJob(
                    jobContext,
                    methodInvocationInfo);
            }
            else
            {
                result = new StaticMethodInvocationJob(
                    jobContext,
                    methodInvocationInfo);
            }

            return(result);
        }
示例#2
0
        private bool IsSupportedSession(CimSession cimSession, TerminatingErrorTracker terminatingErrorTracker)
        {
            bool   flag = false;
            string str;
            bool   flag1 = this.CmdletInvocationInfo.BoundParameters.ContainsKey("Confirm");
            bool   flag2 = this.CmdletInvocationInfo.BoundParameters.ContainsKey("WhatIf");

            if ((flag1 || flag2) && cimSession.ComputerName != null && !cimSession.ComputerName.Equals("localhost", StringComparison.OrdinalIgnoreCase))
            {
                PSPropertyInfo item = PSObject.AsPSObject(cimSession).Properties["Protocol"];
                if (item != null && item.Value != null && item.Value.ToString().Equals("DCOM", StringComparison.OrdinalIgnoreCase))
                {
                    terminatingErrorTracker.MarkSessionAsTerminated(cimSession, out flag);
                    if (!flag)
                    {
                        if (!flag1)
                        {
                            str = "-WhatIf";
                        }
                        else
                        {
                            str = "-Confirm";
                        }
                        object[] computerName = new object[2];
                        computerName[0] = cimSession.ComputerName;
                        computerName[1] = str;
                        string      str1 = string.Format(CultureInfo.InvariantCulture, CmdletizationResources.CimCmdletAdapter_RemoteDcomDoesntSupportExtendedSemantics, computerName);
                        Exception   notSupportedException = new NotSupportedException(str1);
                        ErrorRecord errorRecord           = new ErrorRecord(notSupportedException, "NoExtendedSemanticsSupportInRemoteDcomProtocol", ErrorCategory.NotImplemented, cimSession);
                        base.Cmdlet.WriteError(errorRecord);
                    }
                }
            }
            return(true);
        }
示例#3
0
        /// <summary>
        /// Creates a <see cref="System.Management.Automation.Job"/> object that performs a query against the wrapped object model.
        /// </summary>
        /// <param name="session">Remote session to query</param>
        /// <param name="baseQuery">Query parameters</param>
        /// <returns><see cref="System.Management.Automation.Job"/> object that performs a query against the wrapped object model</returns>
        internal override StartableJob CreateQueryJob(CimSession session, QueryBuilder baseQuery)
        {
            CimQuery query = baseQuery as CimQuery;

            if (query == null)
            {
                throw new ArgumentNullException("baseQuery");
            }

            TerminatingErrorTracker tracker = TerminatingErrorTracker.GetTracker(this.CmdletInvocationInfo, isStaticCmdlet: false);

            if (tracker.IsSessionTerminated(session))
            {
                return(null);
            }

            if (!IsSupportedSession(session, tracker))
            {
                return(null);
            }

            CimJobContext jobContext = this.CreateJobContext(session, targetObject: null);
            StartableJob  queryJob   = query.GetQueryJob(jobContext);

            return(queryJob);
        }
示例#4
0
        internal override StartableJob CreateStaticMethodInvocationJob(CimSession session, MethodInvocationInfo methodInvocationInfo)
        {
            StartableJob            staticMethodInvocationJob;
            TerminatingErrorTracker tracker = TerminatingErrorTracker.GetTracker(this.CmdletInvocationInfo, true);

            if (!tracker.IsSessionTerminated(session))
            {
                if (this.IsSupportedSession(session, tracker))
                {
                    CimJobContext cimJobContext = this.CreateJobContext(session, null);
                    if (!methodInvocationInfo.MethodName.Equals("cim:CreateInstance", StringComparison.OrdinalIgnoreCase))
                    {
                        staticMethodInvocationJob = new StaticMethodInvocationJob(cimJobContext, methodInvocationInfo);
                    }
                    else
                    {
                        staticMethodInvocationJob = new CreateInstanceJob(cimJobContext, methodInvocationInfo);
                    }
                    return(staticMethodInvocationJob);
                }
                else
                {
                    return(null);
                }
            }
            else
            {
                return(null);
            }
        }
示例#5
0
        internal override StartableJob CreateQueryJob(CimSession session, QueryBuilder baseQuery)
        {
            CimQuery cimQuery = baseQuery as CimQuery;

            if (cimQuery != null)
            {
                TerminatingErrorTracker tracker = TerminatingErrorTracker.GetTracker(this.CmdletInvocationInfo, false);
                if (!tracker.IsSessionTerminated(session))
                {
                    if (this.IsSupportedSession(session, tracker))
                    {
                        CimJobContext cimJobContext = this.CreateJobContext(session, null);
                        StartableJob  queryJob      = cimQuery.GetQueryJob(cimJobContext);
                        return(queryJob);
                    }
                    else
                    {
                        return(null);
                    }
                }
                else
                {
                    return(null);
                }
            }
            else
            {
                throw new ArgumentNullException("baseQuery");
            }
        }
示例#6
0
        internal static TerminatingErrorTracker GetTracker(InvocationInfo invocationInfo)
        {
            TerminatingErrorTracker terminatingErrorTracker = null;

            TerminatingErrorTracker.InvocationToTracker.TryGetValue(invocationInfo, out terminatingErrorTracker);
            return(terminatingErrorTracker);
        }
示例#7
0
        private bool IsSupportedSession(CimSession cimSession, TerminatingErrorTracker terminatingErrorTracker)
        {
            bool confirmSwitchSpecified = this.CmdletInvocationInfo.BoundParameters.ContainsKey("Confirm");
            bool whatIfSwitchSpecified  = this.CmdletInvocationInfo.BoundParameters.ContainsKey("WhatIf");

            if (confirmSwitchSpecified || whatIfSwitchSpecified)
            {
                if (cimSession.ComputerName != null && (!cimSession.ComputerName.Equals("localhost", StringComparison.OrdinalIgnoreCase)))
                {
                    PSPropertyInfo protocolProperty = PSObject.AsPSObject(cimSession).Properties["Protocol"];
                    if ((protocolProperty != null) &&
                        (protocolProperty.Value != null) &&
                        (protocolProperty.Value.ToString().Equals("DCOM", StringComparison.OrdinalIgnoreCase)))
                    {
                        bool sessionWasAlreadyTerminated;
                        terminatingErrorTracker.MarkSessionAsTerminated(cimSession, out sessionWasAlreadyTerminated);
                        if (!sessionWasAlreadyTerminated)
                        {
                            string nameOfUnsupportedSwitch;
                            if (confirmSwitchSpecified)
                            {
                                nameOfUnsupportedSwitch = "-Confirm";
                            }
                            else
                            {
                                Dbg.Assert(whatIfSwitchSpecified, "Confirm and WhatIf are the only detected settings");
                                nameOfUnsupportedSwitch = "-WhatIf";
                            }

                            string errorMessage = string.Format(
                                CultureInfo.InvariantCulture,
                                CmdletizationResources.CimCmdletAdapter_RemoteDcomDoesntSupportExtendedSemantics,
                                cimSession.ComputerName,
                                nameOfUnsupportedSwitch);
                            Exception   exception   = new NotSupportedException(errorMessage);
                            ErrorRecord errorRecord = new(
                                exception,
                                "NoExtendedSemanticsSupportInRemoteDcomProtocol",
                                ErrorCategory.NotImplemented,
                                cimSession);
                            this.Cmdlet.WriteError(errorRecord);
                        }
                    }
                }
            }

            return(true);
        }
示例#8
0
        /// <summary>
        /// Creates a <see cref="System.Management.Automation.Job"/> object that invokes an instance method in the wrapped object model.
        /// </summary>
        /// <param name="session">Remote session to invoke the method in.</param>
        /// <param name="objectInstance">The object on which to invoke the method.</param>
        /// <param name="methodInvocationInfo">Method invocation details.</param>
        /// <param name="passThru"><see langword="true"/> if successful method invocations should emit downstream the <paramref name="objectInstance"/> being operated on.</param>
        /// <returns></returns>
        internal override StartableJob CreateInstanceMethodInvocationJob(CimSession session, CimInstance objectInstance, MethodInvocationInfo methodInvocationInfo, bool passThru)
        {
            TerminatingErrorTracker tracker = TerminatingErrorTracker.GetTracker(this.CmdletInvocationInfo, isStaticCmdlet: false);

            if (tracker.IsSessionTerminated(session))
            {
                return(null);
            }

            if (!IsSupportedSession(session, tracker))
            {
                return(null);
            }

            CimJobContext jobContext = this.CreateJobContext(session, objectInstance);

            Dbg.Assert(objectInstance != null, "Caller should verify objectInstance != null");

            StartableJob result;

            if (methodInvocationInfo.MethodName.Equals(CimCmdletAdapter.DeleteInstance_MethodName, StringComparison.OrdinalIgnoreCase))
            {
                result = new DeleteInstanceJob(
                    jobContext,
                    passThru,
                    objectInstance,
                    methodInvocationInfo);
            }
            else if (methodInvocationInfo.MethodName.Equals(CimCmdletAdapter.ModifyInstance_MethodName, StringComparison.OrdinalIgnoreCase))
            {
                result = new ModifyInstanceJob(
                    jobContext,
                    passThru,
                    objectInstance,
                    methodInvocationInfo);
            }
            else
            {
                result = new InstanceMethodInvocationJob(
                    jobContext,
                    passThru,
                    objectInstance,
                    methodInvocationInfo);
            }

            return(result);
        }
示例#9
0
        internal override void StartJob()
        {
            lock (_jobStateLock)
            {
                if (_jobWasStopped)
                {
                    this.SetCompletedJobState(JobState.Stopped, null);
                    return;
                }

                Dbg.Assert(!_alreadyReachedCompletedState, "Job shouldn't reach completed state, before ThrottlingJob has a chance to register for job-completed/failed events");

                TerminatingErrorTracker tracker = TerminatingErrorTracker.GetTracker(this.JobContext.CmdletInvocationInfo);
                if (tracker.IsSessionTerminated(this.JobContext.Session))
                {
                    this.SetCompletedJobState(JobState.Failed, new OperationCanceledException());
                    return;
                }

                if (!_jobWasStarted)
                {
                    _jobWasStarted = true;
                    this.SetJobState(JobState.Running);
                }
            }

            // This invocation can block (i.e. by calling Job.ShouldProcess) and wait for pipeline thread to unblock it
            // Therefore we have to do the invocation outside of the pipeline thread.
            ThreadPool.QueueUserWorkItem(delegate
            {
                this.ExceptionSafeWrapper(delegate
                {
                    IObservable <T> observable = this.GetCimOperation();
                    if (observable != null)
                    {
                        observable.Subscribe(this);
                    }
                });
            });
        }
示例#10
0
        internal override StartableJob CreateInstanceMethodInvocationJob(CimSession session, CimInstance objectInstance, MethodInvocationInfo methodInvocationInfo, bool passThru)
        {
            StartableJob            instanceMethodInvocationJob;
            TerminatingErrorTracker tracker = TerminatingErrorTracker.GetTracker(this.CmdletInvocationInfo, false);

            if (!tracker.IsSessionTerminated(session))
            {
                if (this.IsSupportedSession(session, tracker))
                {
                    CimJobContext cimJobContext = this.CreateJobContext(session, objectInstance);
                    if (!methodInvocationInfo.MethodName.Equals("cim:DeleteInstance", StringComparison.OrdinalIgnoreCase))
                    {
                        if (!methodInvocationInfo.MethodName.Equals("cim:ModifyInstance", StringComparison.OrdinalIgnoreCase))
                        {
                            instanceMethodInvocationJob = new InstanceMethodInvocationJob(cimJobContext, passThru, objectInstance, methodInvocationInfo);
                        }
                        else
                        {
                            instanceMethodInvocationJob = new ModifyInstanceJob(cimJobContext, passThru, objectInstance, methodInvocationInfo);
                        }
                    }
                    else
                    {
                        instanceMethodInvocationJob = new DeleteInstanceJob(cimJobContext, passThru, objectInstance, methodInvocationInfo);
                    }
                    return(instanceMethodInvocationJob);
                }
                else
                {
                    return(null);
                }
            }
            else
            {
                return(null);
            }
        }
示例#11
0
 internal override void StartJob()
 {
     lock (this._jobStateLock)
     {
         if (!this._jobWasStopped)
         {
             TerminatingErrorTracker tracker = TerminatingErrorTracker.GetTracker(this.JobContext.CmdletInvocationInfo);
             if (!tracker.IsSessionTerminated(this.JobContext.Session))
             {
                 if (!this._jobWasStarted)
                 {
                     this._jobWasStarted = true;
                     base.SetJobState(JobState.Running);
                 }
             }
             else
             {
                 this.SetCompletedJobState(JobState.Failed, new OperationCanceledException());
                 return;
             }
         }
         else
         {
             this.SetCompletedJobState(JobState.Stopped, null);
             return;
         }
     }
     ThreadPool.QueueUserWorkItem(param0 => this.ExceptionSafeWrapper(delegate
     {
         IObservable <T> cimOperation = this.GetCimOperation();
         if (cimOperation != null)
         {
             cimOperation.Subscribe(this);
         }
     }));
 }
示例#12
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);
        }
示例#13
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);
        }
示例#14
0
        internal static TerminatingErrorTracker GetTracker(InvocationInfo invocationInfo, bool isStaticCmdlet)
        {
            TerminatingErrorTracker value = TerminatingErrorTracker.InvocationToTracker.GetValue(invocationInfo, (InvocationInfo argument0) => new TerminatingErrorTracker(TerminatingErrorTracker.GetNumberOfSessions(invocationInfo)));

            return(value);
        }