Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell 通过arp-a获取MAC地址_Powershell - Fatal编程技术网

Powershell 通过arp-a获取MAC地址

Powershell 通过arp-a获取MAC地址,powershell,Powershell,我没有过滤MAC地址。它向我显示了完整的输出。正如Ansgar已经指出的那样,您需要从结果的匹配对象中获得值属性值: $MAC = arp -a $address | Select-String ('([0-9a-f]{2}-){5}[0-9a-f]{2}') 或者,使用属性枚举: $MAC = arp -a $address | Select-String '([0-9a-f]{2}-){5}[0-9a-f]{2}' | Select-Object -Expand Matches | Sel

我没有过滤MAC地址。它向我显示了完整的输出。

正如Ansgar已经指出的那样,您需要从结果的
匹配对象中获得
属性值:

$MAC = arp -a $address | Select-String ('([0-9a-f]{2}-){5}[0-9a-f]{2}')
或者,使用属性枚举:

$MAC = arp -a $address | Select-String '([0-9a-f]{2}-){5}[0-9a-f]{2}' | Select-Object -Expand Matches | Select-Object -Expand Value
您可以使用模板

$MAC = (arp -a $address | Select-String '([0-9a-f]{2}-){5}[0-9a-f]{2}').Matches.Value

您想获取哪个mac地址?
。|选择对象-扩展匹配项|选择对象-扩展值
你想实现什么?$address,有我客户的本地ip地址,我想获得他们的macaddresses@AnsgarWiechers它起作用了吗
$template=@" 
{row*:  {IP:0\.0\.0\.0}           {Mac:54-64-d9-6d-28-a3}     {TypeAdress:dynamique}} 
{row*:  {IP:255\.255\.255\.255}         {Mac:ff-ff-ff-ff-ff-ff}     {TypeAdress:statique}}  
"@

#you can then filter directly
arp -a '192.168.0.1'  | ConvertFrom-String -TemplateContent $template | select {$_.Row.Mac}

# or you can filter after
arp -a  | ConvertFrom-String -TemplateContent $template | where {$_.Row.IP -eq '192.168.0.1'} | select {$_.Row.Mac}

#you can too get list of object like this
arp -a  | ConvertFrom-String -TemplateContent $template |  %{[pscustomobject]@{IP=$_.Row.IP;MAC=$_.Row.Mac;Type=$_.Row.TypeAdress}}