diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/GetEventCommand.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/GetEventCommand.cs index f4ab18ac75ab..9ef9c4a1a7ac 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/GetEventCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/GetEventCommand.cs @@ -10,7 +10,7 @@ namespace Microsoft.PowerShell.Commands /// /// Gets events from the event queue. /// - [Cmdlet(VerbsCommon.Get, "Event", DefaultParameterSetName = "BySource", HelpUri = "https://go.microsoft.com/fwlink/?LinkID=2097014")] + [Cmdlet(VerbsCommon.Get, "Event", DefaultParameterSetName = "BySource", HelpUri = "https://go.microsoft.com/fwlink/?LinkID=2097014")] [OutputType(typeof(PSEventArgs))] public class GetEventCommand : PSCmdlet { @@ -59,6 +59,24 @@ public int EventIdentifier } } + /// + /// If set, will return results in descending order (most recent event first) + /// + [Parameter(ValueFromPipelineByPropertyName = true)] + public SwitchParameter Descending { get; set; } + + /// + /// If provided, will only return the first N results. + /// + [Parameter(ValueFromPipelineByPropertyName = true)] + public long First { get; set; } = 0; + + /// + /// If provided, will skip the first N results. + /// + [Parameter(ValueFromPipelineByPropertyName = true)] + public long Skip { get; set; } = 0; + private int _eventId = -1; #endregion parameters @@ -69,15 +87,19 @@ public int EventIdentifier /// Get the requested events. /// protected override void EndProcessing() - { + { bool foundMatch = false; - + long outputCount = 0; + long skipCount = 0; // Go through all the received events and write them to the output // pipeline List eventArgsCollection; lock (Events.ReceivedEvents.SyncRoot) { eventArgsCollection = new List(Events.ReceivedEvents); + if (this.Descending) { + eventArgsCollection.Reverse(); + } } foreach (PSEventArgs eventArg in eventArgsCollection) @@ -96,6 +118,17 @@ protected override void EndProcessing() continue; } + if (Skip > 0 && skipCount < Skip) { + skipCount++; + continue; + } + + if (First > 0 && outputCount >= First) { + break; + } + + outputCount++; + WriteObject(eventArg); foundMatch = true; }