Often when when working in large environments you’ll get interesting requests. These may be to assist in troubleshooting, auditing, reporting or any other reason.
I was recently requested to provide a list of mac addresses for every VM with a vNIC in a specific vlan.
I had a feeling this was going to be a repeated request (for various vlans) so presented a good opportunity to script it.
Finding a mac address for a particular vm is straightforward;
1 |
Get-VM -Name testvm | Get-NetworkAdapter | ft Parent, MacAddress -auto |
and of course, this can be expanded to a Cluster, Host etc;
1 |
Get-cluster CLUSTERNAME | Get-VM | Get-NetworkAdapter | ft Parent, MacAddress -auto |
1 |
Get-VMHost -name HOSTNAME | Get-VM | Get-NetworkAdapter | ft Parent, MacAddress -auto |
Getting all the VM’s on a specified VLAN and including the required details is not that straightforward, but certainly not that difficult. Here’s how I went about it for VLAN ID 2591;
1 2 3 |
Get-Cluster CLUSTERNAME | Get-VM | where { ($_ | Get-VirtualPortGroup | where {$_.vlanid -match "2591"})} | Select Name, @{N="MacAddress";E={[string]::Join(',',($_.Guest.Nics | %{$_.MacAddress}))}}, @{N="VLanID";E={[string]::Join('#',(Get-VirtualPortGroup -VM $_ | %{$_.VLanID}))}} |
Note that a blank MacAddress field indicates the VM is simply powered off.