How to use VMware vSphere PowerCLI to find a virtual machine by MAC address
July 18, 2011 10 Comments
Sometimes 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:
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:
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.
Nice script, very useful
Thanks Sander.
Pingback: How to use VMware vSphere PowerCLI to find the MAC addresses of a virtual machine « Robert van den Nieuwendijk
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
}
}
This is great. Thank you sooo much!
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?
works fine. thank you.
Total lifesaver, thank you so much for this function!
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