How to use VMware vSphere PowerCLI to find an ESX/ESXi server by MAC address

PowerCLI logoIn “Virtual machine failed to power on” Monique Vanmeulebrouk describes a problem where in one stage she needed to find an ESX server that has a certain MAC address. Of course you can log in to all your ESX servers and issue the “ifconfig | grep -i hw” command as described in the VMware Knowledge base article “Identifying the ESX Service Console MAC address”. But this method takes a lot of time. You can do this much easier with VMware vSphere PowerCLI.

You can find the ESX/ESXi server with a certain MAC address by just using the PowerCLI Get-VMhost and Get-VMHostNetworkAdapter cmdlets and piping these together. E.g. to find the host with MAC address “00:50:56:78:98:a2″ you can give the following PowerCLI command:

Get-VMHost | `
  Get-VMHostNetworkAdapter | `
  Where-Object {$_.Mac -eq "00:50:56:78:98:a2"} | `
  Format-List -Property *

Figure 1. PowerCLI command to find a host 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.

Altough much faster than the ifconfig method, it still is kind of slow. In my environment with fifty hosts this PowerCLI command takes about one minute to return.

So I decided to build a PowerCLI advanced function called Get-VMHostByMacAddress that uses the VMware vSphere SDK to find the ESX/ESXi server 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-VMHostByMacAddress {
  <#
  .SYNOPSIS
    Retrieves the host with a certain MAC address on a vSphere server.
	
  .DESCRIPTION
    Retrieves the host with a certain MAC address on a vSphere server.
	
  .PARAMETER MacAddress
    Specify the MAC address of the host to search for.
	
  .EXAMPLE
    Get-VMHostByMacAddress -MacAddress 00:0c:29:1d:5c:ec,00:0c:29:af:41:5c
	Retrieves the hosts 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-VMHostByMacAddress
	Retrieves the hosts 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:    17-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 hosts
    $HostsView = Get-View -ViewType HostSystem -Property Name,Config.Network
  }
  
  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 {	
        $HostsView | `
	      ForEach-Object {
            $HostView = $_
			# Search the physical nics
            if ($HostView.Config.Network.Pnic) {
			  $HostView.Config.Network.Pnic | Where-Object {
	            # Filter the hosts on Mac address
                $_.Mac -eq $Mac
			  } | `
              Select-Object -property @{N="VMhost";E={$HostView.Name}},
			    Device,
				Mac,
			    Portgroup,
				@{N="IpAddress";E={$_.Spec.Ip.IpAddress}},
				@{N="DhcpEnabled";E={$_.Spec.Ip.Dhcp}}
			}
			# Search the virtual nics
			if ($HostView.Config.Network.Vnic) {
			  $HostView.Config.Network.Vnic | Where-Object {
	            # Filter the hosts on Mac address
                $_.Spec.Mac -eq $Mac
			  } | `
              Select-Object -property @{N="VMhost";E={$HostView.Name}},
			    Device,
				@{N="Mac";E={$_.Spec.Mac}},
			    Portgroup,
				@{N="IpAddress";E={$_.Spec.Ip.IpAddress}},
				@{N="DhcpEnabled";E={$_.Spec.Ip.Dhcp}}
			}
			# Search the console virtual nics
			if ($HostView.Config.Network.ConsoleVnic) {
			  $HostView.Config.Network.ConsoleVnic | Where-Object {
	            # Filter the hosts on Mac address
                $_.Spec.Mac -eq $Mac
			  } | `
              Select-Object -property @{N="VMhost";E={$HostView.Name}},
                Device,
				@{N="Mac";E={$_.Spec.Mac}},
			    Portgroup,
				@{N="IpAddress";E={$_.Spec.Ip.IpAddress}},
				@{N="DhcpEnabled";E={$_.Spec.Ip.Dhcp}}
			}
		  }
      }
    }
  }
}

Figure 3: Get-VMHostByMacAddress PowerCLI advanced function.

The Get-VMHostByMacAddress function gives the following output:

Output of the Get-VMHostByMacAddress PowerCLI function

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

The Get-VMHostByMacAddress function took about 4 seconds to complete. That is about fifteen times faster than the first script.

Conclusion

If you take the time to dive into the VMware vSphere SDK, you can gain a lot of speed for your PowerCLI scripts.

About these ads

About Robert van den Nieuwendijk
Robert van den Nieuwendijk is a freelance senior systems engineer with over 25 years of experience in the IT industry. His main focus is VMware vSphere and Microsoft Windows Server. He tries to automate as much of his work as possible using Microsoft Windows PowerShell. Robert is the author of the book “Learning PowerCLI”. 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 5, VTSP 5, VCP4-DCV, VCP5-DCV, VCA-Cloud, VCA-WM, MCSE, MCSA, MCP, MCP+I, and ITIL Foundation. In 2012, 2013 and in 2014 Robert received the VMware vExpert title for his contribution to the community of VMware users over the past year.

2 Responses to How to use VMware vSphere PowerCLI to find an ESX/ESXi server by MAC address

  1. Pingback: Virtual machine failed to power on « This Virtual Realm

  2. Pingback: VM Power-up failure — Reason: Failed to lock the file. Cannot open the disk…

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

Follow

Get every new post delivered to your Inbox.

Join 773 other followers

%d bloggers like this: