How to use VMware vSphere PowerCLI to find an ESX/ESXi server by MAC address
July 17, 2011 4 Comments
In “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:
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:
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.
Pingback: Virtual machine failed to power on « This Virtual Realm
Pingback: VM Power-up failure — Reason: Failed to lock the file. Cannot open the disk…
Pingback: Options to find a MAC Address in VMware ESX Environment. | Techbrainblog
This is one of the art of saving time to troubleshoot IP conflict issues within VMWare. Nice and amazing work. Thank you