How to use VMware vSphere PowerCLI to find the MAC addresses of a virtual machine
September 12, 2011 9 Comments
In 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
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.
Thanks Luc. That is a good suggestion. I will change the function accordingly.
Should the get-view filter also include a start anchor (^) like this?
-Filter @{“Name”=”^$VirtualMachine$”}
Otherwise you’ll get for example OtherVM11 when you ask for VM11.
http://communities.vmware.com/message/2126362
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?
Yes. You can use:
get-vm server-xyz | get-networkadapter
But it is very slow if you have a large environment.
get-vm server-xyz | get-networkadapter
how to get vmname in from of this info
The VM name is in the Parent property of the NetworkAdapterImpl object. So you can get the VM name with:
Get-VM server-xyz | Get-NetworkAdapter | `
Select-Object -Property Parent,Name,Type,NetworkName,MacAddress,WakeOnLanEnabled | `
Format-Table -AutoSize
Very useful, thanks a lot!!
Pingback: Options to find a MAC Address in VMware Environment. | Techbrainblog