MegaBites-AI/Windows-powershell
0308
1// Copyright (c) Microsoft Corporation.2// Licensed under the MIT License.3 4using System;5using System.Management.Automation;6 7namespace Microsoft.PowerShell.Commands8{9 /// <summary>10 /// Base class for object-based event registration.11 /// </summary>12 public abstract class ObjectEventRegistrationBase : PSCmdlet13 {14 #region parameters15 16 /// <summary>17 /// Parameter for an identifier for this event subscription.18 /// </summary>19 [Parameter(Position = 100)]20 public string SourceIdentifier21 {22 get23 {24 return _sourceIdentifier;25 }26 27 set28 {29 _sourceIdentifier = value;30 }31 }32 33 private string _sourceIdentifier = Guid.NewGuid().ToString();34 35 /// <summary>36 /// Parameter for any action to be invoked when the event arrives.37 /// </summary>38 [Parameter(Position = 101)]39 public ScriptBlock Action40 {41 get42 {43 return _action;44 }45 46 set47 {48 _action = value;49 }50 }51 52 private ScriptBlock _action = null;53 54 /// <summary>55 /// Parameter for additional data to be associated with this event subscription.56 /// </summary>57 [Parameter]58 public PSObject MessageData59 {60 get61 {62 return _messageData;63 }64 65 set66 {67 _messageData = value;68 }69 }70 71 private PSObject _messageData = null;72 73 /// <summary>74 /// Parameter for the flag that determines if this subscription is used to support75 /// other subscriptions.76 /// </summary>77 [Parameter]78 public SwitchParameter SupportEvent79 {80 get81 {82 return _supportEvent;83 }84 85 set86 {87 _supportEvent = value;88 }89 }90 91 private SwitchParameter _supportEvent = new SwitchParameter();92 93 /// <summary>94 /// Parameter for the flag that determines whether this95 /// subscription will forward its events to the PowerShell client during remote executions.96 /// </summary>97 [Parameter]98 public SwitchParameter Forward99 {100 get101 {102 return _forward;103 }104 105 set106 {107 _forward = value;108 }109 }110 111 private SwitchParameter _forward = new SwitchParameter();112 113 /// <summary>114 /// Parameter to indicate that the subscriber should be auto-unregistered after being triggered for specified times.115 /// 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.116 /// </summary>117 [Parameter]118 public int MaxTriggerCount119 {120 get121 {122 return _maxTriggerCount;123 }124 125 set126 {127 _maxTriggerCount = value <= 0 ? 0 : value;128 }129 }130 131 private int _maxTriggerCount = 0;132 133 #endregion parameters134 135 /// <summary>136 /// Returns the object that generates events to be monitored.137 /// </summary>138 protected abstract object GetSourceObject();139 140 /// <summary>141 /// Returns the event name to be monitored on the input object.142 /// </summary>143 protected abstract string GetSourceObjectEventName();144 145 /// <summary>146 /// Gets the subscriber generated by this command.147 /// </summary>148 protected PSEventSubscriber NewSubscriber149 {150 get { return _newSubscriber; }151 }152 153 private PSEventSubscriber _newSubscriber;154 155 /// <summary>156 /// Check arguments.157 /// </summary>158 protected override void BeginProcessing()159 {160 if (((bool)_forward) && (_action != null))161 {162 ThrowTerminatingError(163 new ErrorRecord(164 new ArgumentException(EventingResources.ActionAndForwardNotSupported),165 "ACTION_AND_FORWARD_NOT_SUPPORTED",166 ErrorCategory.InvalidOperation,167 null));168 }169 }170 171 /// <summary>172 /// Subscribe to the event on the object.173 /// </summary>174 protected override void EndProcessing()175 {176 object inputObject = PSObject.Base(GetSourceObject());177 string eventName = GetSourceObjectEventName();178 179 try180 {181 if (182 ((inputObject != null) || (eventName != null)) &&183 (Events.GetEventSubscribers(_sourceIdentifier).GetEnumerator().MoveNext())184 )185 {186 // Detect if the event identifier already exists187 ErrorRecord errorRecord = new ErrorRecord(188 new ArgumentException(189 string.Format(190 System.Globalization.CultureInfo.CurrentCulture,191 EventingResources.SubscriberExists, _sourceIdentifier)),192 "SUBSCRIBER_EXISTS",193 ErrorCategory.InvalidArgument,194 inputObject);195 196 WriteError(errorRecord);197 }198 else199 {200 _newSubscriber =201 Events.SubscribeEvent(202 inputObject,203 eventName,204 _sourceIdentifier, _messageData, _action, (bool)_supportEvent, (bool)_forward, _maxTriggerCount);205 206 if ((_action != null) && (!(bool)_supportEvent))207 WriteObject(_newSubscriber.Action);208 }209 }210 catch (ArgumentException e)211 {212 ErrorRecord errorRecord = new ErrorRecord(213 e,214 "INVALID_REGISTRATION",215 ErrorCategory.InvalidArgument,216 inputObject);217 218 WriteError(errorRecord);219 }220 catch (InvalidOperationException e)221 {222 ErrorRecord errorRecord = new ErrorRecord(223 e,224 "INVALID_REGISTRATION",225 ErrorCategory.InvalidOperation,226 inputObject);227 228 WriteError(errorRecord);229 }230 }231 }232}233 