2017-09-15 88 views
-1

我需要能夠通過IP地址找到網卡,無論是全部還是部分。所以寫的東西,如:按IP地址查找網卡

Get-NIC-By-IP 10.10.* 

可以返回:

Ethernet 

我知道如何做到這一點在Bash中,但一直沒能找到一個PowerShell解決這個。

+0

'GET-NetIPAddress -IPAddress 10.10 * |如果Get-NetIPAddress在所使用的版本上可用,請選擇InterfaceAlias,IPAddress'。 – VGSandz

回答

1

通過使用以下命令,您將收到與您在match子句中提及的IP地址相匹配的每個接口。

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

在我的情況是這樣的:

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

當然,你可以根據需要修改輸出。

補充舊OS'es

以根據此線索TechNet上的小命令不包括無法運行在老的Windows瀏覽器這個腳本: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中針對Windows 8和Server 2012(PS V3)的許多cmdlet未包含在Windows 7的V3版本中。例如,Get-NetIPAddress和許多其他與網絡相關的cmdlet。

然後,將操作系統升級到支持的版本(當然,如果可能的話)可能是一個好主意。

+0

Windows 7,PowerShell 5.1:Get-NetIPAddress:術語'Get-NetIPAddress'不被識別爲cmdlet,函數,腳本文件或可操作程序的名稱。檢查名稱的拼寫,如果包含路徑,請檢查路徑是否正確,然後重試。 在線:1 char:1 + Get-NetIPAddress | ?{$ _。AddressFamily -eq「IPv4」 - 和($ _。IPAddress ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Get-NetIPAddress:字符串)[],CommandNotFoundException + FullyQualifiedErrorId:CommandNotFoundException –

+1

我的猜測是'Get-NetIPAddress'在這樣一箇舊的操作系統上不可用https://docs.microsoft.com/en-us/powershell/module/nettcpip/ get-netipaddress?view = win10-ps它應該在運行Windows 8.1或更高版本/類似的機器上可用,如果我閱讀這個問題:https://stackoverflow.com/q/28815302/352640 –

+0

啊,沒有得到你試圖使:-) 我確實認爲OP是使用Windows操作系統的'現代'版本。 –

2

對於Windows和/或PowerShell的版本不支持Get-NetIPAddress,你可以得到的類Win32_NetworkAdapterConfigurationWin32_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 
}