如何在 powershell 中检索 vista 的网络状态(例如“仅限本地”、“本地和互联网”)

发布于 2024-08-24 22:09:07 字数 254 浏览 3 评论 0原文

我的网卡不稳定,有时会掉线,尤其是从休眠状态恢复后。退出对应于 Vista 的网络状态在通知区域中显示为“仅限本地”。有没有办法以编程方式检索这些状态值(例如“有限连接”、“仅限本地”、“本地和互联网”)?

我正在编写一个 powershell 脚本,用于轮询连接是否已关闭,如果是,则重置适配器。目前我正在尝试通过 ping 我的 ISP 的 DNS 服务器来检测连接状态。但是,由于操作系统已经正确识别了这种情况,因此如果我可以检索该值就会简单得多。

谢谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

久隐师 2024-08-31 22:09:07

尝试这个函数:

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

如果将 $connections 和输出从 GetNetwork() 传输到 Get-Member,您会发现一些更有用的方法。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文