且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Using PowerCLI to get the IP address of a VM

更新时间:2022-09-16 16:45:32

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]"

本文转自学海无涯博客51CTO博客,原文链接http://blog.51cto.com/549687/1842533如需转载请自行联系原作者

520feng2007