Skip to content

Commit

Permalink
Get-Event - Supporting -First/-Skip/-Descending (Fixes PowerShell#20444)
Browse files Browse the repository at this point in the history
  • Loading branch information
James Brundage committed Oct 5, 2023
1 parent 109c242 commit 2fb35e1
Showing 1 changed file with 36 additions and 3 deletions.
Expand Up @@ -10,7 +10,7 @@ namespace Microsoft.PowerShell.Commands
/// <summary>
/// Gets events from the event queue.
/// </summary>
[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
{
Expand Down Expand Up @@ -59,6 +59,24 @@ public int EventIdentifier
}
}

/// <summary>
/// If set, will return results in descending order (most recent event first)
/// </summary>
[Parameter(ValueFromPipelineByPropertyName = true)]
public SwitchParameter Descending { get; set; }

/// <summary>
/// If provided, will only return the first N results.
/// </summary>
[Parameter(ValueFromPipelineByPropertyName = true)]
public long First { get; set; } = 0;

/// <summary>
/// If provided, will skip the first N results.
/// </summary>
[Parameter(ValueFromPipelineByPropertyName = true)]
public long Skip { get; set; } = 0;

private int _eventId = -1;

#endregion parameters
Expand All @@ -69,15 +87,19 @@ public int EventIdentifier
/// Get the requested events.
/// </summary>
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<PSEventArgs> eventArgsCollection;
lock (Events.ReceivedEvents.SyncRoot)
{
eventArgsCollection = new List<PSEventArgs>(Events.ReceivedEvents);
if (this.Descending) {
eventArgsCollection.Reverse();
}
}

foreach (PSEventArgs eventArg in eventArgsCollection)
Expand All @@ -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;
}
Expand Down

0 comments on commit 2fb35e1

Please sign in to comment.