| | 1 | | using System.Management.Automation; |
| | 2 | | using System.Management.Automation.Language; |
| | 3 | | using Kestrun.Logging.Enrichers.Extensions; |
| | 4 | |
|
| | 5 | | namespace Kestrun.Logging.Data; |
| | 6 | |
|
| | 7 | | /// <summary> |
| | 8 | | /// Wraps the PowerShell InvocationInfo object and exposes its properties for logging purposes. |
| | 9 | | /// </summary> |
| | 10 | | /// <remarks> |
| | 11 | | /// Initializes a new instance of the <see cref="InvocationInfoWrapper"/> class with the specified <see cref="Invocation |
| | 12 | | /// </remarks> |
| | 13 | | /// <param name="invocationInfo">The PowerShell <see cref="InvocationInfo"/> object to wrap.</param> |
| 4 | 14 | | public class InvocationInfoWrapper(InvocationInfo invocationInfo) |
| | 15 | | { |
| | 16 | | /// <summary> |
| | 17 | | /// Gets the dictionary of bound parameters for the PowerShell invocation. |
| | 18 | | /// </summary> |
| 8 | 19 | | public Dictionary<string, object> BoundParameters { get; } = invocationInfo.BoundParameters; |
| | 20 | | /// <summary> |
| | 21 | | /// Gets the origin of the command (e.g., Runspace, Internal, etc.). |
| | 22 | | /// </summary> |
| 7 | 23 | | public CommandOrigin CommandOrigin { get; } = invocationInfo.CommandOrigin; |
| | 24 | | /// <summary> |
| | 25 | | /// Gets the script extent that displays the position of the command in the script. |
| | 26 | | /// </summary> |
| 7 | 27 | | public IScriptExtent DisplayScriptPosition { get; } = invocationInfo.DisplayScriptPosition; |
| | 28 | | /// <summary> |
| | 29 | | /// Gets a value indicating whether the command is expecting input. |
| | 30 | | /// </summary> |
| 7 | 31 | | public bool ExpectingInput { get; } = invocationInfo.ExpectingInput; |
| | 32 | | /// <summary> |
| | 33 | | /// Gets the history ID of the PowerShell invocation. |
| | 34 | | /// </summary> |
| 7 | 35 | | public long HistoryId { get; } = invocationInfo.HistoryId; |
| | 36 | | /// <summary> |
| | 37 | | /// Gets the name of the command being invoked. |
| | 38 | | /// </summary> |
| 8 | 39 | | public string InvocationName { get; } = invocationInfo.InvocationName; |
| | 40 | | /// <summary> |
| | 41 | | /// Gets the line of the script where the command is invoked. |
| | 42 | | /// </summary> |
| 8 | 43 | | public string Line { get; } = invocationInfo.Line; |
| | 44 | | /// <summary> |
| | 45 | | /// Gets the string representation of the command being invoked. |
| | 46 | | /// </summary> |
| 7 | 47 | | public string? MyCommand { get; } = invocationInfo.MyCommand?.ToString(); |
| | 48 | | /// <summary> |
| | 49 | | /// Gets the offset in the line where the command is invoked. |
| | 50 | | /// </summary> |
| 7 | 51 | | public int OffsetInLine { get; } = invocationInfo.OffsetInLine; |
| | 52 | | /// <summary> |
| | 53 | | /// Gets the length of the pipeline for the PowerShell invocation. |
| | 54 | | /// </summary> |
| 7 | 55 | | public int PipelineLength { get; } = invocationInfo.PipelineLength; |
| | 56 | | /// <summary> |
| | 57 | | /// Gets the position of the command in the pipeline for the PowerShell invocation. |
| | 58 | | /// </summary> |
| 7 | 59 | | public int PipelinePosition { get; } = invocationInfo.PipelinePosition; |
| | 60 | | /// <summary> |
| | 61 | | /// Gets the position message for the PowerShell invocation. |
| | 62 | | /// </summary> |
| 7 | 63 | | public string PositionMessage { get; } = invocationInfo.PositionMessage; |
| | 64 | | /// <summary> |
| | 65 | | /// Gets the path of the PowerShell command being executed. |
| | 66 | | /// </summary> |
| 7 | 67 | | public string PSCommandPath { get; } = invocationInfo.PSCommandPath; |
| | 68 | | /// <summary> |
| | 69 | | /// Gets the script root path of the PowerShell script being executed. |
| | 70 | | /// </summary> |
| 7 | 71 | | public string PSScriptRoot { get; } = invocationInfo.PSScriptRoot; |
| | 72 | | /// <summary> |
| | 73 | | /// Gets the line number in the script where the command is invoked. |
| | 74 | | /// </summary> |
| 8 | 75 | | public int ScriptLineNumber { get; } = invocationInfo.ScriptLineNumber; |
| | 76 | | /// <summary> |
| | 77 | | /// Gets the name of the script where the command is invoked. |
| | 78 | | /// </summary> |
| 7 | 79 | | public string ScriptName { get; } = invocationInfo.ScriptName; |
| | 80 | | /// <summary> |
| | 81 | | /// Gets the list of arguments that were not bound to parameters during the PowerShell invocation. |
| | 82 | | /// </summary> |
| 8 | 83 | | public List<object> UnboundArguments { get; } = invocationInfo.UnboundArguments; |
| | 84 | |
|
| | 85 | | /// <summary> |
| | 86 | | /// Returns a string representation of the <see cref="InvocationInfoWrapper"/> object. |
| | 87 | | /// </summary> |
| | 88 | | /// <returns>A string representation of the current object.</returns> |
| 1 | 89 | | public override string ToString() => this.ToTable(); |
| | 90 | | } |