CFormage
Enthusiast
Enthusiast

List all IP addresses

Jump to solution

I am trying to get all IP addresses on a vm and the associated portgroup.

$vm.Guest.IPAddress

I have the Vlan

$vmvlan = Get-VirtualPortgroup -VM $_.Name | %{$_.VlanId}

Now if there are more than 1 Vlan I can get it like this

$vmvlan[0]

$vmvlan[1]

Does anyone know how can I do the same for the IP addresses?

Blog: www.dcinfrastructure.blogspot.com
1 Solution

Accepted Solutions
oreillydata
Contributor
Contributor

CFormage,

I think you might be doing something wrong. Maybe take out the echo command.

Here is what I get running those commands on my setup. Ps. I am running vCenter 4.1 and ESXi 4.1.

[vSphere PowerCLI] C:\> $vm= get-vm vmname
[vSphere PowerCLI] C:\> $vm.guest.IPAddress
0.0.0.0
10.3.59.102
[vSphere PowerCLI] C:\> $vm.guest.IPAddress[0]
0.0.0.0
[vSphere PowerCLI] C:\> $vm.guest.IPAddress[1]
10.3.59.102

View solution in original post

0 Kudos
8 Replies
LucD
Leadership
Leadership

You can address the individual IP addresses like this (similar to your vlan array) when there is more than one

$vm.Guest.IPAddress[0]

$vm.Guest.IPAddress[1]

...

Or did you mean something else ?

You can automate this in a loop

foreach($ip in $vm.Guest.IpAddress){

     $ip

}

That will return all IP addresses.


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

CFormage
Enthusiast
Enthusiast

When I do use

$vm.Guest.IPAddress[0]

$vm.Guest.IPAddress[1]

I am not getting the whole IP addresses, I am getting the first character of the IP address.

[vSphere PowerCLI] C:\> $VMsAdv = Get-VM TESTVM |  Get-View
[vSphere PowerCLI] C:\> echo  $VMsAdv.Guest.IpAddress[0]
1
[vSphere PowerCLI] C:\> echo  $VMsAdv.Guest.IpAddress[1]
7
[vSphere PowerCLI] C:\> echo  $VMsAdv.Guest.IpAddress
172.20.198.5
[vSphere PowerCLI] C:\>

From the vSphere client when I click "View all" on the summary tab I do see both IP addresses.

I prefer in this specific case not to use a loop. and it does not give me both also.

[vSphere PowerCLI] C:\> foreach($ip in $vm.Guest.IpAddress){
>>
>>      $ip
>>
>> }
>>
192.168.16.205

[vSphere PowerCLI] C:\>

p.s. Thankyou for your quick reply.

Blog: www.dcinfrastructure.blogspot.com
0 Kudos
oreillydata
Contributor
Contributor

CFormage,

I think you might be doing something wrong. Maybe take out the echo command.

Here is what I get running those commands on my setup. Ps. I am running vCenter 4.1 and ESXi 4.1.

[vSphere PowerCLI] C:\> $vm= get-vm vmname
[vSphere PowerCLI] C:\> $vm.guest.IPAddress
0.0.0.0
10.3.59.102
[vSphere PowerCLI] C:\> $vm.guest.IPAddress[0]
0.0.0.0
[vSphere PowerCLI] C:\> $vm.guest.IPAddress[1]
10.3.59.102
0 Kudos
RvdNieuwendijk
Leadership
Leadership

It looks like the virtual machine has only one IP address. In this case the $VMsAdv.Guest.IpAddress object isn't a array but is a string. If you use @() arround it, you make it an array in all cases. So:

@($VMsAdv.Guest.IpAddress)[0]


should give you the first IP address. With:

@($VMsAdv.Guest.IpAddress).Length


you can see how many IP addresses there are.

Regards, Robert

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
0 Kudos
LucD
Leadership
Leadership

Which PowerCLI version are you running ?

Can you do a

Get-PowerCLIVersion

In PowerCLI 4.1 U1 the IPAddress property seems to be always an array, even when there is only 1 entry.

The following returns a value of 1 for me if there is only 1 IP address

Can you check on your side ?

$vm.Guest.IPAddress.Count


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

0 Kudos
CFormage
Enthusiast
Enthusiast

I am using vSphere PowerCLI 4.1 build 264274

The problem is the pipe to get-view, without it everything works.

[vSphere PowerCLI] C:\> $VMsAdv = Get-VM TESTVM
[vSphere PowerCLI] C:\> $VMsAdv.guest.IPAddress
172.20.198.5
192.168.31.149
[vSphere PowerCLI] C:\> echo $VMsAdv.guest.IPAddress
172.20.198.5
192.168.31.149
[vSphere PowerCLI] C:\> $VMsAdv = Get-VM TESTVM | Get-View
[vSphere PowerCLI] C:\> echo $VMsAdv.guest.IPAddress[0]
1
[vSphere PowerCLI] C:\> $VMsAdv.guest.IPAddress[1]
7

Blog: www.dcinfrastructure.blogspot.com
0 Kudos
LucD
Leadership
Leadership

I must have missed that Get-View cmdlet in there.

Why do you use Get-View, that is not needed for waht you are trying to do.

In short, there are 2 types of objects availabe in PowerCLI.

1) The .NET objects. These are the ones returned by the PowerCLI cmdlets

2) The SDK objects. These are the objects used by vSphere and they are documented in VMware vSphere API Reference Documentation

With the Get-VIew cmdlet you can go from 1) to 2).

From PowerCLI 4.1 onwards, you can get at the SDK object through the Extensiondata property of the .NET object

So the following two statements are equivalent

$vmSDK1 = Get-VM MyVM | Get-View

$vmSDK2 = (Get-VM MyVM).Extensiondata

In your case you do not need to access the SDK object.


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

0 Kudos
ChrisJ615
Enthusiast
Enthusiast

I know this is an old thread and marked correct in 2011, but it looks like it was done a bit the hard way.  Check it out - all on one line

Get-VirtualPortGroup | ?{$_.Name -eq "Port-Group-Goes-Here"} | Get-VM | Select Name, {$_.Guest.IPAddress}