winutil/tools/Update-Progress.ps1
2024-08-21 21:05:52 +06:00

49 lines
1.5 KiB
PowerShell
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# Update-Progress.ps1
function Update-Progress {
<#
.SYNOPSIS
A wrapper for PowerShell 'Write-Progress' Cmdlet.
.PARAMETER StatusMessage
A mandatory parameter whichll be used when displaying progress bar using 'Write-Progress' Cmdlet.
.PARAMETER Percent
An integer value (0-100) representing the completion percentage.
.PARAMETER Activity
The activity name to be displayed in the progress bar. Defaults to "Processing" if not provided.
.PARAMETER LogProgress
A switch that indicates whether the progress should be logged to a file.
.EXAMPLE
Update-Progress "Processing files..." 50 "File Processing" -LogProgress
#>
param (
[Parameter(Mandatory, Position = 0)]
[ValidateNotNullOrEmpty()]
[string]$StatusMessage,
[Parameter(Position = 1)]
[ValidateRange(0, 100)]
[int]$Percent,
[Parameter(Position = 2)]
[string]$Activity = "Processing", # Default activity to "Processing" if not provided
[Parameter(Position = 3)]
[switch]$LogProgress
)
# Write the progress to the console
Write-Progress -Activity $Activity -Status $StatusMessage -PercentComplete $Percent
# Optionally log the progress to a file
if ($LogProgress) {
$logMessage = "{0:yyyy-MM-dd HH:mm:ss} - {1} - {2}% - {3}" -f (Get-Date), $Activity, $Percent, $StatusMessage
$logMessage | Out-File -FilePath "$PSScriptRoot\progress.log" -Append -Encoding utf8
}
}