VMware Cloud Community
miguelvelezwhit
Enthusiast
Enthusiast
Jump to solution

How to obtain a VMs IP address along with other parameters in PowerCLI

I know it's a strange question, but with all these different new parameters, along with Get-Member, VIProperty and all other kinds of stuff rolling around in my head, I needed to know the answer to something i think is simple.

When you export the data from a VM listing in vSphere web client, you can customize what columns you do or don't want; including choices like IP Address and Notes

I've gone through all of my notes and I cannot for the life of me find where to request the info for IP Address (IPv4 preferably) 

Can someone 'show me the way'?  (That's for alf of us who are old enough to remember Peter Frampton *LOL*)

Thanks in advance as always,

Miguel

1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

The previous answer will work in most cases, but to have it as the CSV export does it (multiple NICs and IPv6 addresses), you'll have to do something like this.

Note that you'll need to have VMware Tools installed to get the IP adresses.

Get-VM |

Select Name,@{N='IP Address';E={

    @($_.guest.IPAddress | where{[ipaddress]::Parse($_).AddressFamily -eq 'InterNetwork'}

    $_.Guest.IpAddress | where{[ipaddress]::Parse($_).AddressFamily -eq 'InterNetworkv6'}) -join ','

    }}


And yes, this "Joker" remembers Peter Frampton.

In fact I even remember Humble Pie :smileycool:


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

View solution in original post

9 Replies
kwhornlcs
Enthusiast
Enthusiast
Jump to solution

Try something like this, replace "vmname" with your VM:

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

0 Kudos
LucD
Leadership
Leadership
Jump to solution

The previous answer will work in most cases, but to have it as the CSV export does it (multiple NICs and IPv6 addresses), you'll have to do something like this.

Note that you'll need to have VMware Tools installed to get the IP adresses.

Get-VM |

Select Name,@{N='IP Address';E={

    @($_.guest.IPAddress | where{[ipaddress]::Parse($_).AddressFamily -eq 'InterNetwork'}

    $_.Guest.IpAddress | where{[ipaddress]::Parse($_).AddressFamily -eq 'InterNetworkv6'}) -join ','

    }}


And yes, this "Joker" remembers Peter Frampton.

In fact I even remember Humble Pie :smileycool:


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

kwhornlcs
Enthusiast
Enthusiast
Jump to solution

Never fail to learn something new from you LucD‌ - I'll be tucking that one away.Smiley Wink

Dumb question...what do you use for the syntax highlighting? doesn't seem any of the default forum editor formats do the key-word, string or variable highlighting properly for Powershell.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

I use a the Copy as HTML plugin for ISE.

See also Some ways to enter PowerCLI code under the new forum SW


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

miguelvelezwhit
Enthusiast
Enthusiast
Jump to solution

It's good to know that there's someone else besides me remembers back when music was actually music!!  I get teased around here because I was actually alive and watching when man landed on the moon back in '69.  These kids look at me as if I came from Pluto.  Oh well.  Thanks for the flashback

0 Kudos
miguelvelezwhit
Enthusiast
Enthusiast
Jump to solution

Oh by the way, the force is definitely with you Master Luke.  You had my mind boggled with this answer.  I easily spent a good two hours trying to take apart the expression so that I could understand what you did.  I was going through the pages of your 2nd edition guide.  I've managed to determine that what made all this possible was not only a calculated value/expression, but also included some type of .NET class which I still haven't found yet.  Although I remember reading about this, I don't know enough .NET to really wield these the way that you do.  Is there some kind of reference that I can access which shows me all the different classes?  I'm assuming that this is some hybrid of Visual Studio and the .NET Framework.

You've taken me on a whole new course and I know how I'll be spending my weekend - trying to learn more about classes which can be called up in PowerShell.  My problem is that on the MSDN website it mentions that these classes come from C#, F#, VB - but I couldn't find anything specifically on .NET - I'm beginning to understand that whatever doesn't readily exist, can pretty much be built.  However, that's still way outside my skill set at this time.  Anything that you can share that will help me to catch up will be greatly appreciated.

*MIND OFFICIALLY  BLOWN*!!!

0 Kudos
LucD
Leadership
Leadership
Jump to solution

That's one of the beauties from PowerShell, you have access to all the .Net classes.

The IPAddress class has some methods.
Beats constructing RegEx expressions to achieve the same :smileygrin:


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

0 Kudos
miguelvelezwhit
Enthusiast
Enthusiast
Jump to solution

Yes, I'm beginning to see the benefits of using this class.  Do you have anywhere to point me where I can learn more about the other available classes in .NET?  I'm going through the IPAddress class now - and yes, it sure beats the crap out of RegEx.  However, since I'm  just getting  back into the whole .NET development thing - actually for the first time taking it truly in-depth, not just cursory glances, I'm looking for a go to reference(s) that my ObiWan recommends.  Can you point me towards what  you consider to be good books/sites?

Thanks as always.

Your devoted Padawan,

MiguelSmiley Happy

0 Kudos
LucD
Leadership
Leadership
Jump to solution

The MSDN pages are of course the ultimate source for the .Net classes.

Which .Net classes to "learn", is of course fully dependent on the task you have at hand.

And hence it is not obvious which .Net classes to explore.

See also Don Jones thread on the subject, aptly named Why the HECK Do You Want to be Taught .NET in a PowerShell Class?!?!?!

In most cases Google (or Bing) is your friend.

Some of the classes that I used, and which turned out to be very useful (random order).

  • System.Diagnostics (application debugging/diagnostics)
  • System.Runtime (application management)
  • System.Timers (timed events, see for example Get-EsxTop – Another Look)
  • System.Text (encoding/decoding)


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

0 Kudos