检测是否在 Windows 机器上启用了 IPv6

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2083412/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 07:03:59  来源:igfitidea点击:

Detect If IPv6 is Enabled on Windows Machines

windowspowershellnetwork-programmingpowershell-2.0ipv6

提问by Ben Short

I am writing a powershell script that will act as a build compliance test for our servers. One of the things I need to do is detect if IPv6 networking has been disabled.

我正在编写一个 powershell 脚本,它将作为我们服务器的构建合规性测试。我需要做的一件事是检测 IPv6 网络是否已被禁用。

WMI indicates that this information can be found in the IPAddress Property of Win32_NetworkAdapterConfiguration but can be both IPv6 or IPv4. This does not give me a "yes/no" answer I am hoping to find.

WMI 指示可以在 Win32_NetworkAdapterConfiguration 的 IPAddress 属性中找到此信息,但可以是 IPv6 或 IPv4。这并没有给我一个我希望找到的“是/否”答案。

Other caveats are that I would prefer not to scrape the details by accessing the registry directly, nor scrape from the output of a command such as ipconfig.

其他注意事项是,我不希望通过直接访问注册表来获取详细信息,也不想从诸如 ipconfig 之类的命令的输出中获取详细信息。

Given our environment has a mix of 2003/2008 machines, can anyone think of a way to test for IPv6?

鉴于我们的环境混合了 2003/2008 机器,有人能想出一种测试 IPv6 的方法吗?

Cheers

干杯

回答by Jerome Laban

You can use the .NET way :

您可以使用 .NET 方式:

Write-Host 'OS Supports IPv6: ' $( [System.Net.Sockets.Socket]::OSSupportsIPv6 )

The property will be true if it is possible to create an IPv6 datagram socket. This property is also affected by the global machine.config file, so the IPv6 status may not always be accurate.

如果可以创建 IPv6 数据报套接字,则该属性为 true。此属性还受全局 machine.config 文件的影响,因此 IPv6 状态可能并不总是准确的。

Edit: To test this on a remote machine, you can still use powershell, but it has to have powershell 2.0 installed, and WinRM enabled.

编辑:要在远程计算机上进行测试,您仍然可以使用 powershell,但必须安装 powershell 2.0 并启用 WinRM。

If those two conditions are true, then you can probably use Invoke-Commandto do that.

如果这两个条件都为真,那么您可能可以使用Invoke-Command来做到这一点。

回答by Steve-o

Testing for IPv6 would usually be more complicated than a yes/no answer with XP you have,

测试 IPv6 通常比使用 XP 回答是/否更复杂,

  1. Is the "Microsoft TCP/IP version 6" network component installed.
  2. Is there an adapter has an IPv6 address - one would usually assume (1) implies (2).
  3. Whether an adapter has a global-scope IPv6, i.e. not just a link local fe80:: prefixed address or loopback interface, ::1.
  1. 是否安装了“ Microsoft TCP/IP 版本 6”网络组件。
  2. 是否有适配器具有 IPv6 地址 - 人们通常会假设 (1) 暗示 (2)。
  3. 适配器是否具有全局范围的 IPv6,即不仅仅是链接本地 fe80:: 前缀地址或环回接口 ::1。

Presumaly (1) can be found with a Powershell script as demonstrated by Microsoft:

假设 (1) 可以通过 Microsoft 演示的 Powershell 脚本找到:

http://gallery.technet.microsoft.com/ScriptCenter/en-us/c4eb1596-7ac9-4be7-a711-b43446a3e3df

http://gallery.technet.microsoft.com/ScriptCenter/en-us/c4eb1596-7ac9-4be7-a711-b43446a3e3df

回答by Ben Short

Actually, a colleague came up with a nice way of doing it - a little clunky but works. :)

实际上,一位同事想出了一个很好的方法 - 有点笨拙但有效。:)

$IPV6 = $false
$arrInterfaces = (Get-WmiObject -class Win32_NetworkAdapterConfiguration -filter "ipenabled = TRUE").IPAddress

foreach ($i in $arrInterfaces) {$IPV6 = $IPV6 -or $i.contains(":")}

write-host $IPV6

回答by Dolda2000

If you want to specifically check that IPv6 is enabled and working, I'd say the best way would be to try and connect to a test host. That way, you won't just test some specific software setting, but you'll also find out so that all the routing configuration and everything around it works as it should.

如果您想专门检查 IPv6 是否已启用并正常工作,我认为最好的方法是尝试连接到测试主机。这样,您不仅会测试某些特定的软件设置,还会发现所有路由配置及其周围的一切都可以正常工作。

回答by Nick Craver

This is what you didn't want to do, but it seems the best method, going over the output that netsh interfaceprovides.

这是您不想做的,但它似乎是最好的方法,检查netsh 接口提供的输出。

You may want to look here for what you're after. Your implementation would be simpler, but iterating over the netsh interface ipv6 show interfacesresults is all you need, if you find interfaces, it's enabled somewhere, if you don't find any, it's not.

您可能想在这里寻找您想要的东西。你的实现会更简单,但迭代netsh interface ipv6 show interfaces结果就是你所需要的,如果你找到接口,它在某处启用,如果你没有找到,它不是。

回答by Kleinux

I've not tested this, but the .NET Socket class has a OSSupportsIPv6 property. So something along the lines

我没有测试过这个,但是 .NET Socket 类有一个 OSSupportsIPv6 属性。所以沿着这条线

[System.Net.Sockets]::OSSupportsIPv6

The documentation says "true if the operating system and network adaptors support the IPv6 protocol; otherwise, false." It isn't clear to me if that means the value is based on what the OS is capable of, or what it is configured for.

文档说“如果操作系统和网络适配器支持 IPv6 协议,则为 true;否则为 false。” 我不清楚这是否意味着该值是基于操作系统的功能或配置的。

I looked this up in my local help, so I don't have a MSDN link handy. Shouldn't be too hard to find.

我在本地帮助中查找了这个,所以我没有方便的 MSDN 链接。应该不难找。

回答by ComputingDon

I know this is an old thread but I needed to check the same thing.

我知道这是一个旧线程,但我需要检查相同的内容。

To check ipv6 binding for a specific adapter use:

要检查特定适配器的 ipv6 绑定,请使用:

 Get-NetAdapterBinding -Name 'NameOfNic' -ComponentID ms_tcpip6

This will give you the enabled state for that adapter.

这将为您提供该适配器的启用状态。

If you want to just retrieve that state you can use:

如果您只想检索该状态,您可以使用:

 (Get-NetAdapterBinding -Name 'NameOfNic' -ComponentID ms_tcpip6).Enabled

Hope this helps. I did check that this works as far back as 2012. docs.microsoft.com didn't list versions below default for 2012/Win8

希望这可以帮助。我确实检查过这可以追溯到 2012 年。docs.microsoft.com 没有列出低于 2012/Win8 默认值的版本