예제 #1
0
        /// <summary>
        /// Returns a list of valid runspaces based on current parameter set.
        /// </summary>
        /// <returns>IReadOnlyList</returns>
        protected IReadOnlyList <Runspace> GetRunspaces()
        {
            IReadOnlyList <Runspace> results = null;

            if ((ParameterSetName == CommonRunspaceCommandBase.RunspaceNameParameterSet) && ((RunspaceName == null) || RunspaceName.Length == 0))
            {
                results = GetRunspaceUtils.GetAllRunspaces();
            }
            else
            {
                switch (ParameterSetName)
                {
                case CommonRunspaceCommandBase.RunspaceNameParameterSet:
                    results = GetRunspaceUtils.GetRunspacesByName(RunspaceName);
                    break;

                case CommonRunspaceCommandBase.RunspaceIdParameterSet:
                    results = GetRunspaceUtils.GetRunspacesById(RunspaceId);
                    break;

                case CommonRunspaceCommandBase.RunspaceParameterSet:
                    results = new ReadOnlyCollection <Runspace>(new List <Runspace>(Runspace));
                    break;

                case CommonRunspaceCommandBase.RunspaceInstanceIdParameterSet:
                    results = GetRunspaceUtils.GetRunspacesByInstanceId(RunspaceInstanceId);
                    break;
                }
            }

            return(results);
        }
        /// <summary>
        /// Returns a list of valid runspaces based on current parameter set.
        /// </summary>
        /// <returns>IReadOnlyList.</returns>
        protected IReadOnlyList <Runspace> GetRunspaces()
        {
            IReadOnlyList <Runspace> results = null;

            if ((ParameterSetName == CommonRunspaceCommandBase.RunspaceNameParameterSet) && ((RunspaceName is null) || RunspaceName.Length == 0))
            {
                results = GetRunspaceUtils.GetAllRunspaces();
            }
예제 #3
0
        /// <summary>
        /// Process record.
        /// </summary>
        protected override void ProcessRecord()
        {
            IReadOnlyList <Runspace> results;

            if ((ParameterSetName == GetRunspaceCommand.NameParameterSet) && ((Name is null) || Name.Length == 0))
            {
                results = GetRunspaceUtils.GetAllRunspaces();
            }
예제 #4
0
        private bool TryGetRunspace(Breakpoint breakpoint)
        {
            // Breakpoints retrieved from another runspace will have a RunspaceId note property of type Guid on them.
            var pso = new PSObject(breakpoint);
            var runspaceInstanceIdProperty = pso.Properties[RemotingConstants.RunspaceIdNoteProperty];

            if (runspaceInstanceIdProperty is null)
            {
                Runspace = Context.CurrentRunspace;
                return(true);
            }

            Debug.Assert(runspaceInstanceIdProperty.TypeNameOfValue.Equals("System.Guid", StringComparison.OrdinalIgnoreCase), "Instance ids must be GUIDs.");

            var runspaceInstanceId = (Guid)runspaceInstanceIdProperty.Value;

            if (runspaces.ContainsKey(runspaceInstanceId))
            {
                Runspace = runspaces[runspaceInstanceId];
                return(true);
            }

            var matchingRunspaces = GetRunspaceUtils.GetRunspacesByInstanceId(new[] { runspaceInstanceId });

            if (matchingRunspaces.Count != 1)
            {
                WriteError(
                    new ErrorRecord(
                        new ArgumentException(StringUtil.Format(Debugger.RunspaceInstanceIdNotFound, runspaceInstanceId)),
                        "PSBreakpoint:RunspaceInstanceIdNotFound",
                        ErrorCategory.InvalidArgument,
                        null));
                return(false);
            }

            Runspace = runspaces[runspaceInstanceId] = matchingRunspaces[0];
            return(true);
        }
        /// <summary>
        /// Process record
        /// </summary>
        protected override void ProcessRecord()
        {
            IReadOnlyList <Runspace> results;

            if ((ParameterSetName == GetRunspaceCommand.NameParameterSet) && ((Name == null) || Name.Length == 0))
            {
                results = GetRunspaceUtils.GetAllRunspaces();
            }
            else
            {
                switch (ParameterSetName)
                {
                case GetRunspaceCommand.NameParameterSet:
                    results = GetRunspaceUtils.GetRunspacesByName(Name);
                    break;

                case GetRunspaceCommand.IdParameterSet:
                    results = GetRunspaceUtils.GetRunspacesById(Id);
                    break;

                case GetRunspaceCommand.InstanceIdParameterSet:
                    results = GetRunspaceUtils.GetRunspacesByInstanceId(InstanceId);
                    break;

                default:
                    Dbg.Assert(false, "Unknown parameter set in GetRunspaceCommand");
                    results = new List <Runspace>().AsReadOnly();
                    break;
                }
            }

            foreach (Runspace runspace in results)
            {
                WriteObject(runspace);
            }
        }
예제 #6
0
        /// <summary>
        /// End processing.  Do work.
        /// </summary>
        protected override void EndProcessing()
        {
            if (ParameterSetName == DebugRunspaceCommand.RunspaceParameterSet)
            {
                _runspace = Runspace;
            }
            else
            {
                IReadOnlyList <Runspace> runspaces = null;

                switch (ParameterSetName)
                {
                case DebugRunspaceCommand.NameParameterSet:
                    runspaces = GetRunspaceUtils.GetRunspacesByName(new string[] { Name });
                    break;

                case DebugRunspaceCommand.IdParameterSet:
                    runspaces = GetRunspaceUtils.GetRunspacesById(new int[] { Id });
                    break;

                case DebugRunspaceCommand.InstanceIdParameterSet:
                    runspaces = GetRunspaceUtils.GetRunspacesByInstanceId(new Guid[] { InstanceId });
                    break;
                }

                if (runspaces.Count > 1)
                {
                    ThrowTerminatingError(
                        new ErrorRecord(
                            new PSArgumentException(Debugger.RunspaceDebuggingTooManyRunspacesFound),
                            "DebugRunspaceTooManyRunspaceFound",
                            ErrorCategory.InvalidOperation,
                            this)
                        );
                }

                if (runspaces.Count == 1)
                {
                    _runspace = runspaces[0];
                }
            }

            if (_runspace == null)
            {
                ThrowTerminatingError(
                    new ErrorRecord(
                        new PSArgumentNullException(Debugger.RunspaceDebuggingNoRunspaceFound),
                        "DebugRunspaceNoRunspaceFound",
                        ErrorCategory.InvalidOperation,
                        this)
                    );
            }

            Runspace defaultRunspace = LocalRunspace.DefaultRunspace;

            if (defaultRunspace == null || defaultRunspace.Debugger == null)
            {
                ThrowTerminatingError(
                    new ErrorRecord(
                        new PSInvalidOperationException(Debugger.RunspaceDebuggingNoHostRunspaceOrDebugger),
                        "DebugRunspaceNoHostDebugger",
                        ErrorCategory.InvalidOperation,
                        this)
                    );
            }

            if (_runspace == defaultRunspace)
            {
                ThrowTerminatingError(
                    new ErrorRecord(
                        new PSInvalidOperationException(Debugger.RunspaceDebuggingCannotDebugDefaultRunspace),
                        "DebugRunspaceCannotDebugHostRunspace",
                        ErrorCategory.InvalidOperation,
                        this)
                    );
            }

            if (this.Host == null || this.Host.UI == null)
            {
                ThrowTerminatingError(
                    new ErrorRecord(
                        new PSInvalidOperationException(Debugger.RunspaceDebuggingNoHost),
                        "DebugRunspaceNoHostAvailable",
                        ErrorCategory.InvalidOperation,
                        this)
                    );
            }

            if (!ShouldProcess(_runspace.Name, VerbsDiagnostic.Debug))
            {
                return;
            }

            _debugger = defaultRunspace.Debugger;

            try
            {
                PrepareRunspace(_runspace);

                // Blocking call.  Send runspace/command output to host UI while debugging and wait for runspace/command completion.
                WaitAndReceiveRunspaceOutput();
            }
            finally
            {
                RestoreRunspace(_runspace);
            }
        }