2

Is there some way in PowerShell to automatically determine which of multiple NICs has Internet access rather than an connection to a closed network?

Say, a computer has one NIC that can generally get out to the Web and another NIC that is connected to a small, scientific device network. They both have IP addresses and see a "network". Is there a generic way to determine which is Internet connected?

I know about:

gwmi win32_networkadapterconfiguration -filter "ipenabled = 'true'"

and

gwmi win32_networkadapter -filter "NetConnectionStatus LIKE '2'"

and

Get-NetAdapter | ? {($_.status -eq 'up')

but it's not clear to me that those filters are identifying Internet access vs just network access, and unfortunately, I don't have access to a machine with that kind of setup to test.

Thanks.

3

The Test-Connection cmdlet allows you to specify the source address with the -Source parameter. We can test the addresses being used by each adapter.

$adapters = Get-NetIPAddress -AddressFamily ipv4 | Where-Object InterfaceAlias -in (Get-NetAdapter | Select-Object -ExpandProperty Name) | Select-Object IPAddress,InterfaceAlias

$adapters | % {

    Test-Connection -Source $_.IPAddress -Destination 8.8.8.8 -ErrorAction SilentlyContinue | Out-Null

    if($?) {
        write-host $_.InterfaceAlias "(" $_.IPAddress ") can connect to the internet."
    }
    else {
        write-host $_.InterfaceAlias "(" $_.IPAddress ") failed to connect to the internet."
    }

}

Output:

Local Area Connection ( 10.1.1.2 ) can connect to the internet
Ethernet 2 ( 10.1.1.3 ) failed to connect to the internet.
Wireless Network Connection ( 10.1.1.4 ) failed to connect to the internet.
| improve this answer | |
  • I didn't realize test-connection had a -source option. That is exactly what I was looking for! Unfortunately, -source and -quiet cannot both be used. – Teknowledgist Nov 3 '17 at 16:36
0

I believe that the best way would be to check which adapter the default route use.

Get-NetRoute -DestinationPrefix "0.0.0.0/0"

This would give you the ifIndex for the adapter that should be connected to the internet. The same ifIndex can be used to get the adapter...

Get-NetAdapter -InterfaceIndex <number from previous command>
| improve this answer | |
  • Are you certain that default route can't be assigned to NIC that isn't connected to internet? I faced issue with that in Linux previously, default route was assigned based upon which NIC was enabled first. I suppose Windows might have it handled... – Marek Rost Nov 3 '17 at 14:31
  • Well, the default route is where all traffic goes, unless it is specified in the routing table.. So if the default route points to an internal network, you won't have access to the internet at all.. :) (There might be several default routes, though.. Just different priorities) – Magnus Nov 3 '17 at 14:39
  • 1
    You could also use something like Find-NetRoute -RemoteIPAddress 8.8.8.8 to get the index of the interface that would route the traffic to that IP. – Magnus Nov 3 '17 at 14:44
  • My ultimate goal is to disable Internet access while leaving local network connections alone. Once I disable the default route NIC, the next NIC becomes default (I assume). Although it would be rare, I can't assume only two NICs (one online, and one local), so I have to iterate through all the "up" status NICs. – Teknowledgist Nov 3 '17 at 16:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.