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


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:

get-view -viewtype virtualmachine -property name,config.hardware.device |%{$vmname=$_.name; $($_.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:

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: $($vmview.name)"
	    $netcard.macaddress
	   }
   }
 }

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 http://www.vmware.com/support/developer/vc-sdk/visdk41pubs/ApiReference/vim.vm.device.VirtualEthernetCard.html
[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

Advertisements

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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s