/// <summary> /// Invokes the Main PowerShell object synchronously. /// </summary> internal void InvokeMain() { PSInvocationSettings settings = PrepInvoke(true); Exception ex = null; try { LocalPowerShell.InvokeWithDebugger(InputCollection, _localPowerShellOutput, settings, true); } catch (Exception e) { ex = e; } if (ex != null) { // Since this is being invoked asynchronously on a single pipeline thread // any invoke failures (such as possible debugger failures) need to be // passed back to client or the original client invoke request will not respond. string failedCommand = LocalPowerShell.Commands.Commands[0].CommandText; LocalPowerShell.Commands.Clear(); string msg = StringUtil.Format( RemotingErrorIdStrings.ServerSideNestedCommandInvokeFailed, failedCommand ?? string.Empty, ex.Message ?? string.Empty); LocalPowerShell.AddCommand("Write-Error").AddArgument(msg); LocalPowerShell.Invoke(); } }
/// <summary> /// Runs no command but allows the PowerShell object on the client /// to complete. This is used for running "virtual" remote debug /// commands that sets debugger state but doesn't run any command /// on the server runspace. /// </summary> /// <param name="output">The output from preprocessing that we want to send to the client.</param> internal void RunNoOpCommand(IReadOnlyCollection <object> output) { if (LocalPowerShell != null) { System.Threading.ThreadPool.QueueUserWorkItem( (state) => { LocalPowerShell.SetStateChanged( new PSInvocationStateInfo( PSInvocationState.Running, null)); foreach (var item in output) { if (item != null) { _localPowerShellOutput.Add(PSObject.AsPSObject(item)); } } LocalPowerShell.SetStateChanged( new PSInvocationStateInfo( PSInvocationState.Completed, null)); }); } }
private IAsyncResult Start(bool startMainPowerShell) { PSInvocationSettings settings = PrepInvoke(startMainPowerShell); if (startMainPowerShell) { return(LocalPowerShell.BeginInvoke <object, PSObject>(InputCollection, _localPowerShellOutput, settings, null, null)); } else { return(_extraPowerShell.BeginInvoke <object, PSObject>(InputCollection, _localPowerShellOutput, settings, null, null)); } }
/// <summary> /// Stop the local powershell /// </summary> /// <param name="sender">sender of this event, unused</param> /// <param name="eventArgs">unused</param> private void HandleStopReceived(object sender, EventArgs eventArgs) { do // false loop { if (LocalPowerShell.InvocationStateInfo.State == PSInvocationState.Stopped || LocalPowerShell.InvocationStateInfo.State == PSInvocationState.Completed || LocalPowerShell.InvocationStateInfo.State == PSInvocationState.Failed || LocalPowerShell.InvocationStateInfo.State == PSInvocationState.Stopping) { break; } else { // Ensure that the local PowerShell command is not stopped in debug mode. bool handledByDebugger = false; if (!LocalPowerShell.IsNested && _psDriverInvoker != null) { handledByDebugger = _psDriverInvoker.HandleStopSignal(); } if (!handledByDebugger) { LocalPowerShell.Stop(); } } } while (false); if (_extraPowerShell != null) { do // false loop { if (_extraPowerShell.InvocationStateInfo.State == PSInvocationState.Stopped || _extraPowerShell.InvocationStateInfo.State == PSInvocationState.Completed || _extraPowerShell.InvocationStateInfo.State == PSInvocationState.Failed || _extraPowerShell.InvocationStateInfo.State == PSInvocationState.Stopping) { break; } else { _extraPowerShell.Stop(); } } while (false); } }
/// <summary> /// Runs no command but allows the PowerShell object on the client /// to complete. This is used for running "virtual" remote debug /// commands that sets debugger state but doesn't run any command /// on the server runspace. /// </summary> internal void RunNoOpCommand() { if (LocalPowerShell != null) { System.Threading.ThreadPool.QueueUserWorkItem( (state) => { LocalPowerShell.SetStateChanged( new PSInvocationStateInfo( PSInvocationState.Running, null)); LocalPowerShell.SetStateChanged( new PSInvocationStateInfo( PSInvocationState.Completed, null)); }); } }