VMware Cloud Community
LikeABrandit
Enthusiast
Enthusiast
Jump to solution

Find VM via IP

Hey all,

I'm trying to figure out a way to find a VM via IP through PowerCLI. That in itself is easy enough, and I'm using this code currently:

Get-View -ViewType VirtualMachine -Filter @{"Guest.IpAddress"="x.x.x.x"}

The issue is that the IP I need to track by isn't directly on the Guest.IpAddress property (multiple NICs), but it is actually located at Guest.Net.IpAddress. I can find the IP by using:

Get-View -ViewType VirtualMachine -Filter @{"Name"="VMName"} | Select -ExpandProperty Guest | Select -ExpandProperty Net | Select -ExpandProperty IpAddress

This returns three IPs and one is the IP I need. What I can't figure out how to do is filter using it. Trying the following results in invalid property, and I imagine it's because it's an array:

Get-View -ViewType VirtualMachine -Filter @{"Guest.Net.IpAddress"="x.x.x.x"}

Anyone know how I could get this filter working?

~Brandit

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Afaik the Name part of a filter name-value pair needs to be the name of a property that holds a scalar value.

No arrays or nested objects I'm afraid.

Remember, the filter does a RegEx with the value on the content of the property specified by name.


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

View solution in original post

0 Kudos
3 Replies
LikeABrandit
Enthusiast
Enthusiast
Jump to solution

I managed to find a way to do this sort of. The idea I'm using at the moment that "works" somewhat, just gotta do some additional manipulation since I get arrays and properties back for IP. I'd still be interested in getting code that filters at the vCenter level if possible.

Here's my final code at the moment:

$input = ipcsv "input.csv"

$list = @()

foreach ($row in $input) {

    $VM = Get-View -ViewType VirtualMachine | Select Name, @{N="IP";E={$_ | `

        Select -ExpandProperty Guest | `

        Select -ExpandProperty Net | `

        Select -ExpandProperty IpAddress}} | `

        Where {$_.IP -contains $row.IP}

    $list += $VM

}

$list

~Brandit

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Afaik the Name part of a filter name-value pair needs to be the name of a property that holds a scalar value.

No arrays or nested objects I'm afraid.

Remember, the filter does a RegEx with the value on the content of the property specified by name.


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

0 Kudos
LikeABrandit
Enthusiast
Enthusiast
Jump to solution

Thanks Luc,

That would explain why I couldn't figure out a way to do it. 😃 I'm still pretty new to coding/scripting but I'm enjoying learning it. Maybe there'll be a way to filter by arrays or nested objects in a later revision. It would be pretty helpful in certain situations.

Thanks for the info!

~Brandit

0 Kudos