Quantcast
Channel: Network Audit and Documentation, CENTREL Solutions Blog
Viewing all articles
Browse latest Browse all 433

The PowerShell cmdlet Get-NetRoute returns more results than the standard "route print" command

$
0
0
We've been working to migrate our server documentation tool from a combination of WMI, and low level APIs to PowerShell and have come across an interesting issue.

Microsoft recommend using the PowerShell cmdlet Get-NetRoute as a replacement for the old "route print" command, but it returns more results than the standard "route print" command and the WMI class Win32_IP4RouteTable.

It turns out that the former returns routes for interfaces that are down whereas the later two methods do not. This is what leads to the inconsistency.

To ignore the results from network adapters that aren't up you need to correlate them with the results from the Get-NetAdapter cmdlet and check the ifOperStatus.



$routes=Get-NetRoute
$adapters=Get-NetAdapter
foreach ($routein$routes)
{
    $interfaceOnline=$true
    foreach($adapterin$adapters)
    {
        if($adapter.InterfaceIndex -eq$route.InterfaceIndex)
        {
            if([System.Convert]::ToInt32($adapter.ifOperStatus) -ne1) { $interfaceOnline=$false; }
        }
    }
    if (!$interfaceOnline) { continue; }
    $route
}


Viewing all articles
Browse latest Browse all 433

Trending Articles