2016-03-30 14:20:52 -07:00
|
|
|
//
|
|
|
|
|
// Copyright (C) Microsoft. All rights reserved.
|
|
|
|
|
//
|
2016-07-29 13:02:49 -07:00
|
|
|
|
2016-03-30 14:20:52 -07:00
|
|
|
using System;
|
|
|
|
|
using System.Management.Automation;
|
|
|
|
|
|
|
|
|
|
namespace Microsoft.PowerShell.Commands
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Removes an event from the event queue.
|
|
|
|
|
/// </summary>
|
2016-08-23 21:21:31 -04:00
|
|
|
[Cmdlet(VerbsCommon.Remove, "Event", SupportsShouldProcess = true, DefaultParameterSetName = "BySource", HelpUri = "https://go.microsoft.com/fwlink/?LinkID=135247")]
|
2016-03-30 14:20:52 -07:00
|
|
|
public class RemoveEventCommand : PSCmdlet
|
|
|
|
|
{
|
|
|
|
|
#region parameters
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// A source identifier for this event subscription
|
|
|
|
|
/// </summary>
|
2016-07-29 13:02:49 -07:00
|
|
|
[Parameter(Mandatory = true, Position = 0, ParameterSetName = "BySource")]
|
2016-03-30 14:20:52 -07:00
|
|
|
public string SourceIdentifier
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2016-07-29 13:02:49 -07:00
|
|
|
return _sourceIdentifier;
|
2016-03-30 14:20:52 -07:00
|
|
|
}
|
|
|
|
|
set
|
|
|
|
|
{
|
2016-07-29 13:02:49 -07:00
|
|
|
_sourceIdentifier = value;
|
2016-03-30 14:20:52 -07:00
|
|
|
|
2016-07-29 13:02:49 -07:00
|
|
|
if (value != null)
|
2016-03-30 14:20:52 -07:00
|
|
|
{
|
2016-07-29 13:02:49 -07:00
|
|
|
_matchPattern = WildcardPattern.Get(value, WildcardOptions.IgnoreCase);
|
2016-03-30 14:20:52 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-07-29 13:02:49 -07:00
|
|
|
private string _sourceIdentifier = null;
|
2016-03-30 14:20:52 -07:00
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// An identifier for this event subscription
|
|
|
|
|
/// </summary>
|
2016-07-29 13:02:49 -07:00
|
|
|
[Parameter(Mandatory = true, Position = 0, ValueFromPipelineByPropertyName = true, ParameterSetName = "ByIdentifier")]
|
2016-03-30 14:20:52 -07:00
|
|
|
public int EventIdentifier
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2016-07-29 13:02:49 -07:00
|
|
|
return _eventIdentifier;
|
2016-03-30 14:20:52 -07:00
|
|
|
}
|
|
|
|
|
set
|
|
|
|
|
{
|
2016-07-29 13:02:49 -07:00
|
|
|
_eventIdentifier = value;
|
2016-03-30 14:20:52 -07:00
|
|
|
}
|
|
|
|
|
}
|
2016-07-29 13:02:49 -07:00
|
|
|
private int _eventIdentifier = -1;
|
2016-03-30 14:20:52 -07:00
|
|
|
|
|
|
|
|
#endregion parameters
|
|
|
|
|
|
2016-07-29 13:02:49 -07:00
|
|
|
private WildcardPattern _matchPattern;
|
2016-03-30 14:20:52 -07:00
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Remove the event from the queue
|
|
|
|
|
/// </summary>
|
|
|
|
|
protected override void ProcessRecord()
|
|
|
|
|
{
|
|
|
|
|
// Go through all the received events and write them to the output
|
|
|
|
|
// pipeline
|
|
|
|
|
bool foundMatch = false;
|
|
|
|
|
|
2016-07-29 13:02:49 -07:00
|
|
|
lock (Events.ReceivedEvents.SyncRoot)
|
2016-03-30 14:20:52 -07:00
|
|
|
{
|
|
|
|
|
PSEventArgsCollection currentEvents = Events.ReceivedEvents;
|
|
|
|
|
|
2016-07-29 13:02:49 -07:00
|
|
|
for (int eventCounter = currentEvents.Count; eventCounter > 0; eventCounter--)
|
2016-03-30 14:20:52 -07:00
|
|
|
{
|
|
|
|
|
PSEventArgs currentEvent = currentEvents[eventCounter - 1];
|
|
|
|
|
|
|
|
|
|
// If they specified a event identifier and we don't match, continue
|
2016-07-29 13:02:49 -07:00
|
|
|
if ((_sourceIdentifier != null) &&
|
|
|
|
|
(!_matchPattern.IsMatch(currentEvent.SourceIdentifier)))
|
2016-03-30 14:20:52 -07:00
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// If they specified a TimeGenerated and we don't match, continue
|
2016-07-29 13:02:49 -07:00
|
|
|
if ((_eventIdentifier >= 0) &&
|
|
|
|
|
(currentEvent.EventIdentifier != _eventIdentifier))
|
2016-03-30 14:20:52 -07:00
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foundMatch = true;
|
2016-07-29 13:02:49 -07:00
|
|
|
if (ShouldProcess(
|
2016-03-30 14:20:52 -07:00
|
|
|
String.Format(
|
2016-07-29 13:02:49 -07:00
|
|
|
System.Globalization.CultureInfo.CurrentCulture,
|
2016-03-30 14:20:52 -07:00
|
|
|
EventingStrings.EventResource,
|
|
|
|
|
currentEvent.SourceIdentifier),
|
|
|
|
|
EventingStrings.Remove))
|
|
|
|
|
{
|
|
|
|
|
currentEvents.RemoveAt(eventCounter - 1);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Generate an error if we couldn't find the subscription identifier,
|
|
|
|
|
// and no globbing was done.
|
2016-07-29 13:02:49 -07:00
|
|
|
if ((_sourceIdentifier != null) &&
|
|
|
|
|
(!WildcardPattern.ContainsWildcardCharacters(_sourceIdentifier)) &&
|
|
|
|
|
(!foundMatch))
|
2016-03-30 14:20:52 -07:00
|
|
|
{
|
|
|
|
|
ErrorRecord errorRecord = new ErrorRecord(
|
|
|
|
|
new ArgumentException(
|
|
|
|
|
String.Format(
|
|
|
|
|
System.Globalization.CultureInfo.CurrentCulture,
|
2016-07-29 13:02:49 -07:00
|
|
|
EventingStrings.SourceIdentifierNotFound, _sourceIdentifier)),
|
2016-03-30 14:20:52 -07:00
|
|
|
"INVALID_SOURCE_IDENTIFIER",
|
|
|
|
|
ErrorCategory.InvalidArgument,
|
|
|
|
|
null);
|
|
|
|
|
|
|
|
|
|
WriteError(errorRecord);
|
|
|
|
|
}
|
2016-07-29 13:02:49 -07:00
|
|
|
else if ((_eventIdentifier >= 0) && (!foundMatch))
|
2016-03-30 14:20:52 -07:00
|
|
|
{
|
|
|
|
|
ErrorRecord errorRecord = new ErrorRecord(
|
|
|
|
|
new ArgumentException(
|
|
|
|
|
String.Format(
|
|
|
|
|
System.Globalization.CultureInfo.CurrentCulture,
|
2016-07-29 13:02:49 -07:00
|
|
|
EventingStrings.EventIdentifierNotFound, _eventIdentifier)),
|
2016-03-30 14:20:52 -07:00
|
|
|
"INVALID_EVENT_IDENTIFIER",
|
|
|
|
|
ErrorCategory.InvalidArgument,
|
|
|
|
|
null);
|
|
|
|
|
|
|
|
|
|
WriteError(errorRecord);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|