VMware Cloud Community
mark_chuman
Hot Shot
Hot Shot

I like "one-liners". Adding MAC address as output to existing script

I have this for reporting on the name and guestfamily for any virtual machines at hardware version 4.

Get-VM | ForEach-Object {Get-View $_.ID} | Where-Object { $_.Config.Version -eq "vmx-04" } | ForEach-Object {Write-Host

$_.Name","$_.guest.guestfamily }

I am really trying to also report on the mac address of the virtual machine, but am having some difficulty.  I was trying to find it in commands like this:

$_.guest.guestnicinfo

$_.guest.net.macAddress

I think it exists here - Guest                : VMware.Vim.GuestInfo, but not able to find it.

Also was looking at "net" here - http://www.jarvana.com/jarvana/view/net/java/dev/vcc/thirdparty/vi-api/4.0.0-2/vi-api-4.0.0-2-javado..., but no luck.

Thanks

0 Kudos
5 Replies
mark_chuman
Hot Shot
Hot Shot

FYI,

My output in the current state is like this:

virtualmachine name , windowsGuest

Thanks.

0 Kudos
LucD
Leadership
Leadership

Try this

Get-View -ViewType VirtualMachine | Where-Object { $_.Config.Version -eq "vmx-04" } | %{Write-Host $_.Name","$_.guest.guestfamily","$_.guest.net[0].macaddress }

It uses the Get-View cmdlet instead of the Get-VM.

This will also only show the MAC address of the 1st NIC. If you have VMs with more than 1 NIC it will have to adapted.

To avoid errors for VMs that do not have the Net information filled in, you can use this variant

Get-View -ViewType VirtualMachine | Where-Object { $_.Config.Version -eq "vmx-04" } | %{Write-Host $_.Name","$_.guest.guestfamily","{if($_.Guest.Net){$_.guest.net[0].macaddress}} }


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
mark_chuman
Hot Shot
Hot Shot

Thanks for this.  It does appear to be pulling the information correctly, but it's failing for roughly 30 to 40% of the virtual machines targeted.  I am geting this type error, which I initially thought was related to the VM having multiple nics, but the VM the script threw an error on doesn't seem to have any odd configs and only shows one nic.

Cannot index into a null array.
At line:1 char:146
+ Get-View -ViewType VirtualMachine | Where-Object { $_.Config.Version -eq "vmx-04" } | %{Write-Host $_.Name","$_.guest
.guestfamily","$_.guest.net[ <<<< 0].macaddress }
    + CategoryInfo          : InvalidOperation: (0:Int32) [], RuntimeException
    + FullyQualifiedErrorId : NullArray

This is definitely related to this portion of the script ($_.guest.net[0].macaddress) because when I remove that part it runs through fine (with no MAC info obviously).

Thanks

0 Kudos
LucD
Leadership
Leadership

The $_.Guest.Net property will be $null when the VMware Tools are not installed or when the VM is powered off.

The 2nd variation of the one-liner tests this condition.

You shouldn't be getting the errors with the 2nd line.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
mark_chuman
Hot Shot
Hot Shot

Thanks a lot for the help on this.

0 Kudos