开发者

How to retrieve vista's network status (e.g. "Local Only", "Local and Internet") in powershell

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!


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜