Framework/Listeners/UserReports/WriteDataFile.ps1
Set-StrictMode -Version Latest class WriteDataFile: FileOutputBase { hidden static [WriteDataFile] $Instance = $null; hidden [int] $JsonDepth = 10; static [WriteDataFile] GetInstance() { if ( $null -eq [WriteDataFile]::Instance) { [WriteDataFile]::Instance = [WriteDataFile]::new(); } return [WriteDataFile]::Instance } [void] RegisterEvents() { $this.UnregisterEvents(); $this.RegisterEvent([AzSdkRootEvent]::GenerateRunIdentifier, { $currentInstance = [WriteDataFile]::GetInstance(); try { $currentInstance.SetRunIdentifier([AzSdkRootEventArgument] ($Event.SourceArgs | Select-Object -First 1)); } catch { $currentInstance.PublishException($_); } }); $this.RegisterEvent([SVTEvent]::CommandStarted, { $currentInstance = [WriteDataFile]::GetInstance(); try { $currentInstance.SetFilePath($Event.SourceArgs.ResourceContext, [FileOutputBase]::ETCFolderPath, ("SecurityEvaluationData-" + $currentInstance.RunIdentifier + ".json")); } catch { $currentInstance.PublishException($_); } }); $this.RegisterEvent([SVTEvent]::CommandCompleted, { $currentInstance = [WriteDataFile]::GetInstance(); try { $currentInstance.WriteToJson($Event.SourceArgs); $currentInstance.FilePath = ""; } catch { $currentInstance.PublishException($_); } }); } [void] WriteToJson([SVTEventContext[]] $arguments) { if ([string]::IsNullOrEmpty($this.FilePath)) { return; } if($arguments) { if (($arguments | Measure-Object).Count -gt 0) { [SVTEventResult[]] $result = @(); $arguments | ForEach-Object { $result += [SVTEventResult]::new($_); } if ($result.Count -gt 0) { #$result | ConvertTo-Json -Depth $this.JsonDepth | Out-File $this.FilePath #[Helpers]::ConvertToJsonCustom($result) | Out-File $($this.FilePath.Replace(".json", ".custom.json")) [Helpers]::ConvertToJsonCustom($result) | Out-File $this.FilePath [Helpers]::ConvertToPson($result) | Out-File $($this.FilePath.Replace(".json", ".pson")) } } } } } class SVTEventResult { [ResourceContext] $ResourceContext [ControlItem] $ControlItem [ControlResultData[]] $ControlResults = @() [string] $ExceptionMessage SVTEventResult ([SVTEventContext] $context) { $this.ResourceContext = $context.ResourceContext; $this.ControlItem = $context.ControlItem; if($context.ExceptionMessage) { $this.ExceptionMessage = $context.ExceptionMessage.ToString(); } if($context.ControlResults) { $context.ControlResults | ForEach-Object { $this.ControlResults += [ControlResultData]::new($_); } } } } class ControlResultData { [string] $ChildResourceName = "" [string] $VerificationResult = "" [MessageDataObject[]] $Messages = @(); ControlResultData([ControlResult] $result) { $this.ChildResourceName = $result.ChildResourceName; $this.VerificationResult = $result.VerificationResult.ToString(); if($result.Messages) { $result.Messages | ForEach-Object { $this.Messages += [MessageDataObject]::new($_); } } } } class MessageDataObject { [string] $MessageType = ""; [string] $Message = ""; #[bool] $MaintainState = $false; [PSObject] $DataObject; #[string] $DataObjectType; MessageDataObject([MessageData] $message) { $this.MessageType = $message.MessageType.ToString(); $this.Message = $message.Message; if($message.DataObject) { <#if ($message.DataObject.GetType().FullName -eq "System.Management.Automation.ErrorRecord") { if([string]::IsNullOrEmpty($message.Message)) { $this.Message = $message.DataObject.ToString(); } else { $this.DataObject = $message.DataObject.ToString(); } } else {#> try { [Helpers]::ConvertToJsonCustom($message.DataObject); $this.DataObject = $message.DataObject; } catch { $this.DataObject = $message.DataObject | Out-String; } #} } #$this.MaintainState = $message.MaintainState; #$this.DataObjectType = $message.DataObjectType; } } |