Framework/Listeners/UserReports/WritePsConsole.ps1
Set-StrictMode -Version Latest class WritePsConsole: FileOutputBase { hidden static [WritePsConsole] $Instance = $null; static [WritePsConsole] GetInstance() { if ($null -eq [WritePsConsole]::Instance) { [WritePsConsole]::Instance = [WritePsConsole]::new(); } return [WritePsConsole]::Instance } [void] RegisterEvents() { $this.UnregisterEvents(); # Mandatory: Generate Run Identifier Event $this.RegisterEvent([AzSdkRootEvent]::GenerateRunIdentifier, { $currentInstance = [WritePsConsole]::GetInstance(); try { $currentInstance.SetRunIdentifier([AzSdkRootEventArgument] ($Event.SourceArgs | Select-Object -First 1)); } catch { $currentInstance.PublishException($_); } }); $this.RegisterEvent([AzSdkGenericEvent]::CustomMessage, { $currentInstance = [WritePsConsole]::GetInstance(); try { if($Event.SourceArgs) { $messages = @(); $messages += $Event.SourceArgs; $messages | ForEach-Object { $currentInstance.WriteMessageData($_); } } } catch { $currentInstance.PublishException($_); } }); $this.RegisterEvent([AzSdkGenericEvent]::Exception, { $currentInstance = [WritePsConsole]::GetInstance(); try { $currentInstance.WriteMessage(($Event.SourceArgs | Select-Object -First 1), [MessageType]::Error); } catch { #Consuming the exception intentionally to prevent infinite loop of errors #$currentInstance.PublishException($_); } }); $this.RegisterEvent([AzSdkRootEvent]::CustomMessage, { $currentInstance = [WritePsConsole]::GetInstance(); try { if($Event.SourceArgs -and $Event.SourceArgs.Messages) { $Event.SourceArgs.Messages | ForEach-Object { $currentInstance.WriteMessageData($_); } } } catch { $currentInstance.PublishException($_); } }); $this.RegisterEvent([AzSdkRootEvent]::CommandStarted, { $currentInstance = [WritePsConsole]::GetInstance(); try { $currentInstance.CommandStartedAction($Event); } catch { $currentInstance.PublishException($_); } }); $this.RegisterEvent([AzSdkRootEvent]::CommandError, { $currentInstance = [WritePsConsole]::GetInstance(); try { $currentInstance.WriteMessage($Event.SourceArgs.ExceptionMessage, [MessageType]::Error); } catch { $currentInstance.PublishException($_); } }); $this.RegisterEvent([AzSdkRootEvent]::CommandCompleted, { $currentInstance = [WritePsConsole]::GetInstance(); try { $currentInstance.WriteMessage([Constants]::DoubleDashLine, [MessageType]::Info) $currentInstance.WriteMessage("Info: Logs has been exported to path - $([WriteFolderPath]::GetInstance().FolderPath)", [MessageType]::Info) $currentInstance.WriteMessage([Constants]::DoubleDashLine, [MessageType]::Info) $currentInstance.FilePath = ""; } catch { $currentInstance.PublishException($_); } }); # SVT events $this.RegisterEvent([SVTEvent]::CommandStarted, { $currentInstance = [WritePsConsole]::GetInstance(); try { $currentInstance.CommandStartedAction($Event); } catch { $currentInstance.PublishException($_); } }); $this.RegisterEvent([SVTEvent]::CommandError, { $currentInstance = [WritePsConsole]::GetInstance(); try { $currentInstance.WriteMessage($Event.SourceArgs.ExceptionMessage, [MessageType]::Error); } catch { $currentInstance.PublishException($_); } }); $this.RegisterEvent([SVTEvent]::CommandCompleted, { $currentInstance = [WritePsConsole]::GetInstance(); try { $currentInstance.WriteMessage([Constants]::DoubleDashLine, [MessageType]::Info) if(($Event.SourceArgs | Measure-Object).Count -gt 0) { $currentInstance.WriteMessage([Constants]::RemediationMsg, [MessageType]::Info) $currentInstance.WriteMessage([Constants]::SingleDashLine, [MessageType]::Info) } $currentInstance.WriteMessage("Info: Status and detailed logs has been exported to path - $([WriteFolderPath]::GetInstance().FolderPath)", [MessageType]::Info) $currentInstance.WriteMessage([Constants]::DoubleDashLine, [MessageType]::Info) $currentInstance.FilePath = ""; } catch { $currentInstance.PublishException($_); } }); $this.RegisterEvent([SVTEvent]::EvaluationStarted, { $currentInstance = [WritePsConsole]::GetInstance(); try { if(-not [string]::IsNullOrWhiteSpace($Event.SourceArgs.ResourceContext.ResourceGroupName) -and -not [string]::IsNullOrWhiteSpace($Event.SourceArgs.ResourceContext.ResourceName)) { $startHeading = ([Constants]::ModuleStartHeading -f $Event.SourceArgs.ResourceContext.FeatureName, $Event.SourceArgs.ResourceContext.ResourceGroupName, $Event.SourceArgs.ResourceContext.ResourceName); } else { $startHeading = ([Constants]::ModuleStartHeadingSub -f $Event.SourceArgs.ResourceContext.FeatureName, $Event.SourceArgs.ResourceContext.SubscriptionName, $Event.SourceArgs.ResourceContext.SubscriptionId); } $currentInstance.WriteMessage($startHeading, [MessageType]::Info); } catch { $currentInstance.PublishException($_); } }); $this.RegisterEvent([SVTEvent]::EvaluationCompleted, { $currentInstance = [WritePsConsole]::GetInstance(); try { $props = $Event.SourceArgs[0]; if(-not [string]::IsNullOrWhiteSpace($props.ResourceContext.ResourceGroupName) -and -not [string]::IsNullOrWhiteSpace($props.ResourceContext.ResourceName)) { $currentInstance.WriteMessage(([Constants]::CompletedAnalysis -f $props.ResourceContext.FeatureName, $props.ResourceContext.ResourceGroupName, $props.ResourceContext.ResourceName), [MessageType]::Update); } else { $currentInstance.WriteMessage(([Constants]::CompletedAnalysisSub -f $props.ResourceContext.FeatureName, $props.ResourceContext.SubscriptionName, $props.ResourceContext.SubscriptionId), [MessageType]::Update); } } catch { $currentInstance.PublishException($_); } }); $this.RegisterEvent([SVTEvent]::EvaluationError, { $currentInstance = [WritePsConsole]::GetInstance(); try { $currentInstance.WriteMessage($Event.SourceArgs.ExceptionMessage, [MessageType]::Error); } catch { $currentInstance.PublishException($_); } }); $this.RegisterEvent([SVTEvent]::ControlStarted, { $currentInstance = [WritePsConsole]::GetInstance(); try { if(-not [string]::IsNullOrWhiteSpace($Event.SourceArgs.ResourceContext.ResourceName)) { $AnalysingControlHeadingMsg =([Constants]::AnalysingControlHeading -f $Event.SourceArgs.ResourceContext.FeatureName,$Event.SourceArgs.ControlItem.Description,$Event.SourceArgs.ResourceContext.ResourceName) } else { $AnalysingControlHeadingMsg =([Constants]::AnalysingControlHeadingSub -f $Event.SourceArgs.ResourceContext.FeatureName,$Event.SourceArgs.ControlItem.Description,$Event.SourceArgs.ResourceContext.SubscriptionName) } $currentInstance.WriteMessage($AnalysingControlHeadingMsg, [MessageType]::Info) } catch { $currentInstance.PublishException($_); } }); $this.RegisterEvent([SVTEvent]::ControlDisabled, { $currentInstance = [WritePsConsole]::GetInstance(); try { $currentInstance.WriteMessage(("**Disabled**: [{0}]-[{1}]" -f $Event.SourceArgs.ResourceContext.FeatureName, $Event.SourceArgs.ControlItem.Description), [MessageType]::Warning); } catch { $currentInstance.PublishException($_); } }); } #Write message on powershell console with appropriate color [void] WriteMessage([PSObject] $message, [MessageType] $messageType) { if(-not $message) { return; } $colorCode = [System.ConsoleColor]::White switch($messageType) { ([MessageType]::Critical) { $colorCode = [System.ConsoleColor]::Red } ([MessageType]::Error) { $colorCode = [System.ConsoleColor]::Red } ([MessageType]::Warning) { $colorCode = [System.ConsoleColor]::Yellow } ([MessageType]::Info) { $colorCode = [System.ConsoleColor]::Cyan } ([MessageType]::Update) { $colorCode = [System.ConsoleColor]::Green } ([MessageType]::Deprecated) { $colorCode = [System.ConsoleColor]::DarkYellow } } # FilePath check ensures to print detailed error objects on PS host $formattedMessage = [Helpers]::ConvertObjectToString($message, (-not [string]::IsNullOrEmpty($this.FilePath))); Write-Host $formattedMessage -ForegroundColor $colorCode #if($message.GetType().FullName -eq "System.Management.Automation.ErrorRecord") #{ $this.AddOutputLog([Helpers]::ConvertObjectToString($message, $false)); #} #else #{ # $this.AddOutputLog($message); #} } hidden [void] WriteMessage([PSObject] $message) { $this.WriteMessage($message, [MessageType]::Info); } hidden [void] WriteMessageData([MessageData] $messageData) { if($messageData) { $this.WriteMessage(("`r`n" + $messageData.Message), $messageData.MessageType); if($messageData.DataObject) { if (-not [string]::IsNullOrEmpty($messageData.Message)) { $this.WriteMessage("`r`n"); } $this.WriteMessage($messageData.DataObject, $messageData.MessageType); } } } hidden [void] AddOutputLog([string] $message, [bool] $includeTimeStamp) { if([string]::IsNullOrEmpty($message) -or [string]::IsNullOrEmpty($this.FilePath)) { return; } if($includeTimeStamp) { $message = (Get-Date -format "MM\/dd\/yyyy HH:mm:ss") + "-" + $message } Add-Content -Value $message -Path $this.FilePath } hidden [void] AddOutputLog([string] $message) { $this.AddOutputLog($message, $false); } hidden [void] CommandStartedAction($event) { $arg = $event.SourceArgs | Select-Object -First 1; if([Helpers]::CheckMember($arg, "ResourceContext")) { $this.SetFilePath($arg.ResourceContext, [FileOutputBase]::ETCFolderPath, "PowerShellOutput.log"); } else { $this.SetFilePath($arg.Context, [FileOutputBase]::ETCFolderPath, "PowerShellOutput.log"); } $currentVersion = $this.GetCurrentModuleVersion(); $moduleName = $this.GetModuleName(); $this.WriteMessage([Constants]::DoubleDashLine + "`r`n$moduleName Version: $currentVersion `r`n" + [Constants]::DoubleDashLine , [MessageType]::Info); # Version check message if($arg.Messages) { $arg.Messages | ForEach-Object { $this.WriteMessageData($_); } } $this.WriteMessage("Method Name: $($this.InvocationContext.MyCommand.Name) `r`nInput Parameters: $(($this.InvocationContext.BoundParameters | Out-String).TrimEnd()) `r`n" + [Constants]::DoubleDashLine , [MessageType]::Info); if(![ConfigurationManager]::GetAzSdkSettings().UseOnlinePolicyStore) { $this.WriteMessage([Constants]::OfflineScreenWarning , [MessageType]::Warning); } else { $this.WriteMessage([ConfigurationManager]::GetAzSdkConfigData().PolicyMessage,[MessageType]::Warning) } } } |