VMware Cloud Community
leaderone2
Contributor
Contributor
Jump to solution

How to get ESX ip use get-vm CLI

I want to use Get-vm command to get ESX ip, the sample is show below, But it cannot get the EsxIP but the ESX-name,It may has some grammar Error. Who can help me on this, Thanks

I donot want to use get-vmhost command,because it would be very slow

Get-VM | Select Name,id, `

@{Name=’ESXIP’;Expression={$_.VMHost | Select ($_.ExtensionData.Config.Network.Vnic | ? {$_.Device -eq "vmk0"}).Spec.Ip.IpAddress}}

Tags (1)
Reply
0 Kudos
1 Solution

Accepted Solutions
linotelera
Hot Shot
Hot Shot
Jump to solution

Hi, try this:

Get-VM | Select Name, id, @{Name="IP"; Expression={$_.VMHost | Get-VMHostNetwork | Select -ExpandProperty VirtualNic |

where {$_.PortGroupName  -match "pgManagement"} | Select IP}}

Hope this could be useful... pay attention to -match: substitute with your correct portgroup

Regards

View solution in original post

Reply
0 Kudos
5 Replies
jpsider
Expert
Expert
Jump to solution

What about something like:

get-vm | select name,vmhost

or are you trying to find a specific vm?

$vm = <insert vmname>

get-vm $vm | select name,vmhost

Reply
0 Kudos
jpsider
Expert
Expert
Jump to solution

well apparently in my lab I have my ESXhost named via IP.

Does this help? Still runs vmhost, but only against the host with a specific vm:

$vm = '<insert vmname>'

$hostinfo = get-vm $vm | select vmhost

$myhost = $hostinfo.vmhost

get-vmhost $myhost | get-vmhostNetworkAdapter | select IP

Reply
0 Kudos
linotelera
Hot Shot
Hot Shot
Jump to solution

Hi, try this:

Get-VM | Select Name, id, @{Name="IP"; Expression={$_.VMHost | Get-VMHostNetwork | Select -ExpandProperty VirtualNic |

where {$_.PortGroupName  -match "pgManagement"} | Select IP}}

Hope this could be useful... pay attention to -match: substitute with your correct portgroup

Regards

Reply
0 Kudos
Zsoldier
Expert
Expert
Jump to solution

Try this:

  1. Get-VM | Select Name,id, ` 
  2. @{Name="ESXIP";Expression={[system.net.dns]::gethostaddresses($_.VMHost.Name)}}
Chris Nakagaki (中垣浩一)
Blog: https://tech.zsoldier.com
Twitter: @zsoldier
Reply
0 Kudos
leaderone2
Contributor
Contributor
Jump to solution

I have tryed below method,this could run:

#consume more time

Get-VM | Select @{N="ESXip慢";E={((Get-VMHost -VM $_).ExtensionData.Config.Network.Vnic | ? {$_.Device -eq "vmk0"}).Spec.Ip.IpAddress}}

#a quicker method

Get-VM | Select @{Name=’ESXIP’;Expression={ &{$script:esx=get-view -Id $_.vmhostid -property name,config};($script:esx.Config.Network.Vnic | where{$_.Device -eq 'vmk0'}).Spec.Ip.IpAddress }}

Reply
0 Kudos