Skip to content

Instantly share code, notes, and snippets.

@nicholasdille
Created September 8, 2015 08:08
Show Gist options
  • Save nicholasdille/d393b2dc97f6d714dee1 to your computer and use it in GitHub Desktop.
Save nicholasdille/d393b2dc97f6d714dee1 to your computer and use it in GitHub Desktop.
Reads progress information on standard output and displays progress bar
function ConvertTo-Progress {
[CmdletBinding()]
param(
[Parameter(Mandatory,ValueFromPipeline)]
[ValidateNotNullOrEmpty()]
[string[]]
$ProgressText
,
[Parameter()]
[ValidateNotNullOrEmpty()]
[int]
$Id
,
[Parameter()]
[ValidateNotNullOrEmpty()]
[int]
$ParentId
)
Process {
foreach ($Line in $ProgressText) {
$ProgressParams = @{
Activity = '[UNKNOWN_ACTIVITY]'
CurrentOperation = '[UNKNOWN_OPERATION]'
Status = '[UNKNOWN_STATUS]'
PercentComplete = 0
}
if ($PSBoundParameters.ContainsKey('Id')) {
$ProgressParams.Add('Id', $Id)
}
if ($PSBoundParameters.ContainsKey('ParentId')) {
$ProgressParams.Add('ParentId', $ParentId)
}
$LineMatched = $false
if ($Line -imatch 'Id=(\S+)') {
$ProgressParams['Id'] = $Matches[1]
$LineMatched = $true
}
if ($Line -imatch 'ParentId=(\S+)') {
$ProgressParams['ParentId'] = $Matches[1]
$LineMatched = $true
}
if ($Line -imatch 'Activity="([^"]+)"') {
$ProgressParams['Activity'] = $Matches[1]
$LineMatched = $true
}
if ($Line -imatch 'Operation="([^"]+)"') {
$ProgressParams['CurrentOperation'] = $Matches[1]
$LineMatched = $true
}
if ($Line -imatch 'Status="([^"]+)"') {
$ProgressParams['Status'] = $Matches[1]
$LineMatched = $true
}
if ($Line -imatch 'Percentage=(\S+)') {
$ProgressParams['PercentComplete'] = $Matches[1]
$LineMatched = $true
}
if ($LineMatched) {
Write-Progress @ProgressParams
} else {
Write-Output $Line
}
Start-Sleep -Seconds 2
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment