Framework/Abstracts/EventBase.ps1
using namespace System.Management.Automation Set-StrictMode -Version Latest # Class for providing capability to fire events, # also includes support to fire AzSdkGenericEvent and holds InvocationContext class EventBase { [string] $RunIdentifier = "default"; [InvocationInfo] $InvocationContext; [string] GenerateRunIdentifier() { return $(Get-Date -format "yyyyMMdd_HHmmss"); } hidden [void] PublishEvent([string] $eventType, [PSObject] $eventArgument) { New-Event -SourceIdentifier $eventType ` -Sender $this ` -EventArguments $eventArgument | Out-Null } [void] PublishException([ErrorRecord] $eventArgument) { $this.PublishEvent([AzSdkGenericEvent]::Exception, $eventArgument); } [void] PublishCustomMessage([MessageData[]] $messageData) { if($messageData) { $this.PublishEvent([AzSdkGenericEvent]::CustomMessage, $messageData); } } [void] PublishCustomMessage([string] $message, [MessageType] $messageType) { $this.PublishCustomMessage([MessageData]::new($message, $messageType)); } [void] PublishCustomMessage([string] $message, [PSObject] $dataObject) { $this.PublishCustomMessage([MessageData]::new($message, $dataObject)); } [void] PublishCustomMessage([string] $message) { $this.PublishCustomMessage($message, [MessageType]::Info); } [string] GetModuleName() { if($this.InvocationContext) { return $this.InvocationContext.MyCommand.ModuleName; } throw [System.ArgumentException] "The parameter 'InvocationContext' is not set" } [System.Version] GetCurrentModuleVersion() { if($this.InvocationContext) { return [System.Version] ($this.InvocationContext.MyCommand.Version); } # Return default version which is 0.0. return [System.Version]::new(); } # Static Methods static [void] PublishGenericException([ErrorRecord] $eventArgument) { [EventBase]::new().PublishException($eventArgument); } static [void] PublishGenericCustomMessage([string] $message) { [EventBase]::PublishGenericCustomMessage($message, [MessageType]::Info); } static [void] PublishGenericCustomMessage([string] $message, [MessageType] $messageType) { [EventBase]::new().PublishCustomMessage($message, $messageType); } } |