8

I am trying to locate specific VM's based from IP addresses in PowerCLI. I found this script online Grabbing VM ipaddress via PowerCLI

The intial question explaines the issues I was having, and the answer looks to resolve such issues, however when I run such script:

Get-View -ViewType VirtualMachine | Select @{N='IP';E={[string]::Join(',',$_.Guest.net.IPAddress)}}

All I get is the following output:

IP
--

And that's it... Am I missing input such as specifying a cluster or DC, does this work for anyone else?

StackUser_py
  • 173
  • 1
  • 1
  • 6
  • 1
    Try removing the _select_ part and see what you get. Do the VM objects have a property _VM.Guest.net.IPAddress_? –  Mar 02 '15 at 09:15

6 Answers6

8

Get-View

As KERR pointed out, the code in bxm's answer is faster than the code in my alternative solution below. [It was, consistently, 4 times faster for me instead of 10 times faster as KERR claims; but still faster.]

But note tho that for the view objects returned by Get-View, the Guest.IPAddress property consists of a single address and it may not even be an address for a NIC (it may be, e.g. a VPN connection).

Here's a one-line (tweaked) version of bxm's code:

Get-View -ViewType VirtualMachine | ?{ $_.Guest.IPAddress -eq "1.2.3.4" }

and here's a version that should check all of the NIC addresses:

Get-View -ViewType VirtualMachine | ?{ ($_.Guest.Net | %{ $_.IpAddress }) -contains "1.2.3.4" }

where "1.2.3.4" is the IP address for which you want to find the corresponding VM.

Note that my version is slightly different than bxm's. bxm's version effectively ensures that any matching VMs only have the specified IP address assigned and no others (or, rather, it would if the Guest.IPAddress property was an array). My version only ensures that the VM has that specified address, regardless of any other IP addresses it's assigned.

Get-VM

Here's my adaptation of the code at the link provided by StackUser_py's answer:

Get-VM | Where-Object -FilterScript { $_.Guest.Nics.IPAddress -contains "1.2.3.4" }

Note tho that these two solutions return different results, the first an (array of) VirtualMachine (objects), and the second a UniversalVirtualMachineImpl. However, calling Get-VM and passing it the name of the VM returned by the first solution does not significantly alter the duration.

Kenny Evitt
  • 9,291
  • 5
  • 65
  • 93
  • 1
    @KERR Thanks for your help! Please delete your comments here (as they're no longer relevant to the answer as it is now). – Kenny Evitt Oct 04 '17 at 16:26
  • 2
    The plot thickens - while the Get-View method is faster, it only returns (searches) the first IP address of the VM. The Get-VM method works on VMs with multiple IPs. – KERR Oct 10 '17 at 05:30
  • @KERR The IP address via `Get-View` for one of my VMs was for a VPN, not even one of the two NICs! `Get-VM` only returned the addresses for the two NICs. I can't find the VPN IP address, or any other info for the relevant network connection, anywhere else in the view object for that VM. – Kenny Evitt Oct 10 '17 at 14:58
2

I got the command working with a small tweak to the objects used, like so:

$list = Get-View -ViewType VirtualMachine | Select name,@{N='IP';E={[string]::Join(',',$_.Guest.ipaddress)}}
$list | ?{ $_.ip -eq "1.2.3.4" }
bxm
  • 484
  • 4
  • 10
  • You can also use this as a one-liner by separating the commands with a semicolon eg. $list = Get-View -ViewType VirtualMachine | Select name,@{N='IP';E={[string]::Join(',',$_.Guest.ipaddress)}} ; $list | ?{ $_.ip -eq "1.2.3.4" } – KERR Oct 04 '17 at 23:09
  • `$_.Guest.ipaddress` is a (single) string and not an array so the 'string join' expression is unnecessary. – Kenny Evitt Oct 10 '17 at 16:12
  • FYI this method will only list and query the first IP of the VM. – KERR Oct 10 '17 at 23:48
1

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

StackUser_py
  • 173
  • 1
  • 1
  • 6
1

Or, you could just go about it like this.

Get-VM | Where-Object {$_.Guest.IPAddress -eq '1.1.1.2'}
Dubz
  • 11
  • 1
0

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 5.8

PowerCLI 6.3 (IP field populated): PowerCLI 6.3

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)}}

enter image description here

KERR
  • 1,312
  • 18
  • 13
0

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

Hossy
  • 139
  • 1
  • 2
  • 11