如何从powershell获取默认网关? [英] How to get the default gateway from powershell?
问题描述
如果一台计算机有多个网关,我如何使用 PowerShell 确定哪个是默认网关?
If a computer has multiple gateways, how can I determine which is the default gateway using the PowerShell?
推荐答案
您需要知道使用了多个网关中的哪一个?如果是这样的话.据我所知,当有多个网关可用时,使用具有最低指标(基于链接速度的成本")的网关.要获得此信息,请运行以下命令:
You need to know which of the multiple gateways are used? If so. From what I remember, when multiple gateways are available the gateway with the lowest metric("cost" based on link speed) is used. To get this, run the following command:
Get-WmiObject -Class Win32_IP4RouteTable |
where { $_.destination -eq '0.0.0.0' -and $_.mask -eq '0.0.0.0'} |
Sort-Object metric1 | select nexthop, metric1, interfaceindex
如果有多个相同成本的默认网关,我认为是使用网络适配器的绑定顺序决定的.我知道的唯一方法是使用 GUI 和注册表.要包含绑定顺序,您可以保存脚本的输出,从 Win32_networkadapterconfiguration(使用 interfaceindex 识别)获取 settingsid,并读取注册表项 HKEY_LOCAL_MACHINE\System\CurrentControlSet\Services\Tcpip\Linkage\Bind.这个键列出了它看起来的绑定顺序,你从 win32_networkadapterconfiguration 获得的 settingsid 是它们用来标识设备的 GUID.然后使用它们在 Bind reg.key 中的顺序对具有相同指标的网关进行排序,您就得到了答案.
if there are multiple default gateways with the same cost, I think it's decided using the binding order of the network adapters. The only way I know to get this is using GUI and registry. To include binding order you could save the output of the script over, get the settingsid from Win32_networkadapterconfiguration (identify using interfaceindex), and read the registry key HKEY_LOCAL_MACHINE\System\CurrentControlSet\Services\Tcpip\Linkage\Bind. This key lists the binding order it seems, and the settingsid you get from win32_networkadapterconfiguration is the GUID they identify the device with. Then sort the gateways with equal metrics using their order in the Bind reg.key and you got your answer.
解释在:Technet Social - NIC 适配器绑定
这篇关于如何从powershell获取默认网关?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!