I'm trying to find the magical answer to the "what's the primary IP address" of a system. My research has determined that the answer to "best" means this:
- Look at [system.Net.NetworkInformation.NetworkInterface]::GetAllNetworkInterfaces() for interfaces with default gateways assigned. If you only find one, then you are done.
- If there's more than one, you have to look at the routing table for the one with the lowest metric (look at the 0.0.0.0 network entries).
- In case of a tie in the metric, the adapter with the lowest binding order wins.
My question is: how do you determine the binding order using .NET? I've seen a solution that looks at the registry and that's alright, but I was hoping for a prettier interface.
Bonus points for PowerShell samples as that's my language of choice. :)