- Apr 10, 2020
- 61
- 45
Not any type of guarantee that it might not just break but it did work for me. Place the awr files in a directory and run that script using powershell in in that directory (e.g. save it as extract.ps1 and run
If you're not sure how powershell works it might not be the thing to do ... the code is also prone to easy manipulation by who ever authors those awr files. So it's a good idea to have an idea what's up.
powershell -ExecutionPolicy Unrestricted -File extract.ps1
). I ended up with a couple of thousand files. Note that this is also easy to break should a file need multiple lines or some of the format is changed.If you're not sure how powershell works it might not be the thing to do ... the code is also prone to easy manipulation by who ever authors those awr files. So it's a good idea to have an idea what's up.
Code:
if( (Test-Path '.\extract') -eq $false){
New-Item -Type Directory -Name extract
}
if( (Test-Path '.\fail') -eq $false){
New-Item -Type Directory -Name fail
}
$contentLines = Get-Content *.awr | where { $_ -like "*data:*" }
$outputCounter = 0;
foreach($line in $contentLines){
$fileInfo = $line -split ';'
if($fileInfo.count -ne 2){
Write-Warning "Skipped 1 File"
continue;
}
$meta = $fileInfo[0] -replace 'data:'
if( ($meta -match '^.*?\"(.*?)\"') -eq $true){
$name = $matches[1]
$ext = ($meta -split '/')[1]
}else{
Write-Warning "Skipped 1 File"
continue;
}
if( ($fileInfo[1] -match '^base64,') -eq $true){
# Cut Off the first 7 characters (base64,)
$fileOriginal = $fileInfo[1]
$file = $fileOriginal.substring(7)
# Cut Off the ", at the end of the line
$file = $file.Remove($file.length-2)
try{
$binaryData = [Convert]::FromBase64String($file)
$fileName = "$((pwd).Path)\extract\$($name)_$outputCounter.$ext"
[io.file]::WriteAllBytes($fileName,$binaryData)
}catch{
Write-Error "Convertion Failed: $name.$ext"
$fileOriginal | Out-File -FilePath ".\fail\$name.$ext"
continue;
}
}
Write-Output "Processed File: $outputCounter/$($contentLines.count)"
$outputCounter++;
}