2021-01-29 23:26:39 +00:00
|
|
|
[CmdletBinding()]param(
|
|
|
|
[Parameter(Mandatory, Position=0)]
|
|
|
|
[string]$Activity,
|
|
|
|
[string]$Status,
|
|
|
|
[Parameter(ParameterSetName="Calculate",Mandatory)]
|
|
|
|
[int]$ItemProgress,
|
|
|
|
[Parameter(ParameterSetName="Calculate")]
|
|
|
|
[int]$ItemCount,
|
|
|
|
[Parameter(ParameterSetName="PercentComplete",Mandatory)]
|
|
|
|
[int]$PercentComplete,
|
|
|
|
[Parameter(ParameterSetName="SecondsRemaining",Mandatory)]
|
|
|
|
[int]$SecondsRemaining,
|
|
|
|
[Parameter(ParameterSetName="Elapsed",Mandatory)]
|
|
|
|
[Parameter(ParameterSetName="Calculate")]
|
|
|
|
[Timespan]$Elapsed,
|
|
|
|
[Parameter(ParameterSetName="Elapsed")]
|
|
|
|
[int]$TotalSeconds,
|
|
|
|
[string]$CurrentOperation,
|
|
|
|
[Parameter(ParameterSetName="Completed",Mandatory)]
|
|
|
|
[switch]$Completed
|
|
|
|
)
|
|
|
|
|
|
|
|
$local:progressParams = [ordered]@{
|
|
|
|
Activity = $Activity
|
|
|
|
}
|
|
|
|
if( $Status ) { $progressParams.Status = $Status }
|
|
|
|
if( $PercentComplete ) { $progressParams.PercentComplete = $PercentComplete }
|
|
|
|
if( $SecondsRemaining ) { $progressParams.SecondsRemaining = $SecondsRemaining }
|
|
|
|
if( $ItemCount ) { $progressParams.PercentComplete = (100.0 * $ItemProgress / $ItemCount) }
|
|
|
|
if( $CurrentOperation ) { $progressParams.CurrentOperation = $CurrentOperation }
|
|
|
|
if( $Completed ) { $progressParams.Completed = $true }
|
|
|
|
|
|
|
|
if( $Elapsed ) {
|
|
|
|
if( -not $Status ) {
|
2021-04-16 15:45:36 +00:00
|
|
|
$progressParams.Status = "Running for #Elapsed#"
|
2021-01-29 23:26:39 +00:00
|
|
|
}
|
2021-04-22 16:51:42 +00:00
|
|
|
$local:TotalTime = [TimeSpan]::Zero
|
2021-01-29 23:26:39 +00:00
|
|
|
if( $TotalSeconds ) {
|
|
|
|
$progressParams.PercentComplete = [Math]::Floor($(1000 * (($Elapsed.TotalSeconds * 1.0) / ($TotalSeconds * 1.0)))/10.0)
|
2021-04-22 16:51:42 +00:00
|
|
|
$TotalTime = [TimeSpan]::FromSeconds($TotalSeconds)
|
2021-01-29 23:26:39 +00:00
|
|
|
}
|
2021-04-22 16:51:42 +00:00
|
|
|
$progressParams.Status = $progressParams.Status -replace '#Elapsed#',$(ConvertFrom-TimeSpan $Elapsed) -replace "#TotalTime#",$(ConvertFrom-TimeSpan $TotalTime)
|
2021-01-29 23:26:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
If( $ItemProgress ) {
|
|
|
|
if( $progressParams.Status ) { $progressParams.Status = " " + $progressParams.Status }
|
|
|
|
$progressParams.Status = "#$ItemProgress$(if($ItemCount){" out of $ItemCount"})" + $progressParams.Status
|
|
|
|
}
|
|
|
|
|
|
|
|
$local:VerboseProgress = $("$([datetime]::Now.TimeOfDay.ToString("hh\:mm\:ss")) Progress: $($progressParams.Activity)" +
|
|
|
|
"$(if($progressParams.Status){" ($($progressParams.Status))"})" +
|
|
|
|
"$(if($ItemProgress) {" #$ItemProgress$(if($ItemCount){" out of $ItemCount"})"})" +
|
|
|
|
"$(if($progressParams.PercentComplete){" %$($progressParams.PercentComplete)"})" +
|
|
|
|
"$(if($progressParams.Completed){if(-not $progressParams.Status){" Done."}} else {"..."})"
|
|
|
|
)
|
|
|
|
|
|
|
|
Write-Verbose $VerboseProgress
|
|
|
|
if( Test-Path variable:global:psISE ) {
|
|
|
|
if( -not $VerbosePreference -eq 'Continue' ) {
|
|
|
|
Write-Host -ForegroundColor Cyan $VerboseProgress
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Write-Progress @progressParams
|
|
|
|
}
|