views:

140

answers:

2

I have a flaky NIC that drops out from time to time, especially after resuming from hibernation. A drop-out corresponds to Vista's network status showing in the notification area as "Local Only". Is there a way of retrieving these status values (e.g. "Limited Connectivity", "Local Only", "Local and Internet") programmatically?

I am writing a powershell script that polls to see if the connection is down, and if so, resets the adapter. Currently I am trying to detect the connection state by pinging my ISP's DNS server. However, since the OS is already correctly identifying this condition, it would be much simpler if I could just retrieve this value.

Thanks!

+1  A: 

Try this function:

PS> function Get-NetworkStatus {
  $t = [Type]::GetTypeFromCLSID([Guid]"{DCB00C01-570F-4A9B-8D69-199FDBA5723B}")
  $networkListManager = [Activator]::CreateInstance($t)
  $connections = $networkListManager.GetNetworkConnections() 
  function getconnectivity {
    param($network)
    switch ($network.GetConnectivity()) {
       0x0000  { 'disconnected' }
      { $_ -band 0x0001 } { 'IPV4_NOTRAFFIC' }
      { $_ -band 0x0002 } { 'IPV6_NOTRAFFIC' }
      { $_ -band 0x0010 } { 'IPV4_SUBNET' }
      { $_ -band 0x0020 } { 'IPV4_LOCALNETWORK' }
      { $_ -band 0x0040 } { 'IPV4_INTERNET' }
      { $_ -band 0x0100 } { 'IPV6_SUBNET' }
      { $_ -band 0x0200 } { 'IPV6_LOCALNETWORK' }
      { $_ -band 0x0400 } { 'IPV6_INTERNET' }
    }
  }
  $connections | 
    % { 
      $n = $_.GetNetwork(); 
      $name = $n.GetName();
      $category = switch($n.GetCategory()) { 0 { 'public' } 1 { 'private' } 2 { 'domain' } }
      $connectivity = getConnectivity $n
      new-object PsObject -property @{Name=$name; Category=$category; Connectivity=$connectivity } 
    }
}

PS> Get-NetworkStatus
Name                                 Connectivity                         Category
----                                 ------------                         --------
Neznámá síť                          {IPV4_NOTRAFFIC, IPV6_NOTRAFFIC}     public
stefan                               {IPV6_NOTRAFFIC, IPV4_INTERNET}      private

If you pipe $connections and output from GetNetwork() to Get-Member you will find some more useful methods.

stej
Thanks! This is exactly what I was after.
Andrew Strong
It was interesting question :)
stej
A: 

How to use this function? Thanks in advance

Dream