How to use VMware vSphere PowerCLI to find a virtual machine by MAC address

PowerCLI logoSometimes you need to find a virtual machine by MAC address. This can be very time consuming if you have to do this by hand using the VMware vSphere Client. PowerCLI can do this task for you in only a few seconds. The script presented in this blogpost will retrieve the virtual machine that has a certain MAC address.

You can find the virtual machine with a certain MAC address by just using the PowerCLI Get-VM and Get-NetworkAdapter cmdlets and piping these together. E.g. to find the virtual machine with MAC address “00:0c:29:1d:5c:ec” you can give the following PowerCLI command:

Get-VM | `
  Get-NetworkAdapter | `
  Where-Object {$_.MacAddress -eq "00:0c:29:1d:5c:ec"} | `
  Format-List -Property *

Figure 1. PowerCLI command to find a virtual machine with certain MAC address.

The PowerCLI command of figure 1 gives the following output:

Output of the command of figure 1.

Figure 2: Output of the command of figure 1.

In my environment with about five hundred fifty virtual machines this PowerCLI command takes about two minutes and twenty seconds to return.

So I decided to build a PowerCLI advanced function called Get-VmByMacAddress that uses the VMware vSphere SDK to find the virtual machine with a certain MAC address as fast as possible. The function uses PowerShell comment-based help. And there are some examples how you can use this function in the help.

function Get-VmByMacAddress {
  <#
  .SYNOPSIS
    Retrieves the virtual machines with a certain MAC address on a vSphere server.
	
  .DESCRIPTION
    Retrieves the virtual machines with a certain MAC address on a vSphere server.
	
  .PARAMETER MacAddress
    Specify the MAC address of the virtual machines to search for.
	
  .EXAMPLE
    Get-VmByMacAddress -MacAddress 00:0c:29:1d:5c:ec,00:0c:29:af:41:5c
	Retrieves the virtual machines with MAC addresses 00:0c:29:1d:5c:ec and 00:0c:29:af:41:5c.
	
  .EXAMPLE
    "00:0c:29:1d:5c:ec","00:0c:29:af:41:5c" | Get-VmByMacAddress
	Retrieves the virtual machines with MAC addresses 00:0c:29:1d:5c:ec and 00:0c:29:af:41:5c.
	
  .COMPONENT
    VMware vSphere PowerCLI
	
  .NOTES
    Author:  Robert van den Nieuwendijk
	Date:    18-07-2011
	Version: 1.0
  #>
  
  [CmdletBinding()]
  param(
    [parameter(Mandatory = $true,
	           ValueFromPipeline = $true,
			   ValueFromPipelineByPropertyName = $true)]
	[string[]] $MacAddress
  )
  
  begin {
    # $Regex contains the regular expression of a valid MAC address
	$Regex = "^[0-9A-Fa-f][0-9A-Fa-f]:[0-9A-Fa-f][0-9A-Fa-f]:[0-9A-Fa-f][0-9A-Fa-f]:[0-9A-Fa-f][0-9A-Fa-f]:[0-9A-Fa-f][0-9A-Fa-f]:[0-9A-Fa-f][0-9A-Fa-f]$"	
  
    # Get all the virtual machines
    $VMsView = Get-View -ViewType VirtualMachine -Property Name,Guest.Net
  }
  
  process {
    ForEach ($Mac in $MacAddress) {
	  # Check if the MAC Address has a valid format
	  if ($Mac -notmatch $Regex) {
	    Write-Error "$Mac is not a valid MAC address. The MAC address should be in the format 99:99:99:99:99:99."
	  }
	  else {	
        # Get all the virtual machines
        $VMsView | `
	      ForEach-Object {
            $VMview = $_
            $VMView.Guest.Net | Where-Object {
	          # Filter the virtual machines on Mac address
              $_.MacAddress -eq $Mac
            } | `
			  Select-Object -property @{N="VM";E={$VMView.Name}},
			    MacAddress,
			    IpAddress,
				Connected
		  }
      }
    }
  }
}

Figure 3: Get-VmByMacAddress PowerCLI advanced function.

The Get-VmByMacAddress function gives the following output:

Output of the Get-VMHostByMacAddress PowerCLI function.

Figure 4: Output of the Get-VMHostByMacAddress PowerCLI function.

The Get-VmByMacAddress function took about 1.7 seconds to complete. That is about eighty times faster than the first script.

Advertisement

About Robert van den Nieuwendijk
Robert van den Nieuwendijk is a freelance senior systems engineer with over 30 years of experience in the IT industry. He focusses on VMware vCloud Suite and Microsoft Windows Server. He tries to automate as much of his work as possible using Microsoft PowerShell. Robert is the author of the books “Learning PowerCLI” and “Learning PowerCLI – Second Edition.” Robert is a frequent contributor and moderator at the VMware VMTN Communities. He has a bachelor degree in software engineering and holds the following IT certifications and accreditations: VSP 2016, VTSP 2016, VCP4-DCV, VCP5-DCV, VCP6-DCV, VCP6-CMA, VCA-Cloud, VCA-WM, VCA-NV, VMSP, VMTSP, ZCS, ZCP, ZCP-Cloud, MCSE, MCSA, MCP, MCP+I, PRINCE2 Foundation and ITIL Foundation. In 2012, 2013, 2014, 2015, 2016, 2017, 2018 and 2019 Robert received the VMware vExpert award for his contribution to the community of VMware users over the past year. In 2017 Robert also received the VMware vExpert Cloud award. PernixData made him in 2015 a member of the PernixPro.

10 Responses to How to use VMware vSphere PowerCLI to find a virtual machine by MAC address

  1. Sander Daems says:

    Nice script, very useful

  2. Pingback: How to use VMware vSphere PowerCLI to find the MAC addresses of a virtual machine « Robert van den Nieuwendijk

  3. xajax7 says:

    Nice script Sander. But I have a request, how to list down IP Address and Mac Address for given folder ? I dont want to search but just list. something like “listVM ” and it will display VMname, IPAddress, MAC address. Thanks.

    • I’m glad you like my script.

      To get a list of the VM names, IP addresses, MAC addresses, device names and connected states of all the VM’s in a folder called “Folder 1”, you can use the following PowerCLI script:

      Get-Folder -Name “Folder1″ | Get-VM | ForEach-Object {
      $VM = $_
      if ($VM) {
      $VM.Guest.Nics | Select-Object -Property @{N=”VM”;E={$VM.Name}},
      @{N=”IPAddress”;E={[string]::Join(‘,’,$_.IPAddress)}},
      MacAddress,Device,Connected
      }
      }

  4. eric greer says:

    This is great. Thank you sooo much!

  5. Tim says:

    When I try to run it, it just doesn’t do anything. I even tried to pipe the output to a .txt file, and the file is 0 in size.

    What am I doing wrong?

  6. karol says:

    works fine. thank you.

  7. Total lifesaver, thank you so much for this function!

  8. Manea Marius says:

    You should specify this function works only if VMWare tools is installed on the machine.. otherwise you have to resort to the first option which takes much longer

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 )

Facebook photo

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

Connecting to %s

%d bloggers like this: