Srihari Humbarwadi
Srihari Humbarwadi

Reputation: 33

Getting the MAC address by arp -a

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

I am not getting the MAC address filtered. It's showing me complete output.

Upvotes: 1

Views: 3160

Answers (2)

Esperento57
Esperento57

Reputation: 17462

You can use template

$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}}

Upvotes: 0

Mathias R. Jessen
Mathias R. Jessen

Reputation: 174435

As Ansgar already pointed out, you'll want the Value property value from the resulting Match objects:

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

or, using property enumeration:

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

Upvotes: 1

Related Questions