我的谷歌-fu让我失望了。我有一个非常简单的脚本,仅测试从一台服务器到另一台服务器的 ping 连接,并在顶部显示运行时。
我希望能够随时通过一个按键(脚本中的“q”)来停止这个脚本,并让它提供一些关于它运行了多长时间以及失败了多少次的基本统计数据。
我可以使用 ctrl+c 停止脚本,但这会完全退出并且不允许我显示任何统计信息。下面是我的脚本。任何帮助表示赞赏!
###############SETTINGS##########################
#Server to Ping:
$RemoteMachine = "ServerNameorIP"
#Seconds between each ping:
$PingInterval = 5
#File to log results:
$outFile = "C:\PingLog\"+$today+".PingLog.txt"
##################################################
$today = Get-Date -Format "yyyy-MM-dd"
#start a stopwatch for the progress bar
$elapsedTime = [system.diagnostics.stopwatch]::StartNew()
while ($true)
{
#Stick a progress bar at the top that shows elapsed time and how often we're pinging:
write-progress -activity "Now testing ping connectivity to $RemoteMachine every $PingInterval seconds. Press q to quit." -status "$([string]::Format("Time …Run Code Online (Sandbox Code Playgroud)