Scripts/Format-IntuneDiagnostics.ps1
param( $DiagnosticArchiveZipPath = "C:\Users\jterlouw\Downloads\DiagLogs-W04-95582872742-20220221T092806Z.zip" ) #region Formatting Choices $flatFileNameTemplate = '({0:D2}) ({3}) (0x{2:X8})' $maxLengthForInputTextPassedToOutput = 80 #endregion #region Create Output Folders and Expand Zip $diagnosticArchiveTempUnzippedPath = $DiagnosticArchiveZipPath + "_expanded" if (-not (Test-Path $diagnosticArchiveTempUnzippedPath)) { mkdir $diagnosticArchiveTempUnzippedPath } $reformattedArchivePath = $DiagnosticArchiveZipPath + "_formatted" if (-not (Test-Path $reformattedArchivePath)) { mkdir $reformattedArchivePath } Expand-Archive -Path $DiagnosticArchiveZipPath -DestinationPath $diagnosticArchiveTempUnzippedPath #endregion #region Discover and Move/rename Files $resultElements = ([xml](Get-Content -Path (Join-Path -Path $diagnosticArchiveTempUnzippedPath -ChildPath "results.xml"))).Collection.ChildNodes | ForEach-Object { $_ } $n = 0 foreach ( $element in $resultElements ) { $directiveNumber = $n $n++ if ($element.Name -eq 'ID') { continue } $directiveType = $element.Name $directiveStatus = [int]$element.Attributes.ItemOf('HRESULT').psbase.Value $directiveUserInputRaw = $element.InnerText $directiveUserInputFileNameCompatible = $directiveUserInputRaw -replace '[\\|/\[\]<>\:"\?\*%\.\s]', '_' $directiveUserInputTrimmed = $directiveUserInputFileNameCompatible.substring(0, [System.Math]::Min($maxLengthForInputTextPassedToOutput, $directiveUserInputFileNameCompatible.Length)) $directiveSummaryString = $flatFileNameTemplate -f $directiveNumber, $directiveType, $directiveStatus, $directiveUserInputTrimmed $directiveOutputFolder = Join-Path -Path $diagnosticArchiveTempUnzippedPath -ChildPath $directiveNumber $directiveOutputFiles = Get-ChildItem -Path $directiveOutputFolder -File foreach ( $file in $directiveOutputFiles) { $leafSummaryString = $directiveSummaryString, $file.Name -join ' ' Copy-Item $file.FullName -Destination (Join-Path -Path $reformattedArchivePath -ChildPath $leafSummaryString) } } #endregion Remove-Item -Path $diagnosticArchiveTempUnzippedPath -Force -Recurse |