garypaquette
Contributor
Contributor

Get VM MAC within a script pulling all of the data from the host/guests

Jump to solution

I am trying to get all of the information I need to standard output for the host and guests.

Note sure how to post code here and make it pretty :smileylaugh:

The only item I am missing in the puzzle is the MAC address.  I can get it in a stand-alone script, though I cannot figure out how to get it embedded within mine.

Here is what I am trying to do:

$user = 'user'

$pswd = '1234'

$target = '192.168.2.221'

Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -Confirm:$false

Write-Host " "

Write-Host "**************************************************"

Write-Host "VMware Host:"

Connect-VIServer -Server $target -User $user -Password $pswd

Get-VMHost -Server $target | Select ApiVersion,Build,ConnectionState,CpuTotalMhz,CpuUsageMhz,CryptoState,DiagnosticPartition,ExtensionData,FirewallDefaultPolicy,HyperthreadingActive,Id,IsStandalone,LicenseKey,Manufacturer,MaxEVCMode,MemoryTotalGB,MemoryTotalMB,MemoryUsageGB,MemoryUsageMB,Model,Name,NetworkInfo,NumCpu,Parent,ParentId,PowerState,ProcessorType,StorageInfo,TimeZone,Uid,Version,VMSwapfileDatastore,VMSwapfileDatastoreId,VMSwapfilePolicy

Write-Host "**************************************************"

Write-Host "VMware Guest(s):"

Get-VM | Select *,

    @{N="Configured OS";E={$_.ExtensionData.Config.GuestFullname}},

    @{N="IP Address";E={@($_.guest.IPAddress[0])}},

    @{N="MAC Address:";E={@(-- MAC ADRESS --}},

    @{N="Disk Type";E={(Get-Harddisk $_).Storageformat}}

Write-Host "VMware Guest(s) EoF"

Write-Host "**************************************************"

Disconnect-VIServer -Server $target -Confirm:$false

1 Solution

Accepted Solutions
LucD
Leadership
Leadership

This should get you the MAC address

@{N="MAC Address:";E={$_.Guest.Nics.MacAddress -join '|'}},


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

View solution in original post

8 Replies
LucD
Leadership
Leadership

This should get you the MAC address

@{N="MAC Address:";E={$_.Guest.Nics.MacAddress -join '|'}},


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

View solution in original post

garypaquette
Contributor
Contributor

@LucD - your the best!

Thanks

Gary

0 Kudos
garypaquette
Contributor
Contributor

Question:  When I run the correction in my script the MAC is only returned on the first VM and not the others?

My test one show all of the MAC's per the 3 VM's I have on that host.

$user = 'user'

$pswd = '1234'

$target = '192.168.2.221'

Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -Confirm:$false

Connect-VIServer -Server $target -User $user -Password $pswd

Get-VM -PipelineVariable vm | Get-NetworkAdapter |

     Select @{N=”IP Address”;E={$nic = $_; ($vm.Guest.Nics | where{$_.Device.Name -eq $nic.Name}).IPAddress -join '|'}}, MacAddress

Disconnect-VIServer -Server $target -Confirm:$false

0 Kudos
LucD
Leadership
Leadership

That is different code from initially, you now have the Get-NetworkAdapter cmdlet in the pipeline.
Which is inherently better than the method based on the Guest property.

The Guest property only has values when the VMware Tools are installed in the Guest OS and when the VM is powered on, or was recently powered on.

In short:

- data under Guest depends on the VMware Tools and obtains the info from within the Guest OS

- Get-NetworkAdapter obtains the info from outside the VM (the vSphere level)

- the drawback with the outside method is that a Guest OS could overwrite a MAC address


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

0 Kudos
garypaquette
Contributor
Contributor

LucD​  Makes sense - so I suspect even through the external method returns the MAC the internal version will not.

Yeah - I understand the difference between to the approaches now - just trying to fulfill the requirement.

Is there a method around that that would within my primary long script to return that same or no?

Thanks!

Gary

0 Kudos
LucD
Leadership
Leadership

You mean something like this?

$user = 'user'

$pswd = '1234'

$target = '192.168.2.221'

Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -Confirm:$false

Write-Host " "

Write-Host "**************************************************"

Write-Host "VMware Host:"

Connect-VIServer -Server $target -User $user -Password $pswd

Get-VMHost -Server $target | Select ApiVersion,Build,ConnectionState,CpuTotalMhz,CpuUsageMhz,CryptoState,DiagnosticPartition,ExtensionData,FirewallDefaultPolicy,HyperthreadingActive,Id,IsStandalone,LicenseKey,Manufacturer,MaxEVCMode,MemoryTotalGB,MemoryTotalMB,MemoryUsageGB,MemoryUsageMB,Model,Name,NetworkInfo,NumCpu,Parent,ParentId,PowerState,ProcessorType,StorageInfo,TimeZone,Uid,Version,VMSwapfileDatastore,VMSwapfileDatastoreId,VMSwapfilePolicy

Write-Host "**************************************************"

Write-Host "VMware Guest(s):"

Get-VM | Select *,

    @{N="Configured OS";E={$_.ExtensionData.Config.GuestFullname}},

    @{N="IP Address";E={@($_.guest.IPAddress[0])}},

    @{N="MAC Address:";E={(Get-NetworkAdapter -VM $_).MacAddress -join '|'}},

    @{N="Disk Type";E={(Get-Harddisk $_).Storageformat}}

Write-Host "VMware Guest(s) EoF"

Write-Host "**************************************************"

Disconnect-VIServer -Server $target -Confirm:$false


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

garypaquette
Contributor
Contributor

LucD​ - that did the trick!


You can take the rest of the day off :smileylaugh:

Or I would up for a full mind meld...

0 Kudos
LucD
Leadership
Leadership

pIjtFCbiK3usMGOuVYBEvkJZLJA.png


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