PoshCode Logo PowerShell Code Repository

Trace-Route by halr9000 7 months ago
embed code: <script type="text/javascript" src="http://PoshCode.org/embed/3015"></script>download | new post

Posting on behalf of James Brundage of http://blog.start-automating.com/. Here’s his quote from email:
“I mentioned I wrote a Trace Route wrapper that shows an example of wrapping a command that returns over a period of time.”

Look to his blog for further detail on this technique soon.

  1. function Trace-Route
  2. {
  3.    param(
  4.    # The URL to trace
  5.    [Parameter(Mandatory=$true)]
  6.    [Uri]$Url,
  7.    # The timeout for the request, in milliseconds
  8.    [Timespan]$Timeout = "0:0:0.25",
  9.    # The maximum number of hops for the trace route
  10.    [Int]$MaximumHops = 32
  11.    )
  12.  
  13.    process {
  14.        Invoke-Expression "tracert -w $($timeOut.TotalMilliseconds) -h $MaximumHops $url" |
  15.            Where-Object {
  16.                if ($_ -match "[.+]") {
  17.                    $destination
  18.                    try {
  19.                        $destination = [IpAddress]$_.Split("[]",[StringSplitOptions]"RemoveEmptyEntries")[-1]
  20.                    } catch {
  21.                        return $false
  22.                    }
  23.                }
  24.                $true
  25.            } |
  26.            Where-Object {
  27.                if ($_ -like "*Request timed out.*") {
  28.                    throw "Request timed Out"
  29.                }
  30.                return $true
  31.            } |
  32.            Foreach-Object {
  33.                if ($_ -like "*ms*" ) {
  34.                    $chunks = $_ -split "  " | Where-Object { $_ }
  35.                    $destAndip = $chunks[-1]
  36.                    $dest, $ip = $destAndip.Replace("[", "").Replace("]","") -split " "
  37.  
  38.                    if (-not $ip) {
  39.                        $ip = $dest
  40.                        $dest = ""
  41.                    }
  42.  
  43.                    $ip = @($ip)[0].Trim() -as [IPAddress]
  44.  
  45.  
  46.                    if ($chunks[1] -eq '*' -and $chunks[2] -eq '*' -and $chunks[3] -eq '*') {
  47.                        Write-Error "Request Timed Out"
  48.                        return
  49.                    }
  50.                    $trace = New-Object Object
  51.                    $time1 = try { [Timespan]::FromMilliseconds($chunks[1].Replace("<","").Replace(" ms", ""))} catch {}
  52.                    $time2 = try { [Timespan]::FromMilliseconds($chunks[1].Replace("<","").Replace(" ms", ""))} catch {}
  53.                    $time3 = try { [Timespan]::FromMilliseconds($chunks[1].Replace("<","").Replace(" ms", ""))} catch {}
  54.                    $trace |
  55.                        Add-Member NoteProperty HopNumber ($chunks[0].Trim() -as [uint32]) -PassThru |
  56.                        Add-Member NoteProperty Time1 $time1 -PassThru |
  57.                        Add-Member NoteProperty Time2 $time2 -PassThru |
  58.                        Add-Member NoteProperty Time3 $time3 -PassThru |
  59.                        Add-Member NoteProperty Ip $ip -PassThru |
  60.                        Add-Member NoteProperty Host $dest -PassThru |
  61.                        Add-Member NoteProperty DestinationUrl $url -PassThru |
  62.                        Add-Member NoteProperty DestinationIP $destination -PassThru
  63.  
  64.                }
  65.            }
  66.    }
  67. }

Submit a correction or amendment below (
click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:


Remember me