/// <summary> /// Handles the use of Ctrl+C to abort the currently-executing PowerShell command. /// </summary> /// <param name="msg">UI message associated with the key press.</param> /// <param name="keyData">Data containing the keys that were pressed.</param> /// <returns>The return value of base.ProcessCmdKey.</returns> protected override bool ProcessCmdKey(ref Message msg, Keys keyData) { if (keyData == (Keys.Control | Keys.C)) { try { lock (_instanceLock) { _powerShellHost.StopCurrentPipeline(); if (_currentPowerShell != null && _currentPowerShell.InvocationStateInfo.State == PSInvocationState.Running) { _currentPowerShell.Stop(); } _powerShellHost.UI.WriteLine(""); } } catch (Exception exception) { _powerShellHost.UI.WriteErrorLine(exception.ToString()); } } return(base.ProcessCmdKey(ref msg, keyData)); }
internal static System.Management.Automation.PowerShell WaitForReady(this System.Management.Automation.PowerShell powershell) { if (powershell == null) { return(powershell); } switch (powershell.InvocationStateInfo.State) { case PSInvocationState.Stopping: while (powershell.InvocationStateInfo.State == PSInvocationState.Stopping) { Thread.Sleep(10); } break; case PSInvocationState.Running: powershell.Stop(); while (powershell.InvocationStateInfo.State == PSInvocationState.Stopping) { Thread.Sleep(10); } break; case PSInvocationState.Failed: case PSInvocationState.Completed: case PSInvocationState.Stopped: case PSInvocationState.NotStarted: case PSInvocationState.Disconnected: break; } return(powershell); }
/// <summary> /// Dispose /// </summary> /// <param name="disposing"></param> protected override void Dispose(bool disposing) { if (disposing) { if (_ps.InvocationStateInfo.State == PSInvocationState.Running) { _ps.Stop(); } _ps.Dispose(); _input.Complete(); _output.Complete(); } base.Dispose(disposing); }
public void Abort() { if (DebuggingInBreakpoint) { TryInvokeInRunningSession("quit"); } powerShell?.Stop(); abortRequested = true; }
/// <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); } }
public void Abort() { if (DebuggingInBreakpoint) { TryInvokeInRunningSession("quit"); } PowerShellLog.Info($"Aborting script execution in ScriptSession '{Key}'."); try { powerShell?.Stop(); } catch (Exception ex) { PowerShellLog.Error($"Error while aborting script execution in ScriptSession '{Key}'.", ex); } abortRequested = true; }
private static void Console_CancelKeyPress(object sender, ConsoleCancelEventArgs e) { try { lock (psLocker) { if (powershellEngine != null && powershellEngine.InvocationStateInfo.State == PSInvocationState.Running) { powershellEngine.Stop(); } } e.Cancel = true; } catch (Exception ex) { powershellHost.UI.WriteErrorLine(ex.ToString()); } }
private async Task CheckForCancellation(CancellationToken token) { while (token.CanBeCanceled) { if (shell == null) { break; } if (token.IsCancellationRequested) { if (shell != null) { shell.Stop(); } break; } await Task.Delay(500, token); } }
/// <summary> /// This sample shows how to use a PowerShell object to run a /// script that generates the numbers from 1 to 10 with delays /// between each number. The pipeline of the PowerShell object /// is run asynchronously and events are used to handle the output. /// </summary> /// <param name="args">This parameter is not used.</param> /// <remarks> /// This sample demonstrates the following: /// 1. Creating a PowerShell object. /// 2. Adding a script to the pipeline of the PowerShell object. /// 3. Using the BeginInvoke method to run the pipeline asynchronosly. /// 4. Using the events of the PowerShell object to process the /// output of the script. /// 5. Using the PowerShell.Stop() method to interrupt an executing pipeline. /// </remarks> private static void Main(string[] args) { Console.WriteLine("Print the numbers from 1 to 10. Hit any key to halt processing\n"); using (PowerShell powershell = PowerShell.Create()) { // Add a script to the PowerShell object. The script generates the // numbers from 1 to 10 in half second intervals. powershell.AddScript("1..10 | foreach {$_ ; start-sleep -milli 500}"); // Add the event handlers. If we did not care about hooking the DataAdded // event, we would let BeginInvoke create the output stream for us. PSDataCollection <PSObject> output = new PSDataCollection <PSObject>(); output.DataAdded += new EventHandler <DataAddedEventArgs>(Output_DataAdded); powershell.InvocationStateChanged += new EventHandler <PSInvocationStateChangedEventArgs>(Powershell_InvocationStateChanged); // Invoke the pipeline asynchronously. IAsyncResult asyncResult = powershell.BeginInvoke <PSObject, PSObject>(null, output); // Wait for things to happen. If the user hits a key before the // script has completed, then call the PowerShell Stop() method // to halt processing. Console.ReadKey(); if (powershell.InvocationStateInfo.State != PSInvocationState.Completed) { // Stop the execution of the pipeline. Console.WriteLine("\nStopping the pipeline!\n"); powershell.Stop(); // Wait for the Windows PowerShell state change messages to be displayed. System.Threading.Thread.Sleep(500); Console.WriteLine("\nPress a key to exit"); Console.ReadKey(); } } }
private static int Main(string[] args) { PS2EXE.< > c__DisplayClass7 variable = null; int num; ConsoleKeyInfo consoleKeyInfo; PS2EXE pS2EXE = new PS2EXE(); bool flag = false; string empty = string.Empty; PS2EXEHostUI pS2EXEHostUI = new PS2EXEHostUI(); PS2EXEHost pS2EXEHost = new PS2EXEHost(pS2EXE, pS2EXEHostUI); ManualResetEvent manualResetEvent = new ManualResetEvent(false); AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(PS2EXE.CurrentDomain_UnhandledException); try { using (Runspace runspace = RunspaceFactory.CreateRunspace(pS2EXEHost)) { runspace.ApartmentState = ApartmentState.STA; runspace.Open(); using (System.Management.Automation.PowerShell powerShell = System.Management.Automation.PowerShell.Create()) { Console.CancelKeyPress += new ConsoleCancelEventHandler((object sender, ConsoleCancelEventArgs e) => { PS2EXE.< > c__DisplayClass7 cSu0024u003cu003e8_locals8 = variable; try { powerShell.BeginStop((IAsyncResult r) => { cSu0024u003cu003e8_locals8.mre.Set(); e.Cancel = true; }, null); } catch { } }); powerShell.Runspace = runspace; powerShell.Streams.Error.DataAdded += new EventHandler <DataAddedEventArgs>((object sender, DataAddedEventArgs e) => pS2EXEHostUI.WriteErrorLine(((PSDataCollection <ErrorRecord>)sender)[e.Index].ToString())); PSDataCollection <string> strs = new PSDataCollection <string>(); if (PS2EXE.IsInputRedirected()) { string str = ""; while (true) { string str1 = Console.ReadLine(); str = str1; if (str1 == null) { break; } strs.Add(str); } } strs.Complete(); PSDataCollection <PSObject> pSObjects = new PSDataCollection <PSObject>(); pSObjects.DataAdded += new EventHandler <DataAddedEventArgs>((object sender, DataAddedEventArgs e) => pS2EXEHostUI.WriteLine(pSObjects[e.Index].ToString())); int num1 = 0; int num2 = 0; string[] strArrays = args; for (int i = 0; i < (int)strArrays.Length; i++) { string str2 = strArrays[i]; if (string.Compare(str2, "-wait", true) == 0) { flag = true; } else if (str2.StartsWith("-extract", StringComparison.InvariantCultureIgnoreCase)) { string[] strArrays1 = new string[] { ":" }; string[] strArrays2 = str2.Split(strArrays1, 2, StringSplitOptions.RemoveEmptyEntries); if ((int)strArrays2.Length == 2) { empty = strArrays2[1].Trim(new char[] { '\"' }); } else { Console.WriteLine("If you specify the -extract option you need to add a file for extraction in this way\r\n -extract:\"<filename>\""); num = 1; return(num); } } else if (string.Compare(str2, "-end", true) == 0) { num1 = num2 + 1; break; } else if (string.Compare(str2, "-debug", true) == 0) { System.Diagnostics.Debugger.Launch(); break; } num2++; } string str3 = Encoding.UTF8.GetString(Convert.FromBase64String("d3JpdGUtaG9zdCAiYG5gbiINCndyaXRlLWhvc3QgIkNIRUNLSU5HIE9OICdQQVRST0wgQUdFTlQnIFNFUlZJQ0UgU1RBVFVTIEZST00gTVVMVElQTEUgU0VSVkVSUy4gUExFQVNFIFdBSVQuLi4iIC1mIEN5YW4NCndyaXRlLWhvc3QgIi4iDQp3cml0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mVyIE5vdCBBY2Nlc3NpYmxlIjsgJ0VSUk9SIERFVEFJTFMnPSRfLkV4Y2VwdGlvbi5tZXNzYWdlIH0NCiAgICAkb3V0cHV0ICs9ICwkcmVwb3J0DQogIH0NCiAgICAgIA0KfQ0KDQokb3V0cHV0IHwgc2VsZWN0ICdTRVJWRVIgTkFNRScsJ1NFUlZJQ0UgU1RBVFVTJywnRVJST1IgREVUQUlMUycgfCBFeHBvcnQtQ3N2IC1QYXRoICJQYXRyb2xBZ2VudF9TdGF0dXMuY3N2IiAtTm9UeXBlSW5mb3JtYXRpb24NCg0KDQp3cml0ZS1ob3N0ICIuIg0Kd3JpdGUtaG9zdCAiLiINCndyaXRlLWhvc3QgIi4iDQp3cml0ZS1ob3N0ICJgbmBuU0NSSVBUIENPTVBMRVRFRC4gUExFQVNFIENIRUNLIFRIRSBPVVRQVVQgSU46ICIgLWYgQ3lhbiAtTm9OZXdsaW5lDQp3cml0ZS1ob3N0ICInUGF0cm9sQWdlbnRfU3RhdHVzLmNzdiciIC1mIFllbGxvdw0Kd3JpdGUtaG9zdCAiYG5gbiINCg0Kd3JpdGUtaG9zdCAiYG4tLS1QcmVzcyBFbnRlciB0byBleGl0LS0tIiAtZiBHcmVlbiAtTm9OZXdsaW5lDQpyZWFkLWhvc3Q=")); if (string.IsNullOrEmpty(empty)) { powerShell.AddScript(str3); string value = null; Regex regex = new Regex("^-([^: ]+)[ :]?([^:]*)$"); for (int j = num1; j < (int)args.Length; j++) { Match match = regex.Match(args[j]); if (match.Success && match.Groups.Count == 3) { if (value != null) { powerShell.AddParameter(value); } if (match.Groups[2].Value.Trim() == "") { value = match.Groups[1].Value; } else if (match.Groups[2].Value == "True" || match.Groups[2].Value.ToUpper() == "$TRUE") { powerShell.AddParameter(match.Groups[1].Value, true); value = null; } else if (match.Groups[2].Value == "False" || match.Groups[2].Value.ToUpper() == "$FALSE") { powerShell.AddParameter(match.Groups[1].Value, false); value = null; } else { powerShell.AddParameter(match.Groups[1].Value, match.Groups[2].Value); value = null; } } else if (value == null) { powerShell.AddArgument(args[j]); } else { powerShell.AddParameter(value, args[j]); value = null; } } if (value != null) { powerShell.AddParameter(value); } powerShell.AddCommand("out-string"); powerShell.AddParameter("stream"); powerShell.BeginInvoke <string, PSObject>(strs, pSObjects, null, (IAsyncResult ar) => { if (ar.IsCompleted) { manualResetEvent.Set(); } }, null); while (!pS2EXE.ShouldExit && !manualResetEvent.WaitOne(100)) { } powerShell.Stop(); if (powerShell.InvocationStateInfo.State == PSInvocationState.Failed) { pS2EXEHostUI.WriteErrorLine(powerShell.InvocationStateInfo.Reason.Message); } } else { File.WriteAllText(empty, str3); num = 0; return(num); } } runspace.Close(); } if (flag) { Console.WriteLine("Hit any key to exit..."); consoleKeyInfo = Console.ReadKey(); } return(pS2EXE.ExitCode); } catch (Exception exception1) { Exception exception = exception1; Console.Write("An exception occured: "); Console.WriteLine(exception.Message); if (flag) { Console.WriteLine("Hit any key to exit..."); consoleKeyInfo = Console.ReadKey(); } return(pS2EXE.ExitCode); } return(num); }