PowerShell实战1:Ping_Test
生活随笔
收集整理的這篇文章主要介紹了
PowerShell实战1:Ping_Test
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
功能:批量測試遠程主機的Ping值,以及根據TTL值來判斷是否為Windows主機。
使用:在C:\IP.txt中加入需要測試的主機IP或域名,一行一個。例如: www.google.com
www.baidu.com
www.cha123.com
www.yahoo.com
www.msn.com 源碼: function Ping_Test {
??? PROCESS {
????? $ping = $false
????? $results = Get-WmiObject -query `
????? "SELECT * FROM Win32_PingStatus WHERE Address = '$_'"
????? $RT = $results.ResponseTime
????? $TTL = $results.ResponseTimeToLive
????? foreach ($result in $results) {
??????? if ($results.StatusCode -eq 0) {
??????????? if ($TTL -ge 98 -and $TTL -le 128)
??????????????? {Write-Host "`n$_ Response Time=$RT ms, TTL=$TTL,It is a Windows host." -ForegroundColor Green }
????????????? else
????????????????? {Write-Host "`n$_ Response Time=$RT ms, TTL=$TTL, It is NOT a Windows host." -ForegroundColor Blue}
????????? }
??????? else {
????????? Write-Host "`n$_ Ping failed!" -ForegroundColor Red
??????? }
??????? }
??? }
? }
cls
Get-Content c:\IP.txt | Ping_Test 結果:
使用:在C:\IP.txt中加入需要測試的主機IP或域名,一行一個。例如: www.google.com
www.baidu.com
www.cha123.com
www.yahoo.com
www.msn.com 源碼: function Ping_Test {
??? PROCESS {
????? $ping = $false
????? $results = Get-WmiObject -query `
????? "SELECT * FROM Win32_PingStatus WHERE Address = '$_'"
????? $RT = $results.ResponseTime
????? $TTL = $results.ResponseTimeToLive
????? foreach ($result in $results) {
??????? if ($results.StatusCode -eq 0) {
??????????? if ($TTL -ge 98 -and $TTL -le 128)
??????????????? {Write-Host "`n$_ Response Time=$RT ms, TTL=$TTL,It is a Windows host." -ForegroundColor Green }
????????????? else
????????????????? {Write-Host "`n$_ Response Time=$RT ms, TTL=$TTL, It is NOT a Windows host." -ForegroundColor Blue}
????????? }
??????? else {
????????? Write-Host "`n$_ Ping failed!" -ForegroundColor Red
??????? }
??????? }
??? }
? }
cls
Get-Content c:\IP.txt | Ping_Test 結果:
轉載于:https://blog.51cto.com/jackiechen/202580
總結
以上是生活随笔為你收集整理的PowerShell实战1:Ping_Test的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 梦到抓了好几条大鱼
- 下一篇: 梦到给人借钱是怎么回事