< Back
You are here:
Print

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.

 

$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

$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

Leave a Reply

Your email address will not be published. Required fields are marked *

*
*

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Table of Contents