Private/Export-PSMJTestRun.ps1
|
function Set-PSMJTestRunOutcome { [CmdletBinding()] param( [Parameter(Mandatory)] [PSCustomObject]$Run, [Parameter(Mandatory)] [ValidateSet('RemainedActive', 'Disconnected', 'Inconclusive')] [string]$Outcome, [Parameter()] [AllowEmptyString()] [string]$Notes = '' ) $existingRun = @($script:TestRuns | Where-Object RunId -eq $Run.RunId | Select-Object -First 1) if ($existingRun.Count -eq 0) { throw 'The outcome must reference an existing test run.' } if ($Run.Status -eq 'Running') { throw 'Stop the test before recording an observed broker outcome.' } $recordedAtUtc = Get-PSMJUtcNow if ($recordedAtUtc.Kind -ne [DateTimeKind]::Utc) { throw 'Outcome timestamps must be UTC.' } $Run.Outcome = [PSCustomObject]@{ RunId = $Run.RunId Outcome = $Outcome RecordedAtUtc = $recordedAtUtc Notes = $Notes } $Run.Outcome } function Get-PSMJExportFileName { 'PSMouseJiggler-test-results-{0}.csv' -f (Get-PSMJUtcNow).ToString('yyyyMMdd-HHmmss-fff') } function Get-PSMJExportPath { [CmdletBinding()] param() Add-Type -AssemblyName PresentationFramework -ErrorAction Stop $dialog = New-Object Microsoft.Win32.SaveFileDialog $dialog.Title = 'Export VDI test results' $dialog.Filter = 'CSV files (*.csv)|*.csv' $dialog.DefaultExt = '.csv' $dialog.AddExtension = $true $dialog.FileName = Get-PSMJExportFileName if ($dialog.ShowDialog() -eq $true) { return $dialog.FileName } $null } function ConvertTo-PSMJExportTimestamp { param( [AllowNull()] [object]$Value ) if ($null -eq $Value) { return '' } if ($Value -isnot [DateTime] -or $Value.Kind -ne [DateTimeKind]::Utc) { throw 'Export timestamps must be UTC DateTime values.' } $Value.ToString('o') } function Export-PSMJTestRun { [CmdletBinding()] param( [Parameter(Mandatory)] [PSCustomObject]$Run, [Parameter(Mandatory)] [ValidateNotNullOrEmpty()] [string]$Path ) if ($null -eq $Run -or [string]::IsNullOrWhiteSpace($Run.RunId)) { throw 'A valid test run is required for export.' } if (@($script:TestRuns | Where-Object RunId -eq $Run.RunId).Count -eq 0) { throw 'The run to export does not exist in this module session.' } $columns = @( 'RecordType', 'RunId', 'StartedAtUtc', 'EndedAtUtc', 'IntervalMilliseconds', 'ProviderSettings', 'SelectedProviderIds', 'RunStatus', 'HistoryTruncated', 'DroppedEventCount', 'ProviderId', 'ProviderStatus', 'ActivityCount', 'LastActivityAtUtc', 'EventId', 'EventType', 'EventTimestampUtc', 'EventResult', 'Error', 'Outcome', 'OutcomeRecordedAtUtc', 'Notes' ) $rows = [System.Collections.Generic.List[object]]::new() $outcome = $Run.Outcome $providerSettings = if ($null -ne $Run.ProviderSettings) { ConvertTo-Json -InputObject $Run.ProviderSettings -Compress -Depth 10 } else { '{}' } [void]$rows.Add([PSCustomObject][ordered]@{ RecordType = 'RunSummary' RunId = $Run.RunId StartedAtUtc = ConvertTo-PSMJExportTimestamp $Run.StartedAtUtc EndedAtUtc = ConvertTo-PSMJExportTimestamp $Run.EndedAtUtc IntervalMilliseconds = $Run.IntervalMilliseconds ProviderSettings = $providerSettings SelectedProviderIds = @($Run.SelectedProviderIds) -join ';' RunStatus = $Run.Status HistoryTruncated = [bool]$Run.HistoryTruncated DroppedEventCount = [int]$Run.DroppedEventCount ProviderId = '' ProviderStatus = '' ActivityCount = '' LastActivityAtUtc = '' EventId = '' EventType = '' EventTimestampUtc = '' EventResult = '' Error = '' Outcome = if ($null -ne $outcome) { $outcome.Outcome } else { '' } OutcomeRecordedAtUtc = if ($null -ne $outcome) { ConvertTo-PSMJExportTimestamp $outcome.RecordedAtUtc } else { '' } Notes = if ($null -ne $outcome) { $outcome.Notes } else { '' } }) foreach ($provider in $Run.Providers) { [void]$rows.Add([PSCustomObject][ordered]@{ RecordType = 'ProviderSummary' RunId = $Run.RunId StartedAtUtc = '' EndedAtUtc = '' IntervalMilliseconds = '' ProviderSettings = if ($provider.Settings) { ConvertTo-Json -InputObject $provider.Settings -Compress -Depth 10 } else { '{}' } SelectedProviderIds = '' RunStatus = '' HistoryTruncated = [bool]$Run.HistoryTruncated DroppedEventCount = [int]$Run.DroppedEventCount ProviderId = $provider.ProviderId ProviderStatus = $provider.Status ActivityCount = $provider.ActivityCount LastActivityAtUtc = ConvertTo-PSMJExportTimestamp $provider.LastActivityAtUtc EventId = '' EventType = '' EventTimestampUtc = '' EventResult = '' Error = if ($provider.Error) { $provider.Error } else { '' } Outcome = '' OutcomeRecordedAtUtc = '' Notes = '' }) } foreach ($event in $Run.History) { [void]$rows.Add([PSCustomObject][ordered]@{ RecordType = 'ActivityEvent' RunId = $Run.RunId StartedAtUtc = '' EndedAtUtc = '' IntervalMilliseconds = '' ProviderSettings = '' SelectedProviderIds = '' RunStatus = $event.RunStatus HistoryTruncated = [bool]$Run.HistoryTruncated DroppedEventCount = [int]$Run.DroppedEventCount ProviderId = if ($event.ProviderId) { $event.ProviderId } else { '' } ProviderStatus = if ($event.ProviderStatus) { $event.ProviderStatus } else { '' } ActivityCount = '' LastActivityAtUtc = '' EventId = $event.EventId EventType = $event.EventType EventTimestampUtc = ConvertTo-PSMJExportTimestamp $event.OccurredAtUtc EventResult = if ($event.Result) { $event.Result } else { '' } Error = if ($event.Error) { $event.Error } else { '' } Outcome = '' OutcomeRecordedAtUtc = '' Notes = '' }) } if ($PSVersionTable.PSVersion.Major -ge 6) { $csvRows = @($rows | Select-Object -Property $columns | ConvertTo-Csv) } else { $csvRows = @($rows | Select-Object -Property $columns | ConvertTo-Csv -NoTypeInformation) } $encoding = New-Object System.Text.UTF8Encoding($false) [System.IO.File]::WriteAllLines([System.IO.Path]::GetFullPath($Path), $csvRows, $encoding) } |