How to use VMware vSphere PowerCLI to find the MAC addresses of a virtual machine

PowerCLI logoIn the search engine queries that bring people to my blog, I see that some people are looking for a PowerCLI script that finds the MAC addresses of one or more virtual machines.

So I decided to modify my previous Get-VmByMacAddress function that searched the virtual machines by MAC address, to do it the other way around. The new Get-VmMacAddress function uses the Get-View cmdlet to search the virtual machines and is very fast. In my environment with almost 600 MAC addresses, it takes less than 1.5 seconds to retrieve all the MAC addresses.

The Get-VmMacAddress function in Listing 1 contains comment-based help. Examples how to use the function can be found inside the function code. Or you can use the Get-Help cmdlet to get the Get-VmMacAddress help, after you have defined the function in your PowerCLI session.

function Get-VmMacAddress {
  <#
  .SYNOPSIS
    Retrieves the MAC addresses of virtual machines on a vSphere server.

  .DESCRIPTION
    Retrieves the MAC addresses of virtual machines on a vSphere server.
    In addition it retrieves also the IP addresses and the connection state.

  .PARAMETER VM
    Specify virtual machines whose MAC addresses you want to retrieve.

  .EXAMPLE
    Get-VmMacAddress
    Retrieves the MAC addresses of all the virtual machines.

  .EXAMPLE
    Get-VmMacAddress -VM VM0123,VM0456
    Retrieves the MAC addresses of the virtual machines named VM01234 and VM0456.

  .EXAMPLE
    "VM0123","VM0456" | Get-VmMacAddress
    Retrieves the MAC addresses of the virtual machines named VM01234 and VM0456.

  .COMPONENT
    VMware vSphere PowerCLI

  .NOTES
    Author:  Robert van den Nieuwendijk
    Date:    12-09-2011
    Version: 1.0
  #>

  [CmdletBinding()]
  param(
    [parameter(ValueFromPipeline = $true,
               ValueFromPipelineByPropertyName = $true)]
               [string[]] [ValidateNotNull()] $VM=".*"
  )

  process {
    ForEach ($VirtualMachine in $VM) {
      # Get the virtual machine
      $VMsView = Get-View -ViewType VirtualMachine -Property Name,Guest.Net -Filter @{"Name"="$VirtualMachine$"}
      if ($VMsView) {
        $VMsView | `
          ForEach-Object {
            $VMview = $_
            $VMView.Guest.Net | `
              Select-Object -property @{N="VM";E={$VMView.Name}},
                MacAddress,
                IpAddress,
                Connected
          }
      }
    }
  }
}

Listing 1. The Get-VmMacAddress function

Advertisements

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-Cloud, 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 and 2017 Robert received the VMware vExpert award for his contribution to the community of VMware users over the past year. PernixData made him in 2015 a member of the PernixPro.

9 Responses to How to use VMware vSphere PowerCLI to find the MAC addresses of a virtual machine

  1. LucD says:

    Nice function, but I think your Get-View filter should be like this
    -Filter @{“Name”=”$VirtualMachine$”}
    Otherwise you will also get for example VM111 when you ask for VM11.

  2. This is really useful, many thanks!
    Am I right in thinking that this requires a running VMware tools? Any way of getting the MAC with PowerCLI (or any other scriptable API/interface eg. esxcli) for a just built VM with no guest OS?

  3. daniel2ac says:

    Very useful, thanks a lot!!

  4. Pingback: Options to find a MAC Address in VMware Environment. | Techbrainblog

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

%d bloggers like this: