How Can We Help?
Find VM based on MAC
This sample query shows one way of finding a VM which has a vNIC with a specific MAC address.
The following might seem a bit cryptic at first, but it turns out to be the fastest method I came up with till now.
It uses the where-method instead of the Where-Object cmdlet. See also Use the ‘faster’ methods.
1 2 3 4 5 6 7 |
$mac = '12:34:56:78:90:12' $sView = @{ ViewType = 'VirtualMachine' Property = 'Name','Config.Hardware.Device' } (Get-View @sView).Where{$_.Config.Hardware.Device.Where{$_ -is [VMware.Vim.VirtualEthernetCard] -and $_.MacAddress.Equals($mac)}} | Select-Object Name |
If you need to look for a number of MAC addresses, you can do
1 2 3 4 5 6 7 8 |
$macs = '12:34:56:78:90:12','12:34:56:78:90:34','12:34:56:78:90:56' $sView = @{ ViewType = 'VirtualMachine' Property = 'Name','Config.Hardware.Device' } (Get-View @sView).Where{$_.Config.Hardware.Device.Where{$_ -is [VMware.Vim.VirtualEthernetCard] -and $macs -contains $_.MacAddress}} | Select-Object Name |