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?
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.
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
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.
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.
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
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
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.IPAddress172.20.198.5192.168.31.149[vSphere PowerCLI] C:\> echo $VMsAdv.guest.IPAddress172.20.198.5192.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
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
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}