PowerCLI

 View Only
  • 1.  Search VM by IP - wildcard output

    Posted Jan 05, 2017 10:25 AM

    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!



  • 2.  RE: Search VM by IP - wildcard output
    Best Answer

    Posted Jan 05, 2017 11:14 AM

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

    So you could try:

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


  • 3.  RE: Search VM by IP - wildcard output

    Posted Jan 05, 2017 12:32 PM

    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.



  • 4.  RE: Search VM by IP - wildcard output

    Posted Jan 05, 2017 01:55 PM

    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!



  • 5.  RE: Search VM by IP - wildcard output

    Posted Jan 05, 2017 04:01 PM

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



  • 6.  RE: Search VM by IP - wildcard output

    Posted Jan 05, 2017 06:53 PM

    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?



  • 7.  RE: Search VM by IP - wildcard output

    Posted Jan 05, 2017 07:08 PM

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



  • 8.  RE: Search VM by IP - wildcard output

    Posted Jan 09, 2017 10:16 AM

    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.



  • 9.  RE: Search VM by IP - wildcard output

    Posted Nov 08, 2019 09:48 AM

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