[維修小札] 如何持續測試網路PING值並且記錄 How to continuously test network ping values and record them
大概是這樣的功能,他會持續監測,並且記錄PING值。
如果有timeout,會另存檔案作紀錄。
The functionality is like this:
it continuously monitors and records ping values.
The output file name is ping_log.txt.
If there is a timeout, it saves the record in a separate file, ping_error_log.txt.
然後將附檔名改成.ps1
First, create a plain text file and paste the following code into it.
Then, change the file extension to .ps1.
############################################################
$OutputFile = "ping_log.txt"
$ErrorFile = "ping_error_log.txt"
$PingTarget = "168.95.1.1"
while ($true) {
$PingResult = Test-Connection -ComputerName $PingTarget -Count 1 -ErrorAction SilentlyContinue
$Timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
if ($PingResult) {
$LogEntry = "$Timestamp `t $($PingResult.Address) `t Time=$($PingResult.ResponseTime)ms `t TTL=$($PingResult.Ttl)"
Add-Content -Path $OutputFile -Value $LogEntry
if ($PingResult.ResponseTime -eq $null -or $PingResult.ResponseTime -eq 0) {
$ErrorLogEntry = "$Timestamp `t $($PingResult.Address) `t Time=ms `t TTL=$($PingResult.Ttl)"
# Write abnormal entries to the error log file
Add-Content -Path $ErrorFile -Value $ErrorLogEntry
}
} else {
$LogEntry = "$Timestamp `t $PingTarget `t Request timed out"
Add-Content -Path $OutputFile -Value $LogEntry
Add-Content -Path $ErrorFile -Value $LogEntry
}
Start-Sleep -Seconds 1
}
############################################################
右鍵"用PowerShell執行"
他就會動產生 ping_log.txt隨時記錄,
並且在timeout的時候會另存ping_error_log.txt 方便觀測。
Right-click and select "Run with PowerShell".
It will generate ping_log.txt to continuously record.
It saves to ping_error_log.txt, when there is a timeout.
留言
張貼留言