Skip to content

Instantly share code, notes, and snippets.

@JustinGrote
Last active March 10, 2022 11:41
Show Gist options
  • Save JustinGrote/c212d05fed2dd3f1ecf199c37efab662 to your computer and use it in GitHub Desktop.
Save JustinGrote/c212d05fed2dd3f1ecf199c37efab662 to your computer and use it in GitHub Desktop.
An Example of parsing command output text into Write-Progress from a Foreach -Parallel Pipeline
$commandOutput = @'
save image[D:\2D\Test\Just Flow Dark 03.png]: 50 of 4500, 20% complete
save image[D:\2D\Test\Just Flow Dark 03.png]: 100 of 4500, 40% complete
save image[D:\2D\Test\Just Flow Dark 03.png]: 150 of 4500, 60% complete
save image[D:\2D\Test\Just Flow Dark 05.png]: 50 of 4500, 20% complete
save image[D:\2D\Test\Just Flow Dark 03.png]: 200 of 4500, 80% complete
save image[D:\2D\Test\Just Flow Dark 03.png]: 250 of 4500, 90% complete
save image[D:\2D\Test\Just Flow Dark 05.png]: 100 of 4500, 40% complete
save image[D:\2D\Test\Just Flow Dark 05.png]: 150 of 4500, 60% complete
save image[D:\2D\Test\Just Flow Dark 05.png]: 200 of 4500, 80% complete
save image[D:\2D\Test\Just Flow Dark 04.png]: 50 of 4500, 20% complete
save image[D:\2D\Test\Just Flow Dark 04.png]: 100 of 4500, 40% complete
save image[D:\2D\Test\Just Flow Dark 04.png]: 150 of 4500, 60% complete
save image[D:\2D\Test\Just Flow Dark 04.png]: 200 of 4500, 80% complete
save image[D:\2D\Test\Just Flow Dark 04.png]: 250 of 4500, 90% complete
save image[D:\2D\Test\Just Flow Dark 04.png]: 300 of 4500, 100% complete
save image[D:\2D\Test\Just Flow Dark 03.png]: 300 of 4500, 100% complete
save image[D:\2D\Test\Just Flow Dark 05.png]: 250 of 4500, 90% complete
save image[D:\2D\Test\Just Flow Dark 05.png]: 300 of 4500, 100% complete
'@ -split '\n'
function Show-MagickProgres {
[CmdletBinding()]
param(
[Parameter(ValueFromPipeline)]$MagickOutput
)
begin {
#We want one progress per file name
$PathToId = @{}
#High number just so we don't conflict with any other progress
[int]$activityId = 1000000
#Top level progress indicator
$parentId = Get-Random -min 9000 -max 10000
Write-Progress -Id $parentId -Activity 'Batch ImageMagick Processing'
}
process {
#Capture groups are used so we can pull straight from $matches
$MagickOutputRegex = 'save image\[(?<path>.*)\]: (?<current>\d+) of (?<total>\d+), (?<percentage>\d+)% complete'
if ($MagickOutput -notmatch $MagickOutputRegex) {
Write-Error "Invalid output: $MagickOutput"
return
}
#Fetch the ID for the file or create a new one
$pathId = if ($PathToId.ContainsKey($matches.path)) {
$PathToId[$matches.path]
} else {
$activityId++
$PathToId[$matches.path] = $activityId
$activityId
}
$ProgressParams = @{
Id = $pathId
ParentId = $parentId
Activity = "Saving Image $($matches.path)"
PercentComplete = $matches.percentage
Status = "$($matches.current) of $($matches.total)"
}
Write-Progress @ProgressParams
}
end {
Write-Progress -Id $ParentId -Activity 'Complete' -Completed
}
}
$commandOutput | Foreach {sleep 0.5;$_} | Show-MagickProgres
@JustinGrote
Copy link
Author

Demo.mp4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment