Categories

WMICookbook: Read Routing Table

When we need to troubleshoot networking problems we will sometimes need to read the routing table on a machine. The routing table contains the information on the routes known to the network interfaces. This can be created automatically or manually . On the local machine we can use the route command to find this information – but how do we find it on a remote machine. WMI has a class that enables us to read the routing table.

001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
function Get-RouteTable {
param (
    [parameter(ValueFromPipeline=$true)]
    [string]$computer="."
)

## create class for object
$source=@"
public class WmiIPRoute
{
    private string _destination;
    private string _mask;
    private string _nexthop;
    private string _interface;
    private int _metric;
   
     public string Destination {
        get {return _destination;}
        set {_destination = value;}
    }
   
    public string Mask {
        get {return _mask;}
        set {_mask = value;}
    }
   
    public string NextHop {
        get {return _nexthop;}
        set {_nexthop = value;}
    }
   
    public string Interface {
        get {return _interface;}
        set {_interface = value;}
    }
   
    public int Metric {
        get {return _metric;}
        set {_metric = value;}
    }
}
"@

Add-Type -TypeDefinition $source

    $data = @()
    Get-WmiObject -Class Win32_IP4RouteTable -ComputerName $computer| foreach {
        $route = New-Object -TypeName WmiIPRoute
        $route.Destination = $_.Destination
        $route.Mask        = $_.Mask
        $route.NextHop     = $_.NextHop
        $route.Metric      = $_.Metric1
       
        $filt = "InterfaceIndex='" + $_.InterfaceIndex + "'" 
        $ip = (Get-WmiObject -Class Win32_NetworkAdapterConfiguration -Filter $filt -ComputerName $computer).IPAddress

        if ($_.InterfaceIndex -eq 1) {$route.Interface = "127.0.0.1"}
        elseif ($ip.length -eq 2){$route.Interface = $ip[0]}
        else {$route.Interface = $ip}
       
        $data += $route
    }
    $data | Format-Table -AutoSize 
}

 

Our function takes a single parameter – a computer name (or IP address) I’ve used the advanced function parameters to this function operates on the pipeline.  We then create a .NET class to hold our data – we will be accessing a couple of WMI classes so we’ll make the presentation neat.  The class is added using Add-Type.

As an aside I really like this technique for collecting data together into a single object.  Its neater and easier to use than Add-Member.

We can then use Get-WmiObject -Class Win32_IP4RouteTable -ComputerName $computer to retrieve the routing information. We create an instance of our object and populate the properties.  One thing we need to know is the Interface ie which address on our machine is using this route,  We can find this from the Win32_NetworkAdapterConfiguration  class.  There isn’t an association but we can find the address by using the InterfaceIndex as a filter – its the same value in both classes.  if the InterfaceIndex = 1 its the Loopback Adapter on 127.0.0.1

We can then add our route to the data. When all the routes are collected we can display the data.  The data could be output onto the pipeline but at the moment I can’t think what else to do with it so we’ll leave it like this for now.

Note: Win32_IP4RouteTable is only available on Windows 2003 and later

One Response to WMICookbook: Read Routing Table

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>