按 IP 地址查找网卡



我需要能够通过IP地址找到NIC,无论是全部还是部分。所以写这样的东西:

Get-NIC-By-IP 10.10.*

可以返回:

Ethernet

我知道如何在Bash中做到这一点,但无法找到PowerShell解决方案。

对于不支持Get-NetIPAddress的Windows和/或PowerShell版本,您可以通过组合Win32_NetworkAdapterConfiguration类和Win32_NetworkAdapter类的WMI查询来获取必要的信息:

$Configs = Get-WMIObject -Class Win32_NetworkAdapterConfiguration -Filter "IPEnabled='TRUE'" | Where-Object {$_.IPAddress -like "192.168.*"}
ForEach ($Config in $Configs) {
Get-WMIObject -Class Win32_NetworkAdapter -Filter "Index=$($Config.Index)" | Select-Object NetConnectionID,Description
}

通过使用以下命令,您将收到与您在match子句中提到的 IP 地址匹配的每个接口。

Get-NetIPAddress | ?{ $_.AddressFamily -eq "IPv4" -and ($_.IPAddress -match "192.")} | Select-Object InterfaceAlias

就我而言,这是:

InterfaceAlias
--------------
Ethernet 2
Ethernet

当然,如有必要,您可以修改输出。

旧操作系统的补充

不能在旧的 Windows 浏览器上运行此脚本,因为根据 TechNet 上的此线程,该 cmdlet 不包含在内:https://social.technet.microsoft.com/Forums/office/en-US/dcc966a1-24c2-4ae4-b39d-b78df52b6aef/install-of-powershell-3-on-windows-7-seems-to-be-missing-modules?forum=winserverpowershell

Powershell for Windows 8 和 Server 2012 (PS V3) 中有许多 cmdlet 未包含在 Windows 7 的 V3 版本中。 一个例子是 Get-NetIPAddress 和许多其他与网络相关的 cmdlet。

再说一次,将操作系统升级到受支持的版本可能是个好主意(当然,如果可能的话)。

最新更新