Progress improvements + Waiting with progress

This commit is contained in:
lksz 2021-04-16 15:45:36 +00:00
parent b1f612828f
commit 870887a577
2 changed files with 33 additions and 1 deletions

View File

@ -32,8 +32,9 @@ if( $Completed ) { $progressParams.Completed = $true
if( $Elapsed ) {
if( -not $Status ) {
$progressParams.Status = "Running for $(ConvertFrom-TimeSpan $Elapsed)"
$progressParams.Status = "Running for #Elapsed#"
}
$progressParams.Status = $progressParams.Status -replace '#Elapsed#',$(ConvertFrom-TimeSpan $Elapsed)
if( $TotalSeconds ) {
$progressParams.PercentComplete = [Math]::Floor($(1000 * (($Elapsed.TotalSeconds * 1.0) / ($TotalSeconds * 1.0)))/10.0)
}

31
base/Wait-For.ps1 Normal file
View File

@ -0,0 +1,31 @@
[CmdletBinding()] param (
[Parameter(Mandatory)]
[ScriptBlock]$WhileLogic,
[Parameter(Mandatory)]
[string]$Activity,
[string]$Status,
[int]$TotalSeconds,
[int]$StepDelaySeconds = 1,
[switch]$NoCompleteMessage,
[string]$CompleteMessage = "Run time",
[System.ConsoleColor]$ForegroundColor = [System.ConsoleColor]::Cyan,
[int]$MinSecondsDelayForCompleteMessage = 0
)
$local:progressParams = [ordered]@{
Activity = $Activity
}
if( $Status ) { $progressParams.Status = $Status }
if( $TotalSeconds ) { $progressParams.TotalSeconds = $TotalSeconds}
$local:sw = [System.Diagnostics.Stopwatch]::StartNew()
while( Invoke-Command -ScriptBlock $WhileLogic -ArgumentList @($sw) ) {
$progressParams.Elapsed = $sw.Elapsed
Show-Progress @progressParams
Start-Sleep -Seconds $StepDelaySeconds
}
Show-Progress -Activity $Activity -Completed
if( -not $NoCompleteMessage -and ($MinSecondsDelayForCompleteMessage * 1000) -le $sw.ElapsedMilliseconds ) {
Write-Host -ForegroundColor $ForegroundColor "$CompleteMessage`: $(ConvertFrom-TimeSpan $sw.Elapsed)"
}