Is there a PowerShell equivalent tracert that works in version 2?

Sylca picture Sylca · Sep 7, 2015 · Viewed 28.6k times · Source

I'm using PSVersion 2.0 and I was wondering is there a equivalent to the traceroute for it?

I'm aware that on PowerShell v4 there is Test-NetConnection cmdlet to do tracert but v2?! It can be done like:

Test-NetConnection "IPaddress/HOSTaname" -TraceRoute

Thanks

Answer

Mathias R. Jessen picture Mathias R. Jessen · Sep 7, 2015

As mentioned in the comment, you can make your own "poor-mans-PowerShell-tracert" by parsing the output from tracert.exe:

function Invoke-Tracert {
    param([string]$RemoteHost)

    tracert $RemoteHost |ForEach-Object{
        if($_.Trim() -match "Tracing route to .*") {
            Write-Host $_ -ForegroundColor Green
        } elseif ($_.Trim() -match "^\d{1,2}\s+") {
            $n,$a1,$a2,$a3,$target,$null = $_.Trim()-split"\s{2,}"
            $Properties = @{
                Hop    = $n;
                First  = $a1;
                Second = $a2;
                Third  = $a3;
                Node   = $target
            }
            New-Object psobject -Property $Properties
        }
    }
}

By default, powershell formats objects with 5 or more properties in a list, but you can get a tracert-like output with Format-Table:

enter image description here