是否可以将后续信息输出到同一行
Is it possible to output subsequent information to same line
我有一个脚本可以 ping 一个 IP 地址并将该信息发送到控制台 window。在高 ping 时间或错过 ping 的情况下,它也会写入日志。我只想在控制台 window 中保留高 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
}
我认为您应该使用 Write-Progress
以获得好的 ping。你不需要给出百分比,你可以使用 -Status
参数只显示最后一个好的。
这是我写的一个小例子,它可能会演示它是如何工作的look/operate(你可以自己执行这个看看,它不会 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
中使用它,但我想表明您不需要将其用作进度条有用。
有更好的方法来解决这个问题,但我也在玩弄并想出了这个。它在 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时间范围的选项。设置一个小的散列 table,它会散落到 write-host
。不完美,但它展示了另一种方法。
同样,这主要是为了好玩。
正如我在评论中提到的,可以通过以下方法控制光标位置:
[control]::SetCursorPosition([int]$x,[int]$y)
[console]
类型加速器指向相同的 Console
class,使您能够 WriteLine()
到 C# 控制台应用程序中的控制台。如果您愿意,还可以控制颜色和其他控制台行为:
Clear-Host
[console]::ForegroundColor = "Red"
1..10|%{
[console]::SetCursorPosition(2+$_,$_-1)
[console]::WriteLine("Hello World!")
}
[console]::ForegroundColor = "Green"
我有一个脚本可以 ping 一个 IP 地址并将该信息发送到控制台 window。在高 ping 时间或错过 ping 的情况下,它也会写入日志。我只想在控制台 window 中保留高 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
}
我认为您应该使用 Write-Progress
以获得好的 ping。你不需要给出百分比,你可以使用 -Status
参数只显示最后一个好的。
这是我写的一个小例子,它可能会演示它是如何工作的look/operate(你可以自己执行这个看看,它不会 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
中使用它,但我想表明您不需要将其用作进度条有用。
"`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时间范围的选项。设置一个小的散列 table,它会散落到 write-host
。不完美,但它展示了另一种方法。
同样,这主要是为了好玩。
正如我在评论中提到的,可以通过以下方法控制光标位置:
[control]::SetCursorPosition([int]$x,[int]$y)
[console]
类型加速器指向相同的 Console
class,使您能够 WriteLine()
到 C# 控制台应用程序中的控制台。如果您愿意,还可以控制颜色和其他控制台行为:
Clear-Host
[console]::ForegroundColor = "Red"
1..10|%{
[console]::SetCursorPosition(2+$_,$_-1)
[console]::WriteLine("Hello World!")
}
[console]::ForegroundColor = "Green"