question

oggy-9213 avatar image
0 Votes"
oggy-9213 asked RichMatheisen-8856 answered

Sort-Object ascending

Hi Everyone

I want to print routing table in windows and sort the metric value ascending
I tried something like

route print | Select-String -Pattern " IPv4 | Metric " -Context 1,50 | Sort-Object -Unique

the above is just example where I want to take IPv4 routing table and sort the Metric value ascending

Can anyone help me to figure out the correct syntax ?

Thanks

windows-server-powershell
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

LeonLaude avatar image
0 Votes"
LeonLaude answered LeonLaude commented

Hi @oggy-9213,

You could try using the PowerShell equivalent for the route command instead, Get-NetRoute.
Here's an example:

Get-NetRoute | Sort-Object -Property InterfaceMetric

Example:

102703-get-netroute.png


(If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)


Best regards,
Leon



get-netroute.png (28.5 KiB)
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thank you LeonLaude , this is not what I'm looking for

the getnetroute output is rather different than route print as it doesn't the interface of the route

IPv4 Route Table


IPv4 Route Table
=======================================================================102761-image.png

from the above output , I want to sort metric of 1


0 Votes 0 ·
image.png (42.1 KiB)

I’m not familiar with the cmdlet but you can check play around with the cmdlet and see if the interface parameter exists.

1 Vote 1 ·
RichMatheisen-8856 avatar image
0 Votes"
RichMatheisen-8856 answered

You can "roll your own" set of data, too:

 $r = [ordered]@{
     "Network Destination" = ""
     Netmask = ""
     Gateway = ""
     Interface = ""
     RouteMetric = ""
     InterfaceMetric = ""
 }
 Get-NetRoute -AddressFamily IPv4 |
     ForEach-Object{
         $r.Interface = Get-NetIPAddress -InterfaceIndex $_.ifIndex -AddressFamily IPv4 
         $r.Gateway = (Get-NetIPConfiguration -InterfaceIndex $_.ifIndex).IPv4DefaultGateway.NextHop
         $r."Network Destination" = ($_.DestinationPrefix -split "/")[0]
         $cidr =($_.DestinationPrefix -split "/")[1]
         $bmask = ("1" * $cidr) + ("0" * (32 - $cidr))
         $bytes = @()
         for ($i = 0; $i -le 24; $i=$i+8){
             $bytes += [System.Convert]::ToInt16($bmask.substring($i,8),2)
         }
         $r.Netmask = $bytes -join "."
         $r.RouteMetric = $_.RouteMetric
         $r.InterfaceMetric = $_.InterfaceMetric
         [PSCustomObject]$r
     }
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.