VMware Cloud Community
netjim66
Enthusiast
Enthusiast

Get VM IP, and use it later in the script

I use the following to get a VM's IP (from the OS):

$ip = get-vm | select name, @{name="ip"; expression={foreach($nic in (get-view $_.id).guest.net) {$nic.ipaddress}}}

However it returns this raw string:

@{Name=VF00001; ip=192.168.10.65}

Try setting a machine's IP with that string. As you can guess, it fails.

How do I get the raw IP?

String manipulation does not work in a PowerGUI script.

Tags (2)
0 Kudos
2 Replies
LucD
Leadership
Leadership

That's because the Select-Object creates an object, not a single value.

Try this (if you have only 1 NIC on the VM

$ip = (Get-VM MyVM).Guest.Nics[0].IpAddress[0]


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

netjim66
Enthusiast
Enthusiast

Man.
I've wasted HOURS on this.

Thank You!
That did it.

I'll read up on the "Select Object" since my WMI calls return the same type of strings @{.....}

0 Kudos