2016-03-15 145 views
3

我想根據命令輸出中的'some string value'來篩選結果。 例如,我需要從ipconfig的輸出中獲取MAC地址。 我試圖在PowerShell中獲得篩選結果

ipconfig /all | Select-String -Pattern "Physical Address" 

我得到的所有MAC編輯部地址一樣

Physical Address. . . . . . . . . : AA-AA-AA-AA-AA-AA 
Physical Address. . . . . . . . . : AA-AA-AA-AA-AA-AA 

我想有WiFi適配器的唯一的MAC地址。 SO輸出將是AA-AA-AA-AA-AA-AA。 我不想整個線

Physical Address. . . . . . . . . : AA-AA-AA-AA-AA-AA 
+0

什麼是你的WiFi物理地址是該段的頭?除了無線網卡外,這部分還有其他物理地址嗎? – dfundako

+0

它說,'無線局域網適配器無線網絡連接:' 是的。 ipconfig/all輸出中共有5個物理地址。 – Vinnie

+0

要澄清,在無線局域網適配器無線網絡連接部分,有5? – dfundako

回答

2

這個作品在我的系統上:

$Wireless_Adapter_Regex = 
@' 
(?ms).+?Wireless LAN adapter .+? 
    Physical Address. . . . . . . . . : (\S+) 
'@ 

(ipconfig /all | out-string) -match $Wireless_Adapter_Regex > $null 
$matches[1] 

C4-D9-87-41-37-F1 
+0

這確實解決了問題。明智的方式來獲得正則表達式匹配字符串。 非常感謝。 – Vinnie

0

可以使用下面的命令:

ipconfig /all | Select-String -Pattern "\w\w-\w\w-\w\w-\w\w-\w\w-\w\w" -AllMatches | % { $_.Matches } | % { $_.Value } 
+0

這根本不能解決OP的問題 - 他如何限制匹配僅限於WiFi適配器接口? –

+0

@Krzysztof我真的很感激你的快速回復。 有沒有一種方法來過濾這個只有MAC地址的無線適配器? – Vinnie

0

ipconfig /all | Select-String -Pattern "Physical Address"可進一步擴展到通過分割輸出「:」分隔符和顯示第二場。示例如下。

PS C:\Users\jump> ipconfig /all | Select-String -SimpleMatch Physical 
-AllMatches | ForEach-Object { $_.ToString().split(":")[1]} 
B8-AC-6F-28-08-C5