首页 > 其他 > 详细

检测网络连通性并记录时间戳

时间:2015-05-11 10:22:35      阅读:212      评论:0      收藏:0      [点我收藏+]
Test-Connection, 之所以不采用这种方法, 是因为该命令对于丢包返回系统报错, 无法被抓取.
$end = [datetime]"2015年4月27日 20:32:20"
$start = Get-Date
$count = [math]::Round(($end - $start).TotalSeconds)
while($count -gt 0) {
Test-Connection 208.67.222.222 -Count 100 | select -Property address, responsetime, @{n="timestamp";e={Get-Date}}, @{n="status";e={if($_.responsetime -eq $null) {"unreachable"}}} #| Export-Csv -Path "C:\Users\Desktop\result.csv"
$count--
}
Test-Connection : Testing connection to computer ‘208.67.222.222‘ failed: Error due to lack of resources
At line:1 char:1
+ Test-Connection 208.67.222.222 -Count 100
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ResourceUnavailable: (208.67.222.222:String) [Test-Connection], PingException
    + FullyQualifiedErrorId : TestConnectionException,Microsoft.PowerShell.Commands.TestConnectionCommand
 


win32_PingStatus, 这种方式相较上一种而言更友好, 对于丢包不会报错, 仅仅是不显示Time(ms).
$end = [datetime]"2015年4月27日 20:32:20"
$start = Get-Date
$count = [math]::Round(($end - $start).TotalSeconds)
While ($Count -gt 0) {
Get-WmiObject -Class win32_PingStatus -Filter "address=‘208.67.22.220‘" | select address, responsetime, @{n="timestamp";e={Get-Date}}, @{n="status";e={if($_.responsetime -eq $null ) { "unreachable"}}} | Export-Csv -Path "C:\Users\814072\Desktop\result.csv" -Append
$count--
}
Source        Destination     IPV4Address      IPV6Address                              Bytes    Time(ms)
------        -----------     -----------      -----------                              -----    --------
CNHZPD-4GF... 208.67.22.220                                                             32                

参考: http://community.spiceworks.com/topic/337701-ping-via-powershell-log-results-with-timestamp

检测网络连通性并记录时间戳

原文:http://www.cnblogs.com/IvanChen/p/4493674.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!