| | | 1 | | using System.Management.Automation; |
| | | 2 | | using Serilog.Events; |
| | | 3 | | |
| | | 4 | | namespace Kestrun.Languages; |
| | | 5 | | |
| | | 6 | | /// <summary> |
| | | 7 | | /// Shared helper methods for configuring and invoking PowerShell scripts in a consistent manner. |
| | | 8 | | /// Extracted to reduce duplication between <see cref="PowerShellDelegateBuilder"/> and script-based probes. |
| | | 9 | | /// </summary> |
| | | 10 | | internal static class PowerShellExecutionHelpers |
| | | 11 | | { |
| | | 12 | | /// <summary> |
| | | 13 | | /// Sets variables in the PowerShell runspace from the provided argument dictionary. |
| | | 14 | | /// Existing variables are overwritten. Null or empty collections are ignored. |
| | | 15 | | /// </summary> |
| | | 16 | | internal static void SetVariables(PowerShell ps, IReadOnlyDictionary<string, object?>? arguments, Serilog.ILogger lo |
| | | 17 | | { |
| | 7 | 18 | | if (arguments is null || arguments.Count == 0) |
| | | 19 | | { |
| | 4 | 20 | | return; |
| | | 21 | | } |
| | | 22 | | |
| | 3 | 23 | | if (log.IsEnabled(LogEventLevel.Verbose)) |
| | | 24 | | { |
| | 0 | 25 | | log.Verbose("Setting PowerShell variables from arguments: {Count}", arguments.Count); |
| | | 26 | | } |
| | 3 | 27 | | var proxy = ps.Runspace.SessionStateProxy; |
| | 18 | 28 | | foreach (var kv in arguments) |
| | | 29 | | { |
| | 6 | 30 | | proxy.SetVariable(kv.Key, kv.Value); |
| | | 31 | | } |
| | 3 | 32 | | } |
| | | 33 | | |
| | | 34 | | /// <summary> |
| | | 35 | | /// Adds a script block to the pipeline. |
| | | 36 | | /// </summary> |
| | 4 | 37 | | internal static void AddScript(PowerShell ps, string code) => _ = ps.AddScript(code); |
| | | 38 | | |
| | | 39 | | /// <summary> |
| | | 40 | | /// Invokes the configured PowerShell pipeline asynchronously with cooperative cancellation. |
| | | 41 | | /// Cancellation attempts to stop the pipeline gracefully. |
| | | 42 | | /// </summary> |
| | | 43 | | internal static async Task<PSDataCollection<PSObject>> InvokeAsync(this PowerShell ps, Serilog.ILogger log, Cancella |
| | | 44 | | { |
| | 16 | 45 | | if (log.IsEnabled(LogEventLevel.Verbose)) |
| | | 46 | | { |
| | 0 | 47 | | log.Verbose("Executing PowerShell script..."); |
| | | 48 | | } |
| | | 49 | | |
| | | 50 | | // If cancellation is already requested before the pipeline starts, do not invoke at all. |
| | | 51 | | // On some platforms/runtimes, calling Stop() before invocation begins is a no-op and |
| | | 52 | | // the pipeline may still start and run indefinitely. |
| | 16 | 53 | | ct.ThrowIfCancellationRequested(); |
| | | 54 | | |
| | 16 | 55 | | using var registration = ct.Register(static state => |
| | 16 | 56 | | { |
| | 3 | 57 | | var shell = (PowerShell)state!; |
| | 6 | 58 | | try { shell.Stop(); } catch { /* ignored */ } |
| | 19 | 59 | | }, ps); |
| | | 60 | | |
| | | 61 | | try |
| | | 62 | | { |
| | 16 | 63 | | var invokeTask = ps.InvokeAsync(); |
| | | 64 | | |
| | | 65 | | // If cancellation is requested during the short window between registration and invocation, |
| | | 66 | | // attempt to stop immediately. |
| | 16 | 67 | | if (ct.IsCancellationRequested) |
| | | 68 | | { |
| | 2 | 69 | | try { ps.Stop(); } catch { /* ignored */ } |
| | | 70 | | } |
| | | 71 | | |
| | 16 | 72 | | var results = await invokeTask.ConfigureAwait(false); |
| | | 73 | | |
| | 13 | 74 | | if (log.IsEnabled(LogEventLevel.Verbose)) |
| | | 75 | | { |
| | 0 | 76 | | log.Verbose("PowerShell script executed with {Count} results.", results.Count); |
| | | 77 | | } |
| | 13 | 78 | | return results; |
| | | 79 | | } |
| | 3 | 80 | | catch (PipelineStoppedException) when (ct.IsCancellationRequested) |
| | | 81 | | { |
| | | 82 | | // Convert PipelineStoppedException to OperationCanceledException when cancellation was requested |
| | 3 | 83 | | throw new OperationCanceledException("PowerShell pipeline was stopped due to cancellation.", ct); |
| | | 84 | | } |
| | 13 | 85 | | } |
| | | 86 | | } |