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