VMware Cloud Community
estanev
Enthusiast
Enthusiast
Jump to solution

Search VM by IP - wildcard output

Good day,

I have the following one-liner which i'm using to search VM by specific IP address via Slack (hubot integrated with vSphere and Slack):

Get-View -ViewType VirtualMachine -Filter @{”Guest.IpAddress”=”$1"} | Select Name, @{N="Folder";E={Get-View $_.Parent | Select -ExpandProperty Name}}

where $1 is the IP address. If the IP address is for example 192.168.0.13 the output contains all VMs starting with IP 192.168.0.13 (wildcard output, same as @{”Guest.IpAddress”=”^$1"} ) :

Name -- Folder

vm4    Test  192.168.0.13

vm15  Test  192.168.0.135

vm23  Test  192.168.0.139

How can i filter the output to list only the VM with the exact IP which im searching for (in that example it should list only vm4)?

The second question is - how can i search within all VM NICs? Currently the output return results only if the IP address is assigned to the first NIC but i have many VMs with multiple networks.

Thank you for your time!

Have a great day!

1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

The Filter expression on the Get-View cmdlet is a RegEx expression.

So you could try:

-Filter @{'Guest.IpAddress'="^$($1)$"}


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

View solution in original post

Reply
0 Kudos
8 Replies
LucD
Leadership
Leadership
Jump to solution

The Filter expression on the Get-View cmdlet is a RegEx expression.

So you could try:

-Filter @{'Guest.IpAddress'="^$($1)$"}


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

Reply
0 Kudos
estanev
Enthusiast
Enthusiast
Jump to solution

Thanks alot for pointing me to the right direction, i've found some RegEx manuals/examples and

-Filter @{”Guest.IpAddress”=”\b$1\b"}

did the trick.

Reply
0 Kudos
estanev
Enthusiast
Enthusiast
Jump to solution

Any clue where i can find the available filters for Get-View? I got error while trying to filter all IPs with:

-Filter @{”Guest.IpAddress[0]”=”192.168.0.13" ; ”Guest.IpAddress[1]”=”192.168.0.13" ...}

Get-View : 1/5/17 8:25:46 AM    Get-View           

At /tmp/ff168056-5fc5-45fb-b95c-79f9a3fa7539.ps1:1 char:1

+ Get-View -ViewType VirtualMachine -Filter @{"Guest.IpAddress[0]"="31. ...

+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    + CategoryInfo          : NotSpecified: (:) [Get-View], InvalidProperty

    + FullyQualifiedErrorId : Client20_MoServiceImpl_GetNetInteropEntityView_V

   iError,VMware.VimAutomation.ViCore.Cmdlets.Commands.DotNetInterop.GetVIVie 

  w


I'm stuck at finding the proper filter to check all Guest IPs/NICs, not only the first one.

Thanks again!

Reply
0 Kudos
estanev
Enthusiast
Enthusiast
Jump to solution

Ok just realized that there is no way to filter on arrays ... searching all IPs with Get-VM took 9 minutes to complete Smiley Sad

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Afaik the Guest.IpAddress property is not an array.

It's a string that contains the primary IP address.

Are you trying to find VMs where the IP address could not be the primary IP address?


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

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Is this any faster than the regular Get-VM?

$tgtIp = '192.168.1.1'

Get-View -ViewType VirtualMachine |

where{$_.Guest.Net.ipconfig.ipaddress.ipaddress -contains $tgtIp} |

Select Name,@{N='IP';E={$_.Guest.Net.ipconfig.ipaddress.ipaddress -join '|'}}


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

estanev
Enthusiast
Enthusiast
Jump to solution

Thank you for your reply!

Are you trying to find VMs where the IP address could not be the primary IP address?

Yes, exactly.

$tgtIp = '192.168.1.1'

Get-View -ViewType VirtualMachine |

where{$_.Guest.Net.ipconfig.ipaddress.ipaddress -contains $tgtIp} |

Select Name,@{N='IP';E={$_.Guest.Net.ipconfig.ipaddress.ipaddress -join '|'}}

Get-View is much faster (12-15 secs to compete which is perfect). The output from the command above is strange - only PoweredOff or VMs with no VMware Tools installed.

Reply
0 Kudos
Redhatcc
Enthusiast
Enthusiast
Jump to solution

Thanks LucD, this worked like magic! I just added " | Format-Table" on the end.

Reply
0 Kudos