powershell - PowerShell脚本开始在100多行输出之后添加空白行吗?

标签 powershell

我似乎遇到了一个奇怪的错误,或者我错过了脚本中的某些内容。

在我的脚本中,我正在显示人造进度条功能的进度。它通过重复更新控制台的同一行来工作,模仿了进度条。参见以下功能:

# 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:我已经确认,如果增加控制台窗口的缓冲区宽度和高度,此问题将消失。我仍然不确定如何解决此错误。有什么想法吗?

最佳答案

我在自己的计算机上验证了此错误。一旦开始包装缓冲区,-nonewline将无法完成其任务。

您可以做一些事情作为解决方法:

1)以编程方式将BufferSize增加到固定大小

$Host.UI.RawUI.BufferSize = New-Object System.Management.Automation.Host.Size(120, 5000)

2)经常清除屏幕,也许每100个节点或在每个节点之后清除屏幕,如果不需要跟踪输出

3)仅在接近缓冲区限制时才清除屏幕
if($Host.UI.RawUI.CursorPosition.Y -gt ($Host.UI.RawUI.BufferSize.Height - 5)) {cls}

4)通过暂时减小缓冲区的大小,仅清除屏幕缓冲区的旧部分。我原封不动地使用了您的功能(重命名删除了第二个“-”除外)。当屏幕缓冲区为100时,奇数行为将从50开始(每个循环2条输出线)
49 / 2000
[####################] 100% | FINAL
50 / 2000
[####                ]  20% | First
[########            ]  40% | Second
[############        ]  60% | Third
[################    ]  80% | Fourth
[####################] 100% | FINAL

51 / 2000
[####                ]  20% | First
[########            ]  40% | Second
[############        ]  60% | Third
[################    ]  80% | Fourth
[####################] 100% | FINAL

但是随着BufferSize的切换,我一直顺利地达到了2000/2000
1998 / 2000
[####################] 100% | FINAL
1999 / 2000
[####################] 100% | FINAL
2000 / 2000
[####################] 100% | FINAL

$range = 1..2000

测试代码如下:
foreach($i in $range)
{
    $Host.UI.RawUI.BufferSize = New-Object System.Management.Automation.Host.Size(120, 3000)

    Write-Host
    Write-Host $i "/ 2000"

    Update-ProgressBar "20" "First"
    Update-ProgressBar "40" "Second"
    Update-ProgressBar "60" "Third"
    Update-ProgressBar "80" "Fourth"
    Update-ProgressBar "100" "FINAL"

    $Host.UI.RawUI.BufferSize = New-Object System.Management.Automation.Host.Size(120, 2990)
}

实际上,您可以结合使用3和4,以便仅在缓冲区几乎已满时清除缓冲区的旧部分。

关于powershell - PowerShell脚本开始在100多行输出之后添加空白行吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18361392/

相关文章:

powershell - 将 "unknown"类型的 "unknown"属性设置为 "unknown"值的有效方法?

PowerShell - 向 while 循环添加计数器

powershell - 从 CSV 获取时出现错误 "Get-ADComputer : Cannot find an object with identity"

python - 在 powershell、python 或其他运行 windows 的脚本语言中通过首字母移动文件

powershell - 在 Powershell 中重定向 DLL 输出

powershell - powershell的第一个参数是object []类型吗?

powershell - 如何通过powershell重命名azure订阅名称

powershell - 批处理文件将命令的返回值分配给变量(来自 powershell)

powershell - 您可以更改脚本的 Out-Default 行为吗?

powershell - 使用计划的 Powershell 脚本调用 URL