VMware Cloud Community
dmacph
Contributor
Contributor
Jump to solution

How do i get MAC address and IP in a format i can use in a script


Hi

I want to collect the IP and MAC from a specific MV for use elsewhere.

The 2 queries i have are

Get-VM $VMname | Get-NetworkAdapter | select MacAddress

Get-VM $VMname | Select @{N=”IP Address”;E={@($_.guest.IPAddress[0])}}

They both return a header and the value

IP Address

----------

10.38.96.82

I just want the value that i can stick in a variable for use elsewhere - like so

$VMmac = (Get-VM $VMname | Get-NetworkAdapter | select MacAddress )

What simple thing am i missing?

Assistance much appreciated

Tags (2)
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

The other one is similar.

Get-VM $vmname | Select @{N=”IP Address”;E={@($_.guest.IPAddress[0])}} | Select -ExpandProperty "IP Address"


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

View solution in original post

Reply
0 Kudos
14 Replies
LucD
Leadership
Leadership
Jump to solution

Use the ExpandProperty parameter on the Select-Object cmdlet.

Get-VM $VMname | Get-NetworkAdapter | select -ExpandProperty MacAddress


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

dmacph
Contributor
Contributor
Jump to solution

Thanks for the quick reply LucD

MAC address - sorted

That gets me 50% there.

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

The other one is similar.

Get-VM $vmname | Select @{N=”IP Address”;E={@($_.guest.IPAddress[0])}} | Select -ExpandProperty "IP Address"


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

Reply
0 Kudos
dmacph
Contributor
Contributor
Jump to solution

Thankly muchly

Think i had every variation, a fraction away from that this morning.:smileyconfused:

Reply
0 Kudos
esxi1979
Expert
Expert
Jump to solution

LucD

how can i get name & mac both as output ? thanks

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Try like this

Get-VM -PipelineVariable vm | Get-NetworkAdapter |

Select @{N='VM';E={$vm.Name}},Name,

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

    MacAddress


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

glavelis
Contributor
Contributor
Jump to solution

Hi , I tried your snippet to get Name DNSName and IPAddress from my vCenter and it works just fine, but I want to get only the VMs that have an active IP (some VMs doen't have Tools installed and don't report an IP).

I tried this but get no results :

get-vm | select name, Powerstate, @{N="IPAddress"; E={$_.Guest.IPAddress[0]}}, @{N="DnsName"; E={$_.ExtensionData.Guest.Hostname}} | where {($_.powerstate -eq 'poweredon') -and ($_.Guest.IPAddress -ne $null)}

Any suggestions?

 

Thanks in advance 🙂

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Place a Where-clause earlier in the stream

Get-VM -PipelineVariable vm | where{$_.Guest.Nics.IpAddress} | Get-NetworkAdapter |

Select @{N='VM';E={$vm.Name}},Name,

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

  MacAddress


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

glavelis
Contributor
Contributor
Jump to solution

Thanx mate! Worked like a charm! 😉
Reply
0 Kudos
RadoslawStarcze
Contributor
Contributor
Jump to solution

hi,

is it possible to add host (VMHost) name for this output? 

What if I have more than one IP - more vnics? is it possible to add mac for each vnic? 

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

It should run through all vNICs connected to a VM.

Try like this

Get-VM -PipelineVariable vm | where{$_.Guest.Nics.IpAddress} | Get-NetworkAdapter |

Select @{N='VM';E={$vm.Name}},Name,

   @{N='VMHost';E={$vm.VMHost.Name}},

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

  MacAddress


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

Reply
0 Kudos
RadoslawStarcze
Contributor
Contributor
Jump to solution

now I see host, but when i have more ips I see only one mac:

 

VM         : UTDrrrr

Name       : Network adapter 1

VMHost     : <name of host>

IP Address : fe80::ip6|10.x.x.x.|169.254.124.56

MacAddress : 00:50:xx:xx:a1:1x

VM         : UTDPCSM001
Name       : Network adapter 1
VMHost     : cdtesas0cmp018.cdtdpc.local
IP Address : fe80::2cc5:b316:3a19:7c38|10.227.163.238|169.254.124.56
MacAddress : 00:50:56:85:a1:10
Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

I suspect that is since there is no observed IP addr on that 2nd vNIC.
Try leaving out the Where-clause in the 1st line


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

Reply
0 Kudos
aryajp
Contributor
Contributor
Jump to solution

Hi LucD, I'm looking for a PowerCLI script that helps to determine the Hardware address of vms which can be grabed from the input file. Please help.
Reply
0 Kudos