【发布时间】:2015-04-10 08:33:23
【问题描述】:
我的 google-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 Elapsed: {0:d2}:{1:d2}:{2:d2}", $elapsedTime.Elapsed.hours, $elapsedTime.Elapsed.minutes, $elapsedTime.Elapsed.seconds))"
$pingFails = 0
#If the ping test fails, say something, and write it out to a log
if(!(Test-Connection -ComputerName bb-ts-db -Quiet))
{
$pingFails++
$dropTime = Get-Date -Format "MM/dd/yyyy - HH:mm:ss"
Add-Content -Path $outfile -value ("Connection Lost at: $dropTime")
Echo "Connection Lost at $dropTime"
}
#Exit the loop if a key is pressed:
if ($Host.UI.RawUI.KeyAvailable -and ("q" -eq $Host.UI.RawUI.ReadKey("IncludeKeyUp,NoEcho").Character))
{
Write-Host "Exiting now, don't try to stop me...." -Background DarkRed
break;
}
#Wait the preset amount of time between each ping loop
Start-Sleep $pingInterval
}
$elapsedTime.stop()
#Display the stats of this session:
echo "Test runtime: $elapsedTime.Elapsed"
echo "# of failed pings: $pingFails"
#If there WERE ping fails, write stats to $outFile
if($pingFails -gt 0)
{
Add-content -Path $outfile -value "`n `n `n"
Add-content -Path $outfile -value "Test runtime: $elapsedTime.Elapsed"
Add-content -Path $outfile -value "# of failed pings: $pingFails"
}
【问题讨论】:
-
您可以将 Pings 作为作业运行,然后执行
Do{<check for key press and quit if detected>}While((Get-Job -State Running))循环,尽管这种方式会杀死您的整个进度条。
标签: powershell keypress ping exit