Is my computer connected to the Internet

When I wrote my second (draft) article entitled How to view and restore hidden Windows Updates with PowerShell on www.powershellmagazine.com I included the following code in a function to test whether the computer was connected to the Internet before initiating a scan.

    Function Test-ConnectedToInternet {
        [Activator]::CreateInstance(
            [Type]::GetTypeFromCLSID(
                [Guid]'{DCB00C01-570F-4A9B-8D69-199FDBA5723B}'
            )
        ).IsConnectedToInternet
    }

It raised some legitimate questions and of course I learned new things I’d like to share 🙂

The first question was about how the Windows Update Agent is configured. In some locked-down environments, computers/servers might not be connected directly to Internet and the WUA is configured to use an internal WSUS server. The function I proposed works with WSUS but would have failed because of the above test I planned to initially add. It was actually wiser not to test whether the computer is connected to the Internet. In other words, a scan can be performed against WSUS even if the computer hasn’t access to the Internet.

The second question was about the ‘IsConnectedToInternet’ property.
The property I’m testing above is the one attached directly to the Network List Manager object.

The same property also exists for all the network interfaces present on the computer.

$nlm = [Activator]::CreateInstance([Type]::GetTypeFromCLSID([Guid]"{DCB00C01-570F-4A9B-8D69-199FDBA5723B}"))
$nlm.GetNetworkConnections() | ForEach-Object { 
    [PSCustomObject]@{
        NetworkName = ($_.GetNetwork().GetName());
        isConnectedToInternet = $_.isConnectedToInternet;
    }
} | Format-Table -AutoSize 


So the property directly attached to the Network List Manager ( $nlm.isConnectedToInternet ) is somehow a global property set to summarize what was found per interface. If you’ve multiple interfaces and at least one has access to the Internet, then your computer has access to Internet.

Bonus:
On Windows 8.1, I’ve also got a new cmdlet function Get-NetConnectionProfile that will do the job.

Get-NetConnectionProfile -IPv4Connectivity Internet


Behind the scene this new cmdlet function queries and presents more nicely what you can find in the WMI repository by executing

Get-WmiObject -Namespace root/StandardCimv2 -Class MSFT_NetConnectionProfile

What is exactly the IPv4connectivity property returned by the Get-NetConnectionProfile cmdlet?
It’s easy to get its definition like this,

Get-NetConnectionProfile | Select -First 1 |            
Get-Member | Where Name -eq "IPv4Connectivity" |            
Select -ExpandProperty Definition
System.Object IPv4Connectivity {get=[Microsoft.PowerShell.Cmdletization.GeneratedTypes.NetConnectionProfile.IPv4Connectivity]($this.PSBase.CimInstanceProperties['IPv4Connectivity'].Value);set=param($newValue)
          $this.PSBase.CimInstanceProperties['IPv4Connectivity'].Value = [System.UInt32][Microsoft.PowerShell.Cmdletization.GeneratedTypes.NetConnectionProfile.IPv4Connectivity]$newValue;}

One can discover the translation being operated between numeric values and its literal definition like this:

0..4| % {            
'{0}>{1}'-f $_,            
[Microsoft.PowerShell.Cmdletization.GeneratedTypes.NetConnectionProfile.IPv4Connectivity]$_            
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s