我似乎遇到了一个奇怪的错误,或者我的脚本中遗漏了一些东西。
在我的脚本中,我使用人造进度条功能显示进度。它通过重复更新控制台的同一行来工作,模仿进度条。请看下面的函数:
# Update-Progress-Bar : v1.0 : 2013-07-29
# Displays a percentage bar. Meant to be used repeatedly to update the same console line (giving the appearance of incrementing progress).
# - $Percentage : Determines how much progress is shown on the bar.
# - $Message : The message that accompanies the progress bar.
function Update-Progress-Bar ($Percentage, $Message){
# Save the current cursor position so we can come back later.
$CursorPosition = $Host.UI.RawUI.CursorPosition
# Convert the percentage into a proper progress bar.
$ProgressBarMax = "20"
$ProgressBarCount = [Math]::Floor([Decimal]($Percentage / 5))
$ProgressBar = ("#" * $ProgressBarCount) + (" " * ($ProgressBarMax - $ProgressBarCount))
# Change the format of the percentage depending on length.
switch ($Percentage.Length){
1 {$Percentage = " " + $Percentage + "%"}
2 {$Percentage = " " + $Percentage + "%"}
default {$Percentage = $Percentage + "%"}
}
# Trim or pad the message as necessary.
$MessageMaxLength = "50"
if ($Message.Length -gt $MessageMaxLength){ $Message = $Message.Remove($MessageMaxLength) }
else { $Message = $Message + (" " * ($MessageMaxLength - $Message.Length)) }
# Display our progress bar, percentage, and message.
Write-Host -nonewline -ForeGroundColor Blue "[$ProgressBar] $Percentage"
Write-Host " | $Message"
# Revert back to the original cursor position.
$Host.UI.RawUI.CursorPosition = $CursorPosition
}
无论出于何种原因,在处理了大约 100 多条记录之后(我将其用作脚本的一部分,我定期对 1000 台机器执行操作),它开始执行双换行符,这破坏了进度条的功能. 所以我最终得到了这个......
[ 126 / 2275 ] ComputerName1
[ ] 0% | Verifying network connectivity...
[## ] 10% | Verifying file system access...
[#### ] 20% | Determining installed operating system...
[###### ] 30% | Executing action...
[####################] 100% | Action Completed
[ 127 / 2275 ] ComputerName2
[ ] 0% | Verifying network connectivity...
[## ] 10% | Verifying file system access...
[#### ] 20% | Determining installed operating system...
[###### ] 30% | Executing action...
[####################] 100% | Action Completed
当我应该有....
[ 126 / 2275 ] ComputerName1
[####################] 100% | Action Completed
[ 127 / 2275 ] ComputerName2
[####################] 100% | Action Completed
关于这个问题的任何想法和可能的解决方法?
编辑#1:当我达到控制台的缓冲区高度限制时,是否可能发生这种情况(如,它开始丢弃旧的输出行)?
编辑#2:我已经确认如果我增加控制台窗口的缓冲区宽度和高度,这个问题就会消失。我仍然不确定如何解决这个错误。想法?