Using PowerCLI to get the IP address of a VM

PowerShell Scripting VMware

Here is a simple but handy PowerCLI one liner which can output the VM name and it’s IP address.

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

Name                                            IP Address
—-                                            ———-
ABWEB1v                                         172.16.100.1
ABWEB2v                                         172.16.100.2
ABWEB3v                                         172.16.100.3
ABWEB4v                                         172.16.100.4
ABWEB5v                                         172.16.100.5
ABWEB6v                                         172.16.100.6
ABWEB7v                                         172.16.100.7
ABWEB8v                                         172.16.100.8
ABWEB9v                                         172.16.100.9
ABWEB10v                                        172.16.100.10
ABWEB11v                                        172.16.100.11
ABAPP1v                                         172.16.101.20
ABDOM1v                                         172.16.102.20

Getting a little more complicated here we are getting all the VMs in any cluster starting with “Web-Cluster-” and then returning the VM Name, ESXi host and IP address:

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

Due to the way the IP address is referenced and a VM can have more than one IP, you can list additional IPs by adding or changing the array pointer from 0 to 1 and so on, in this part of the command “$_.guest.IPAddress[1]”

Source article: https://www.vmadmin.co.uk/resources/48-vspherepowercli/403-using-powercli-to-get-the-ip-address-of-a-vm

Leave a Reply

Your email address will not be published. Required fields are marked *