PowerShell Friday: Retrieving IP addresses for VMs
As I explained in the article about ExtensionData each VM object has a lot of information stored. If you want to know the IP addresses of a particular VM you can use PowerCLI to get those properties.
You can get the IP addresses for VMs from within the virtual machine from the guest property:
(Get-VM -Name MyVM).Guest.IPAddress
if you use this on all VMs you get a list of IP addresses and not knowing which IP address belongs to which VM:
PowerCLI C:\> (Get-VM).Guest.IPAddress 192.168.1.75 fe80::250:56ff:fe81:aa21 192.168.1.78 fe80::250:56ff:fe81:d61 192.168.1.79
Since what we wanted was usable IP addresses we have to do something else/different:
Get-VM -Name MyVM | Select Name, @{N="IP";E={@($_.Guest.IPAddress)}}
This wil return the IP address, or if you have more IP addresses, like IPv6, you get an array with the IP addresses.
Or another way:
One VM
(Get-VMGuest -VM (Get-VM -name MyVM)).IPAddress
Multiple VMs
Get-VM | Get-VMGuest | Format-Table VM, IPAddress
Results in:
VM IP Address -- ---------- control {} Photon {192.168.1.12} dc01 {192.168.3.4} OpenVPN {192.168.1.254,192.168.2.1} vCenter6 { 192.168.1.79, fe80::250:56ff:fe81:d61}
Retrieving IP addresses for all VMs
This is useful if you want to create a list of all your VMs with corresponding IP addresses.
Get-VM | Select Name, @{N="IP";E={@($_.guest.IPaddress)}} | foreach { write-host $_.Name $_.IP}
or
Get-VM | Get-VMGuest | Format-Table VM, IPAddress
The list you get contains the name of the VM and all IP addresses that are known or recognized for the VM. Note that the VMs have to be powered on to retrieve the IP addresses.
Related Posts
2 Comments
Leave a Reply Cancel reply
You must be logged in to post a comment.
you guys are posting very amazing post.keep it up.
Is it possible when retrieving this data for multiple VM’s to exclude IPv6 addresses?