$list = Get-View -ViewType VirtualMachine | Select name,@{N='IP';E={[string]::Join(',',$_.Guest.ipaddress)}}$list | ?{ $_.ip -eq "1.2.3.4" }
Although I'm not sure why the above still doesn't work, i found the following that may help people. Very useful for Large VM enviroments. (This is what I was trying to script from the above initially).
Using PowerCLI to Find a Specific Guest IP
Or, you could just go about it like this.
Get-VM | Where-Object {$_.Guest.IPAddress -eq '1.1.1.2'}
NOTE: I found that this only works on PowerCLI 6.3, and doesn't work on PowerCLI 5.8. This may be why OP is not getting any results for the 'IP'.
PowerCLI 5.8 (IP field empty):
PowerCLI 6.3 (IP field populated):
Finally found a way to use Get-View AND search across VMs with multiple IPs (including IPv6):
$ip = "192.168"$list = get-view -ViewType VirtualMachine$list | ? {$_.guest.net.IpAddress -match $ip } | select name, @{N='IP';E={[string]::Join(',',$_.Guest.net.IPAddress)}}
It is also important to note that IpAddress is a string[], so if you do anything other than -contains
(like -match
) then you'll need to add an extra layer to your ForEach-Object:
Get-View -ViewType VirtualMachine | Where-Object { ($_.Guest.Net | ForEach-Object { $_.IpAddress | ForEach-Object { $_ -match '^1\.2\.3\.4\d$' } }) -contains $true }
This finds all VMs with IPs in the range 1.2.3.40-1.2.3.49