I was working on a troubleshooting module and start to think about how I’d go about testing internet connectivity.
First you need to determine the network adapter that’s connected to the internet. You can use the network connection profile to discover the network that’s connected to the internet:
PS> Get-NetConnectionProfile -IPv4Connectivity Internet Name : Network 2 InterfaceAlias : vEthernet (Wireless) InterfaceIndex : 27 NetworkCategory : Private IPv4Connectivity : Internet IPv6Connectivity : NoTraffic
You then need to discover the adapter that’s supplying that connection:
PS> Get-NetConnectionProfile -IPv4Connectivity Internet | Get-NetAdapter | select Name, ifindex, Status Name ifIndex Status ---- ------- ------ vEthernet (Wireless) 27 Up
The IP associated with that adapter:
PS> Get-NetConnectionProfile -IPv4Connectivity Internet | Get-NetIPAddress -AddressFamily IPv4 IPAddress : 192.168.1.172 InterfaceIndex : 27 InterfaceAlias : vEthernet (Wireless) AddressFamily : IPv4 Type : Unicast PrefixLength : 24 PrefixOrigin : Dhcp SuffixOrigin : Dhcp AddressState : Preferred ValidLifetime : 06:10:59 PreferredLifetime : 06:10:59 SkipAsSource : False PolicyStore : ActiveStore
And finally the default gateway and DNS server
PS> Get-NetConnectionProfile -IPv4Connectivity Internet | Get-NetIPConfiguration InterfaceAlias : vEthernet (Wireless) InterfaceIndex : 27 InterfaceDescription : Hyper-V Virtual Ethernet Adapter #2 NetProfile.Name : Network 2 IPv4Address : 192.168.1.172 IPv6DefaultGateway : IPv4DefaultGateway : 192.168.1.1 DNSServer : 192.168.1.1
So lets put all of that together:
function get-internetconnection { $prf = Get-NetConnectionProfile -IPv4Connectivity Internet $nic = $prf | Get-NetAdapter $ip = $prf | Get-NetIPAddress -AddressFamily IPv4 $dg = $prf | Get-NetIPConfiguration $props = [ordered]@{ NetworkName = $prf.Name NetConnection = $prf.IPv4Connectivity iIndex = $prf.InterfaceIndex iAlias = $prf.InterfaceAlias Status = if ($nic.InterfaceOperationalStatus -eq 1){'Up'}else{'Down'} IPAddress = $ip.IPAddress PrefixLength = $ip.PrefixLength DefaultGateway = $dg.IPv4DefaultGateway | select -ExpandProperty NextHop DNSserver = ($dg.DNSServer).serverAddresses } New-Object -TypeName PSobject -Property $props }
This results in:
PS> get-internetconnection NetworkName : Network 2 NetConnection : Internet iIndex : 27 iAlias : vEthernet (Wireless) Status : Up IPAddress : 192.168.1.172 PrefixLength : 24 DefaultGateway : 192.168.1.1 DNSserver : 192.168.1.1
I’ve now got an object I can work with to run some tests which I’ll show you how to do next time.