function GetTraceRoute($hostname) {
function __TracerRouteResult($Address,$Status, $TTL) {
[PSCustomObject]@{Address=$Address;Status=$Status;TTL=$TTL}
}
$timeout = 10000;
$maxTTL = 30;
$bufferSize = 32;
# ---------------------------------------------
$buffer = [byte[]]@(1..32)
[Random]::new().NextBytes($buffer)
$pinger = [System.Net.NetworkInformation.Ping]::new()
for ($ttl = 1; $ttl -le $maxTTL; $ttl++) {
$options = [System.Net.NetworkInformation.PingOptions]::new($ttl, $true)
$reply = $pinger.Send($hostname, $timeout, $buffer, $options)
if ($reply.Status -eq [System.Net.NetworkInformation.IPStatus]::Success) {
# Success means the tracert has completed
Write-Output (__TracerRouteResult $reply.Address ([System.Net.NetworkInformation.IPStatus]::Success) $ttl)
return
} elseif ($reply.Status -eq [System.Net.NetworkInformation.IPStatus]::TtlExpired) {
# TtlExpired means we‘ve found an address, but there are more addresses
Write-Output (__TracerRouteResult $reply.Address ([System.Net.NetworkInformation.IPStatus]::TtlExpired) $ttl)
} elseif ($reply.Status -eq [System.Net.NetworkInformation.IPStatus]::TimedOut) {
Write-Output (__TracerRouteResult ‘‘ ([System.Net.NetworkInformation.IPStatus]::TimedOut) $ttl)
} else {
break
}
}
}