VMware Cloud Community
BlackHawk-VM
Contributor
Contributor
Jump to solution

Get-VM & Get-VMGuest: How to match NetworkName up with IPAddress

Hi all,

I can get the network names of a VM.

(get-vm -name My-VM-Name).NetworkAdapters

I can get the IP adresses from a guest.

(Get-VMGuest -VM My-VM-Name).ipaddress

But when I try to get both they don't match up. The IP does not belong to the named network. They don't appear to be stored the same in there internal arrays. Is there a relationship defined somewhere to get these together so I can list the IPs assigned and the network names they are on?

Thx!

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Provided the guest is running and you have VMware tools installed, you can do something like this:

Get-VM | %{
  Write-Host $_.Name
  foreach($nic in $_.Guest.Nics){
    Write-Host $nic.NetworkName $nic.IPAddress
  }
}


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

View solution in original post

Reply
0 Kudos
8 Replies
LucD
Leadership
Leadership
Jump to solution

Provided the guest is running and you have VMware tools installed, you can do something like this:

Get-VM | %{
  Write-Host $_.Name
  foreach($nic in $_.Guest.Nics){
    Write-Host $nic.NetworkName $nic.IPAddress
  }
}


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

Reply
0 Kudos
BlackHawk-VM
Contributor
Contributor
Jump to solution

Thanks a ton!

That whole object.object.object.property notation is rather new to me.

I can see it is very powerful though.

Ciao

Reply
0 Kudos
SimonM1
Contributor
Contributor
Jump to solution

That line doesnt work for me (unexpected token 'in' in expression or statement...)

I think you need to include a semi-colon before the foreach statement, i.e.:

Get-VM | %{ Write-Host $_.Name; foreach($nic in $_.Guest.Nics){ Write-Host $nic.NetworkName $nic.IPAddress } }

Thanks!

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

That's probably because you think it was a one-liner.

It was not, the sample script contained multiple lines.

That is a known issue with IE and the forum SW Smiley Sad


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

Reply
0 Kudos
SimonM1
Contributor
Contributor
Jump to solution

Ah WOW ! Switched to Firefox and all becomes clear, I see your line breaks now.

Many thanks LucD.

Reply
0 Kudos
gdesmo
Enthusiast
Enthusiast
Jump to solution

Hello... What is the issue with IE and the forum? How would I need to type in your above script to get it to work? Thanks

Reply
0 Kudos
halr9000
Commander
Commander
Jump to solution

Hello... What is the issue with IE and the forum?

In order to get syntax highlighting and (some) protection from code being mangled by the wiki-formatting nature of this forum software, you can use the and tags in your posts. (You remove the spaces between the words and the braces in order for it to work.) It works great when viewed from FF, but not so well from IE or Chrome. I have been informed that this will be resolved in a forum upgrade to happen in November.

How would I need to type in your above script to get it to work?

View it in FF to see the proper line-breaks, or ask for it to be saved as an attachment or posted elsewhere (such as pastebin.com). Hopefully this problem will go away next month.






Author of the upcoming book: Managing VMware Infrastructure with PowerShell

Co-Host, PowerScripting Podcast (http://powerscripting.net)

My signature used to be pretty, but then the forum software broked it. vExpert. Microsoft MVP (Windows PowerShell). Author, Podcaster, Speaker. I'm @halr9000
Reply
0 Kudos
admin
Immortal
Immortal
Jump to solution

The other workaround is to reply to the message and quote it (press the quote icon to bring the parent message in). For whatever reason it will render properly then.

Reply
0 Kudos