lo so, questo è abbastanza vecchio, ma ero nella stessa situazione und modificato la soluzione dalla Boluwade Kujero, proprio perché scrivere righe vuote prima di scrivere il nuovo output può tradursi in una "tremolante" di uscita.
Quindi, nella seguente funzione, faccio semplicemente sovrascrivere la linea esistente, scrivere spazi vuoti fino a raggiungere la vecchia posizione del cursore e tornare all'ultimo carattere della nuova riga.
Inoltre ho aggiunto una barra di avanzamento ottico. Il progresso è calcolato dalla funzione attraverso determinati parametri:
function Write-Status
{
param([int]$Current,
[int]$Total,
[string]$Statustext,
[string]$CurStatusText,
[int]$ProgressbarLength = 35)
# Save current Cursorposition for later
[int]$XOrg = $host.UI.RawUI.CursorPosition.X
# Create Progressbar
[string]$progressbar = ""
for ($i = 0 ; $i -lt $([System.Math]::Round($(([System.Math]::Round(($($Current)/$Total) * 100, 2) * $ProgressbarLength)/100), 0)); $i++) {
$progressbar = $progressbar + $([char]9608)
}
for ($i = 0 ; $i -lt ($ProgressbarLength - $([System.Math]::Round($(([System.Math]::Round(($($Current)/$Total) * 100, 2) * $ProgressbarLength)/100), 0))); $i++) {
$progressbar = $progressbar + $([char]9617)
}
# Overwrite Current Line with the current Status
Write-Host -NoNewline "`r$Statustext $progressbar [$($Current.ToString("#,###").PadLeft($Total.ToString("#,###").Length))/$($Total.ToString("#,###"))] ($($(($Current/$Total) * 100).ToString("##0.00").PadLeft(6)) %) $CurStatusText"
# There might be old Text behing the current Currsor, so let's write some blanks to the Position of $XOrg
[int]$XNow = $host.UI.RawUI.CursorPosition.X
for ([int]$i = $XNow; $i -lt $XOrg; $i++) {
Write-Host -NoNewline " "
}
# Just for optical reasons: Go back to the last Position of current Line
for ([int]$i = $XNow; $i -lt $XOrg; $i++) {
Write-Host -NoNewline "`b"
}
}
utilizzare la funzione in questo modo:
For ([int]$i=0; $i -le 8192; $i++) {
Write-Status -Current $i -Total 8192 -Statustext "Running a long Task" -CurStatusText "Working on Position $i"
}
Il risultato sarà una progressbar esecuzione che sarà simile a questa (in una sola riga):
Esecuzione di un task lungo ██████████████████░░░░░░░░░░░░░░░░░ [4.242/ 8.192] (51,78%) Lavorando sulla posizione 424 2
Spero che questo vi aiuterà a qualcun altro
Solo un suggerimento, usando 'scrittura Progress' probabilmente fare qualcosa di simile a quello che si sta cercando di raggiungere. – arco444
Strano. Questo ha funzionato bene per me ... –
Avvolgi l'output in un '$ (...)' quindi è '' Write-Host -NoNewline $ ("' rWriting $ outputFileName ($ i/$ fileCount) ... $ perc% ")' ' – James