private async Task AssertStateChange(PowerShellContextState expectedState)
        {
            SessionStateChangedEventArgs newState =
                await this.stateChangeQueue.DequeueAsync();

            Assert.Equal(expectedState, newState.NewSessionState);
        }
Example #2
0
 /// <summary>
 /// Creates a new instance of the SessionStateChangedEventArgs class.
 /// </summary>
 /// <param name="newSessionState">The new session state.</param>
 /// <param name="executionResult">The result of the operation that caused the state change.</param>
 /// <param name="errorException">An exception that describes the failure, if any.</param>
 public SessionStateChangedEventArgs(
     PowerShellContextState newSessionState,
     PowerShellExecutionResult executionResult,
     Exception errorException)
 {
     this.NewSessionState = newSessionState;
     this.ExecutionResult = executionResult;
     this.ErrorException  = errorException;
 }
 /// <summary>
 /// Creates a new instance of the SessionStateChangedEventArgs class.
 /// </summary>
 /// <param name="newSessionState">The new session state.</param>
 /// <param name="executionResult">The result of the operation that caused the state change.</param>
 /// <param name="errorException">An exception that describes the failure, if any.</param>
 public SessionStateChangedEventArgs(
     PowerShellContextState newSessionState,
     PowerShellExecutionResult executionResult,
     Exception errorException)
 {
     this.NewSessionState = newSessionState;
     this.ExecutionResult = executionResult;
     this.ErrorException = errorException;
 }
        private async Task AssertStateChange(
            PowerShellContextState expectedState,
            PowerShellExecutionResult expectedResult = PowerShellExecutionResult.Completed)
        {
            SessionStateChangedEventArgs newState =
                await this.sessionStateQueue.DequeueAsync(new CancellationTokenSource(10000).Token).ConfigureAwait(false);

            Assert.Equal(expectedState, newState.NewSessionState);
            Assert.Equal(expectedResult, newState.ExecutionResult);
        }
Example #5
0
        private async Task AssertStateChange(
            PowerShellContextState expectedState,
            PowerShellExecutionResult expectedResult = PowerShellExecutionResult.Completed)
        {
            SessionStateChangedEventArgs newState =
                await this.sessionStateQueue.DequeueAsync();

            Assert.Equal(expectedState, newState.NewSessionState);
            Assert.Equal(expectedResult, newState.ExecutionResult);
        }
        private static SessionStateChangedEventArgs TranslateInvocationStateInfo(PSInvocationStateInfo invocationState)
        {
            PowerShellContextState    newState        = PowerShellContextState.Unknown;
            PowerShellExecutionResult executionResult = PowerShellExecutionResult.NotFinished;

            switch (invocationState.State)
            {
            case PSInvocationState.NotStarted:
                newState = PowerShellContextState.NotStarted;
                break;

            case PSInvocationState.Failed:
                newState        = PowerShellContextState.Ready;
                executionResult = PowerShellExecutionResult.Failed;
                break;

            case PSInvocationState.Disconnected:
                // TODO: Any extra work to do in this case?
                // TODO: Is this a unique state that can be re-connected?
                newState        = PowerShellContextState.Disposed;
                executionResult = PowerShellExecutionResult.Stopped;
                break;

            case PSInvocationState.Running:
                newState = PowerShellContextState.Running;
                break;

            case PSInvocationState.Completed:
                newState        = PowerShellContextState.Ready;
                executionResult = PowerShellExecutionResult.Completed;
                break;

            case PSInvocationState.Stopping:
                // TODO: Collapse this so that the result shows that execution was aborted
                newState = PowerShellContextState.Aborting;
                break;

            case PSInvocationState.Stopped:
                newState        = PowerShellContextState.Ready;
                executionResult = PowerShellExecutionResult.Aborted;
                break;

            default:
                newState = PowerShellContextState.Unknown;
                break;
            }

            return
                (new SessionStateChangedEventArgs(
                     newState,
                     executionResult,
                     invocationState.Reason));
        }
        private void OnSessionStateChanged(object sender, SessionStateChangedEventArgs e)
        {
            Logger.Write(
                LogLevel.Verbose,
                string.Format(
                    "Session state changed --\r\n\r\n    Old state: {0}\r\n    New state: {1}",
                    this.SessionState.ToString(),
                    e.NewSessionState.ToString()));

            this.SessionState = e.NewSessionState;

            if (this.SessionStateChanged != null)
            {
                this.SessionStateChanged(sender, e);
            }
        }
        /// <summary>
        /// Disposes the runspace and any other resources being used
        /// by this PowerShellContext.
        /// </summary>
        public void Dispose()
        {
            this.SessionState = PowerShellContextState.Disposed;

            if (this.powerShell != null)
            {
                this.powerShell.InvocationStateChanged -= this.powerShell_InvocationStateChanged;
                this.powerShell.Dispose();
                this.powerShell = null;
            }

            if (this.ownsInitialRunspace && this.initialRunspace != null)
            {
                // TODO: Detach from events
                this.initialRunspace.Close();
                this.initialRunspace.Dispose();
                this.initialRunspace = null;
            }
        }
        private void Initialize(Runspace initialRunspace)
        {
            Validate.IsNotNull("initialRunspace", initialRunspace);

            this.SessionState = PowerShellContextState.NotStarted;

            this.initialRunspace = initialRunspace;
            this.currentRunspace = initialRunspace;
            this.currentRunspace.Debugger.SetDebugMode(DebugModes.LocalScript | DebugModes.RemoteScript);
            this.currentRunspace.Debugger.BreakpointUpdated += OnBreakpointUpdated;
            this.currentRunspace.Debugger.DebuggerStop      += OnDebuggerStop;

            this.powerShell = PowerShell.Create();
            this.powerShell.InvocationStateChanged += powerShell_InvocationStateChanged;
            this.powerShell.Runspace = this.currentRunspace;

            // TODO: Should this be configurable?
            this.SetExecutionPolicy(ExecutionPolicy.RemoteSigned);

            this.SessionState = PowerShellContextState.Ready;
        }
        private async Task AssertStateChange(
            PowerShellContextState expectedState,
            PowerShellExecutionResult expectedResult = PowerShellExecutionResult.Completed)
        {
            SessionStateChangedEventArgs newState =
                await this.sessionStateQueue.DequeueAsync();

            // TODO #22
            //Assert.Equal(expectedState, newState.NewSessionState);
            //Assert.Equal(expectedResult, newState.ExecutionResult);
        }
        private async Task AssertStateChange(PowerShellContextState expectedState)
        {
            SessionStateChangedEventArgs newState =
                await this.stateChangeQueue.DequeueAsync();

            Assert.Equal(expectedState, newState.NewSessionState);
        }