mirror of
https://github.com/aphrody-labs/PowerShell.git
synced 2026-10-18 05:05:36 +00:00
220 lines
6.6 KiB
C#
220 lines
6.6 KiB
C#
//
|
|||
|
|
// Copyright (C) Microsoft. All rights reserved.
|
||
|
|
//
|
||
|
|
using System;
|
||
|
|
using System.Management.Automation;
|
||
|
|
|
||
|
|
namespace Microsoft.PowerShell.Commands
|
||
|
|
{
|
||
|
|
/// <summary>
|
||
|
|
/// Base class for object-based event registration.
|
||
|
|
/// </summary>
|
||
|
|
public abstract class ObjectEventRegistrationBase : PSCmdlet
|
||
|
|
{
|
||
|
|
#region parameters
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// Parameter for an identifier for this event subscription
|
||
|
|
/// </summary>
|
||
|
|
[Parameter(Position = 100)]
|
||
|
|
public string SourceIdentifier
|
||
|
|
{
|
||
|
|
get
|
||
|
|
{
|
||
|
|
return sourceIdentifier;
|
||
|
|
}
|
||
|
|
set
|
||
|
|
{
|
||
|
|
sourceIdentifier = value;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
private string sourceIdentifier = Guid.NewGuid().ToString();
|
||
|
|
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// Parameter for any action to be invoked when the event arrives
|
||
|
|
/// </summary>
|
||
|
|
[Parameter(Position = 101)]
|
||
|
|
public ScriptBlock Action
|
||
|
|
{
|
||
|
|
get
|
||
|
|
{
|
||
|
|
return action;
|
||
|
|
}
|
||
|
|
set
|
||
|
|
{
|
||
|
|
action = value;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
private ScriptBlock action = null;
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// Parameter for additional data to be associated with this event subscription
|
||
|
|
/// </summary>
|
||
|
|
[Parameter]
|
||
|
|
public PSObject MessageData
|
||
|
|
{
|
||
|
|
get
|
||
|
|
{
|
||
|
|
return messageData;
|
||
|
|
}
|
||
|
|
set
|
||
|
|
{
|
||
|
|
messageData = value;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
private PSObject messageData = null;
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// Parameter for the flag that determines if this subscription is used to support
|
||
|
|
/// other subscriptions
|
||
|
|
/// </summary>
|
||
|
|
[Parameter]
|
||
|
|
public SwitchParameter SupportEvent
|
||
|
|
{
|
||
|
|
get
|
||
|
|
{
|
||
|
|
return supportEvent;
|
||
|
|
}
|
||
|
|
set
|
||
|
|
{
|
||
|
|
supportEvent = value;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
private SwitchParameter supportEvent = new SwitchParameter();
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// Parameter for the flag that determines whether this
|
||
|
|
/// subscription will forward its events to the PowerShell client during remote executions
|
||
|
|
/// </summary>
|
||
|
|
[Parameter]
|
||
|
|
public SwitchParameter Forward
|
||
|
|
{
|
||
|
|
get
|
||
|
|
{
|
||
|
|
return forward;
|
||
|
|
}
|
||
|
|
set
|
||
|
|
{
|
||
|
|
forward = value;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
private SwitchParameter forward = new SwitchParameter();
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// Parameter to indicate that the subscriber should be auto-unregistered after being triggered for specified times.
|
||
|
|
/// If the value is equal or less than zero, there is no limit on the number of times the event can be triggered without being unregistered
|
||
|
|
/// </summary>
|
||
|
|
[Parameter]
|
||
|
|
public int MaxTriggerCount
|
||
|
|
{
|
||
|
|
get
|
||
|
|
{
|
||
|
|
return _maxTriggerCount;
|
||
|
|
}
|
||
|
|
set
|
||
|
|
{
|
||
|
|
_maxTriggerCount = value <= 0 ? 0 : value;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
private int _maxTriggerCount = 0;
|
||
|
|
|
||
|
|
#endregion parameters
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// Returns the object that generates events to be monitored
|
||
|
|
/// </summary>
|
||
|
|
protected abstract Object GetSourceObject();
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// Returns the event name to be monitored on the input object
|
||
|
|
/// </summary>
|
||
|
|
protected abstract String GetSourceObjectEventName();
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// Gets the subscriber generated by this command
|
||
|
|
/// </summary>
|
||
|
|
protected PSEventSubscriber NewSubscriber
|
||
|
|
{
|
||
|
|
get { return newSubscriber; }
|
||
|
|
}
|
||
|
|
private PSEventSubscriber newSubscriber;
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// Check arguments
|
||
|
|
/// </summary>
|
||
|
|
protected override void BeginProcessing()
|
||
|
|
{
|
||
|
|
if (((bool)forward) && (action != null))
|
||
|
|
{
|
||
|
|
ThrowTerminatingError(
|
||
|
|
new ErrorRecord(
|
||
|
|
new ArgumentException(EventingResources.ActionAndForwardNotSupported),
|
||
|
|
"ACTION_AND_FORWARD_NOT_SUPPORTED",
|
||
|
|
ErrorCategory.InvalidOperation,
|
||
|
|
null));
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// Subscribe to the event on the object
|
||
|
|
/// </summary>
|
||
|
|
protected override void EndProcessing()
|
||
|
|
{
|
||
|
|
Object inputObject = PSObject.Base(GetSourceObject());
|
||
|
|
string eventName = GetSourceObjectEventName();
|
||
|
|
|
||
|
|
try
|
||
|
|
{
|
||
|
|
if (
|
||
|
|
((inputObject != null) || (eventName != null)) &&
|
||
|
|
(Events.GetEventSubscribers(sourceIdentifier).GetEnumerator().MoveNext())
|
||
|
|
)
|
||
|
|
{
|
||
|
|
// Detect if the event identifier already exists
|
||
|
|
ErrorRecord errorRecord = new ErrorRecord(
|
||
|
|
new ArgumentException(
|
||
|
|
String.Format(
|
||
|
|
System.Globalization.CultureInfo.CurrentCulture,
|
||
|
|
EventingResources.SubscriberExists, sourceIdentifier)),
|
||
|
|
"SUBSCRIBER_EXISTS",
|
||
|
|
ErrorCategory.InvalidArgument,
|
||
|
|
inputObject);
|
||
|
|
|
||
|
|
WriteError(errorRecord);
|
||
|
|
}
|
||
|
|
else
|
||
|
|
{
|
||
|
|
newSubscriber =
|
||
|
|
Events.SubscribeEvent(
|
||
|
|
inputObject,
|
||
|
|
eventName,
|
||
|
|
sourceIdentifier, messageData, action, (bool) supportEvent, (bool) forward, _maxTriggerCount);
|
||
|
|
|
||
|
|
if((action != null) && (! (bool) supportEvent))
|
||
|
|
WriteObject(newSubscriber.Action);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
catch(ArgumentException e)
|
||
|
|
{
|
||
|
|
ErrorRecord errorRecord = new ErrorRecord(
|
||
|
|
e,
|
||
|
|
"INVALID_REGISTRATION",
|
||
|
|
ErrorCategory.InvalidArgument,
|
||
|
|
inputObject);
|
||
|
|
|
||
|
|
WriteError(errorRecord);
|
||
|
|
}
|
||
|
|
catch (InvalidOperationException e)
|
||
|
|
{
|
||
|
|
ErrorRecord errorRecord = new ErrorRecord(
|
||
|
|
e,
|
||
|
|
"INVALID_REGISTRATION",
|
||
|
|
ErrorCategory.InvalidOperation,
|
||
|
|
inputObject);
|
||
|
|
|
||
|
|
WriteError(errorRecord);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|