【问题标题】:Is it possible to output subsequent information to same line是否可以将后续信息输出到同一行
【发布时间】:2015-06-24 15:05:00
【问题描述】:

我有一个脚本可以 ping 一个 IP 地址并将该信息发送到控制台窗口。在 ping 时间长或错过 ping 的情况下,它也会写入日志。我只想在控制台窗口中保留高 ping 时间和错过的 ping,并允许好的 ping 相互覆盖。这可能吗?

对于高 ping 时间,这是输出(类似的代码用于错过 ping)。

$out = ("{0}ms at $(get-date -format G)" -f $ping.ResponseTime)
write-host $out -foregroundcolor "yellow"
$out >> .\runningPing$ipAddress.txt

对于正常的 ping 时间,输出是这样的。

$out ("{0}ms" -f $ping.ResponseTime)
write-host $out -foregroundcolor "green"

我想让最后一行只为正常 ping 覆盖它自己,但在程序运行时让高和错过的 ping 向上推屏幕。这是我可以在 PS 中做的事情吗?

解决方案 感谢@Mathias R. Jensen,我想出了这个解决方案:

  if ($ping.statuscode -eq 0) {
    if ($ping.responsetime -gt $waitTime) {
      $highPings = $highPings + 1
      $out = ("{0}ms at $(get-date -format G)" -f $ping.ResponseTime)
      [console]::SetCursorPosition(0,$highPings + $droppedPings + 1)
      write-host $out -foregroundcolor "yellow"
      $out >> $outFile
    }
    else {  
      $out = ("{0}ms $i of $pingCount" -f $ping.ResponseTime)
      [console]::SetCursorPosition(0,$highPings + $droppedPings + 2)
      write-host $out -foregroundcolor "green"
    }
  }
  else {
    $droppedPings = $droppedPings + 1
    $out = ("missed ping at $(get-date -format G)")
    [console]::SetCursorPosition(0,$highPings + $droppedPings + 1)
    write-host $out -foregroundcolor "red" 
    $out >> $outFile    
  }

【问题讨论】:

  • 简单的解决方案是将Clear-Host 与 if 语句一起使用,或者在与您的 ping 相同的块中使用。那行得通吗?在控制台中,您还可以使用退格键进行花哨的足部操作
  • 您想输出类似于旧存档进度条的行,就像旧的pkzip.exeCompressing test.dat 2% 行中所做的那样,并随着时间的推移用更高的值覆盖该百分比?我对此表示怀疑,Powershell 不喜欢搞乱流。我检查了$h=[char]8(Ctrl+H,一个退格键),不,输出这并没有改变字符串的前一部分,所以在 Powershell 自己的窗口中可能是不可能的。
  • @Matt clear-host 也会清除旧的警告信息,所以不要掷骰子。
  • 您可以使用 [console]::SetCursorPosition($x,$y) 来完成此操作,但我更喜欢使用 Write-Progress 的 briantist 解决方案,而不是摆弄控制台光标
  • @MathiasR.Jessen 这正是我所需要的。我使用我一直在跟踪丢弃和高 ping 的计数器将光标向下推到适当的行数。我暂时遇到了在最后一个高点或错过 ping 的顶部写好的 ping 的问题,但除此之外它工作得很好。谢谢

标签: powershell powershell-4.0


【解决方案1】:

我认为您应该使用Write-Progress 来获得良好的 ping。您不需要给出百分比,您可以使用-Status 参数仅显示最后一个好的百分比。

这是我写的一个小例子,它可能会展示它的外观/操作方式(你可以自己执行它来查看,它不会 ping 任何东西,它只是一个模拟):

$goods = 0
0..100 | % {
    if ((Get-Random -Minimum 0 -Maximum 100) -ge 50) {
        $goods += 1
        Write-Progress -Activity Test -Status "Last good ping: $_ ($goods total good pings)"
    } else {
        Write-Warning "Bad ping"
    }
    Start-Sleep -Seconds 1
}

在这种情况下,您甚至可以计算出良好 ping 的百分比,并在 Write-Progress 中使用它,但我想表明您不需要将其用作进度条以使其有用。

【讨论】:

  • 这实现了我最初的目标。它把所有东西都放在同一个地方,它给了我一些东西来验证进程是否仍在运行。谢谢。
【解决方案2】:

正如我在cmets中提到的,可以通过这种方法控制光标位置:

[control]::SetCursorPosition([int]$x,[int]$y)

[console] 类型加速键指向同一个 Console 类,使您能够在 C# 控制台应用程序中将 WriteLine() 指向控制台。如果您愿意,还可以控制颜色和其他控制台行为:

Clear-Host
[console]::ForegroundColor = "Red"
1..10|%{
    [console]::SetCursorPosition(2+$_,$_-1)
    [console]::WriteLine("Hello World!")
}
[console]::ForegroundColor = "Green"

【讨论】:

    【解决方案3】:

    briantist 有更好的方法来解决这个问题,但我也在玩,也想出了这个。它在 ISE 中不起作用,但应该在 PowerShell 控制台上起作用。它使用"`b",这是退格字符,以便文本将在控制台主机写入时覆盖自身。可能对您没有帮助,但可能对其他人有用。

    switch($ping.ResponseTime){
        {$_ -ge 0 -and $_ -le 100}{
            $out = "{0}ms" -f $_
            $options = @{ForegroundColor = "Green"; NoNewline = $true}
            $backup = "`b" * $out.Length
        }
        {$_ -ge 500 -and $_ -le 900}{
            $out = "{0}ms at $(get-date -format G)" -f $_
            $options = @{ForegroundColor = "Yellow"; NoNewline = $false}
            $backup = "`n"
        }
    }
    
    Write-Host "$backup$out" @options
    

    使用switch 根据 ping 时间范围设置选项。设置一个小哈希表,该哈希表被喷到write-host。不完美,但它显示了另一种方法。

    同样,这主要是为了好玩。

    【讨论】:

      猜你喜欢
      • 2014-08-11
      • 2019-09-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多