2016-03-15 135 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