powershell 强大的PowerShell ping错误检查:P

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了powershell 强大的PowerShell ping错误检查:P相关的知识,希望对你有一定的参考价值。

function Get-Ping ($url) {
 $timeout = 500
 $ping = new-object system.net.networkinformation.ping
 try {
    # The @() ensures the return if a single string is still an array
    $urlArray = @($url.split(' ') | where { $_ -ne "-" })
    #"urlArray = $urlArray"
    $url = $urlArray[0]
    #$url
    $ip = $urlArray[1]
    #$ip
  } catch { 
  # We don't care if no IP was found 
  }
 
   try {
     $response = $ping.Send($url,$timeout) | select address,status
     if ($ip) {
         if ($response.Address -ne $ip) {
            write-error "The returned address $($response.Address) didn't match the expected IP $ip"
            }
        }
     return "$url $($response.Status)"
     } catch {
        write-host "No reply from host, address $url appears to be available"
    }
 }
$error.clear()
$errorCount = $($Error.Count)

$result = foreach ($url in $urls) { Get-Ping $url }
#$result = foreach ($url in $urls) { ping -a -n 1 $url } # Useful if above gives "out of resources" aka sockets?
$result | ft -AutoSize

$newErrorCount = $($Error.Count)

if ($errorCount -ne $newErrorCount) {
    Write-Host "Some errors occurred, not all URLs ready"
    foreach ($error in $($newErrorCount - $errorCount)) {
    Write-Host "Most recent error was: $($Error[$error])"
    }
}

# This was developed for an internal tool to spit out DNS requests in a format
# the network team could pretty much copy and paste into their tools
Get-Ping "something.mydomain.com - 192.168.1.100 - SOMEHOSTNAME"

以上是关于powershell 强大的PowerShell ping错误检查:P的主要内容,如果未能解决你的问题,请参考以下文章

powershell和powershell ise到底分别干啥用,powershell命令那么长怎么记

powershell和cmd区别

Windows PowerShell 学习---第一章 PowerShell介绍

PowerShell扩展——PowerShell从零开始系列之三

使用PowerShell操作Windows服务的命令小结

powershell自启内存占用