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.
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