PowerShell / Windows脚本:获取包含指定网关IP的接口名称

时间:2013-07-04 03:13:37

标签: netsh

我正在尝试构建一个Windows批处理脚本,根据其网关IP捕获网络接口的名称,然后重命名该接口。我无法捕获接口名称,这是一个随机字符串。在Windows中,我可以通过运行管道netsh命令获取所需信息:

C:\Users\Administrator>netsh interface ipv4 dump | findstr "nexthop=2.2.2.1"

add route prefix=0.0.0.0/0 interface="Management" nexthop=2.2.2.1 publish=yes

如何解析输出中显示的接口字符串?

我还尝试使用以下命令的组合在PowerShell中进行实验,但运气不佳。我希望得到任何帮助,或者朝着正确的方向努力。

 get-wmiObject -Query "select NetConnectionID from Win32_NetworkAdapter"
 get-wmiObject -Query "select DefaultIPgateway from Win32_NetworkAdapterConfiguration"
编辑:我已经开始尝试使用wmic了。鉴于DefaultIPGateway是nicConfig的一部分,而NetConnectionID是nic的一部分,无论如何我可以使用它吗?

wmic nic where "DefaultIPgateway=2.2.2.1" get NetConnectionID

1 个答案:

答案 0 :(得分:0)

也许您可以使用InterfaceIndex链接这些对象:

get-wmiObject -Query "select InterfaceIndex from Win32_NetworkAdapter"
get-wmiObject -Query "select InterfaceIndex from Win32_NetworkAdapterConfiguration"

InterfaceIndex 数据类型:sint32 访问类型:读/写 该路由下一跳的IP地址。此属性中的值与Win32_NetworkAdapter和Win32_NetworkAdapterConfiguration实例中InterfaceIndex属性中的值相同,后者表示路由下一跳的网络接口。 在绑定到使用广播媒体实现的接口的路由的情况下,该字段的值是该接口上的代理IP地址。 来自:http://msdn.microsoft.com/en-us/library/windows/desktop/aa394162(v=vs.85).aspx

的描述

类似这样的事情

$index = get-wmiObject -Class Win32_NetworkAdapterConfiguration | Where-Object { ($_.DefaultIPGateway -eq "2.2.2.1") } | Select-object InterfaceIndex
get-wmiObject -Class Win32_NetworkAdapter | Where-Object { ($_.InterfaceIndex -eq $index.InterfaceIndex) } | Select NetConnectionId
相关问题