Sunday, May 06, 2018

PowerShell Effective Route Table Lookup

Looking up the routes on a windows host via PowerShell can be misleading; at work (www.appliedi.net) our principal use is to see not just normal destination next-hops associated with actual adapters, but moreso those of SSL-VPN connections.  A Disclaimer: what follows is ugly, and while I applaud the ability to derive this information from a single PowerShell statement, I abhor the statement itself and Microsoft's continued inability to to easily provide what is universally needed. With that chiding disclaimer, here ya go:

Get-NetIpInterface -ConnectionState Connected |Where-Object -FilterScript {$_.InterfaceAlias -notmatch "^Lo.*"}|Select-Object -Unique -Property ifIndex|Get-NetRoute|Where-Object -FilterScript {$_.NextHop -notmatch "((0`.0`.0`.0)|::)"}|Format-Table -Property @{L='Destination';E='DestinationPrefix'}, @{L='Next Hop';E='NextHop'},@{L='Interface';E='InterfaceAlias'}


We grab routes from the IP Interfaces they originate from, ferreting out the useless usual suspects with output to human-readable-non-CamelCase output. Here's an example:

Destination       Next Hop    Interface
-----------       --------    ---------
174.136.79.138/32 192.168.1.1 Wi-Fi 4
0.0.0.0/0         192.168.1.1 Wi-Fi 4
216.167.192.0/20  10.95.0.8   fortissl
192.168.75.0/24   10.95.0.8   fortissl
174.136.88.0/21   10.95.0.8   fortissl
174.136.86.0/23   10.95.0.8   fortissl
174.136.85.0/24   10.95.0.8   fortissl
174.136.84.224/27 10.95.0.8   fortissl

Enjoy!