Checking which VM has mac address of “X” using get-view

by Grzegorz Kulikowski

This time i wanted to show how can we check which vm is using mac address X. For example some ntwk admin can ask you which vm has this mac address. So i will show two ways of doing this, there are many many more besides those.
First is 1 liner:
[sourcecode language=”powershell”]
get-view -viewtype virtualmachine -property name,config.hardware.device |%{$vmname=$; $($_.config.hardware.device | ?{ $_ -is [Vmware.vim.virtualethernetCard]}) | select *,@{N="vm name";E={$vmname}} |?{$_.MacAddress -eq ’00:50:56:66:77:88′} }
’00:50:56:66:77:88′ – is the mac i am looking for. You can define your own output using this one liner, i wanted to see the whole device + the vm name that holds this device.
First we get all virtual machines then for each virtual machine we will be printing out its devices, but only those devices which are network cards, then selecting * (all properties) + $vmname where the macaddress equals our mac that we are looking for. I stored my vmname in $vmname after first pipe.
Ok, now the same thing but i guess in more readable way:
[sourcecode language=”powershell”]
foreach($vmview in get-view -viewtype virtualmachine -property name,config.hardware.device)
foreach ($netcard in ($vmview.config.hardware.device|?{$_ -is [Vmware.vim.virtualethernetCard]}))
if ($netcard.macaddress -eq "00:50:56:66:77:88")
"Found mac in vm: $($"
So we do : get-view for all vms. Each of this vm view object will be stored in $vmview, and for each $vmview we will be displaying its network cards.
$netcard is variable the holds the virtual machine network device configuration.
$vmview.config.hardware.device -> this holds all devices
More about this
[Vmware.vim.virtualethernetCard] -> this is the type of network card , so we are only getting network cards.
So we check then if the macaddress of this network cards matches our mac, if yes, print out message

I saw many people doing this in different ways, for now i saw i think 5 different approaches to check this.
The virtual machine can be powered off/on, guest operating system can be installed or not, it does not make any difference using this approach as we are going though virtual machine config.

If you want to narrow down the area where your vms are you can modify this line
foreach($vmview in get-view -viewtype virtualmachine)
So when you want to check this in some cluster or datacenter , you can add -Searchroot to get-view, and give the moref
How do you get the moref of that particulard datacenter or cluster ? you can do it in different ways as well:
get-cluster “your cluster”|select id,name
get-view -viewtype clusterComputeResource | select name,moref
So if you would give in get-view -viewtype virtualmachine -Searchroot (get-cluster “cluster A”).id -> then this would give you vms only that reside in cluster A

You may also like

1 comment

Roger January 10, 2013 - 4:06 pm

Very nice and helpful script. How is is possible to import a list of mac addresses, e.g. from a csv-File?


Leave a Reply

Chinese (Simplified)EnglishFrenchGermanHindiPolishSpanish