VMware Cloud Community
piselloa
Contributor
Contributor
Jump to solution

Find VMs in an IP range and output Name,IP, and Cluster

Hello,

I am trying to create a script to output all VMs that fall into an IP range and output the VM Name, IP address, and Cluster the VM is hosted in. I have gotten so far as to get the output of the VM Name and IP, but cannot seem to get the cluster portion to work.I cannot seem to find an expression for a Cluster attribute in Get-View. Does anyone know of a way to complete this? Or perhaps another way to go about it?

Get-View -ViewType VirtualMachine -Filter @{"Guest.IpAddress"="10.10.10.*"} | select Name,@{N="IP";E={$_.guest.IPAddress}}

OUTPUT:

Name                                                                                                 IP                                                                                                  

----                                                                                                      --                                                                                                  

SERVER1                                                                                          10.10.10.1   

Thanks!

Tony                                                                                 

1 Solution

Accepted Solutions
piselloa
Contributor
Contributor
Jump to solution

Running that kicks back an error for every VM it tries to query (below). I am not sure, but are you able to use -Property and -Id in the same line as -Filter?

Get-View : Parameter set cannot be resolved using the specified named parameters.

At line:15 char:13

+             Get-View -Id $esx.VM -Property Name,Guest.IPAddress -Filter @{"Guest ...

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

    + CategoryInfo          : InvalidArgument: (:) [Get-View], ParameterBindingException

    + FullyQualifiedErrorId : AmbiguousParameterSet,VMware.VimAutomation.ViCore.Cmdlets.Commands.DotNetInterop.GetVIView

Modifying what you have there to remove the -Filter and add back in the where statement did the trick. Runs with no errors, but still takes a minute or two to complete. But that's fine.

foreach($cluster in (Get-View -ViewType ClusterComputeResource -Property Name,Host)){

    foreach($esx in (Get-View -Id $cluster.Host -Property VM)){

        if($esx.VM){

            Get-View -Id $esx.VM -Property Name,Guest.IPAddress | where {$_.guest.IpAddress -like "10.10.10.*"} |

            Select Name,@{N='Cluster';E={$cluster.Name}},@{N='IP';E={$_.guest.IPAddress}}

        }

    }

}

If I wanted to filter by Vlan name or VlanID would I be able to swap out the Guest.IpAddress property for a related VLAN property?

Thank you for the help!!!

View solution in original post

Reply
0 Kudos
11 Replies
LucD
Leadership
Leadership
Jump to solution

Why don't you turn it around ?

First get the clusters, then the VMs on each host in that cluster.

Something like this

foreach($cluster in (Get-View -ViewType ClusterComputeResource)){

    foreach($esx in (Get-View -Id $cluster.Host)){

        Get-View -Id $esx.VM |

        Select Name,@{N='Cluster';E={$cluster.Name}}

    }

}


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

piselloa
Contributor
Contributor
Jump to solution

LucD thank you for your help!

But as I am fairly new to scripting, I'm having trouble figuring out where the IP filter would fit in. By adding the @{N="IP";E={$_.guest.IPAddress}} into the select field the script will display VM Name, IP and Cluster name, but this is polling every VM in the vCenter.

I have not been able to successfully fit in the -Filter @{"Guest.IPAddress"="10.10.10.*"} yet. As that is the subnet that I want to pull the information from.

Could this be done with another foreach or a Where statement?

Reply
0 Kudos
piselloa
Contributor
Contributor
Jump to solution

Got it..... kind of

foreach($cluster in (Get-View -ViewType ClusterComputeResource)){

  foreach($esx in (Get-View -Id $cluster.Host)){

  Get-View -Id $esx.VM | where {$_.guest.IpAddress -like "10.10.10.*"} |

  Select Name,@{N="IP";E={$_.guest.IPAddress}},@{N='Cluster';E={$cluster.Name}}

  }

}

Name                                                              IP                                                                  Cluster                                                           

----                                                                   --                                                                   -------                                                           

SERVER1                                                       10.10.10.1                                                       CLUSTER01                                                   

SERVER2                                                       10.10.10.2                                                       CLUSTER02         

However, the script seems to take forever to run and comes back with the following error. The script runs and lists servers, then this error comes up twice, then it seems to switch clusters and continue.


Get-View : Cannot validate argument on parameter 'Id'. The argument is null, empty, or an element of the argument collection contains a null value. Supply a collection that does not contain any null

values and then try the command again.

At C:\scripts\find-server-by-IP.ps1:6 char:16

+         Get-View -Id $esx.VM | where {$_.guest.IpAddress -like "10.10.10.*.*"} |

+                      ~~~~~~~

    + CategoryInfo          : InvalidData: (:) [Get-View], ParameterBindingValidationException

    + FullyQualifiedErrorId : ParameterArgumentValidationError,VMware.VimAutomation.ViCore.Cmdlets.Commands.DotNetInterop.GetVIView

                                  

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

That would be because there are probably ESXi nodes with no VMs on them.

Try like this, should be a bit faster as well

foreach($cluster in (Get-View -ViewType ClusterComputeResource -Property Name,Host)){

    foreach($esx in (Get-View -Id $cluster.Host -Property VM)){

        if($esx.VM){

            Get-View -Id $esx.VM -Property Name,Guest.IPAddress -Filter @{"Guest.IpAddress"="10.10.10.*"|

            Select Name,@{N='Cluster';E={$cluster.Name}},@{N='IP';E={$_.guest.IPAddress}}

        }

    }

}

 


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

piselloa
Contributor
Contributor
Jump to solution

Running that kicks back an error for every VM it tries to query (below). I am not sure, but are you able to use -Property and -Id in the same line as -Filter?

Get-View : Parameter set cannot be resolved using the specified named parameters.

At line:15 char:13

+             Get-View -Id $esx.VM -Property Name,Guest.IPAddress -Filter @{"Guest ...

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

    + CategoryInfo          : InvalidArgument: (:) [Get-View], ParameterBindingException

    + FullyQualifiedErrorId : AmbiguousParameterSet,VMware.VimAutomation.ViCore.Cmdlets.Commands.DotNetInterop.GetVIView

Modifying what you have there to remove the -Filter and add back in the where statement did the trick. Runs with no errors, but still takes a minute or two to complete. But that's fine.

foreach($cluster in (Get-View -ViewType ClusterComputeResource -Property Name,Host)){

    foreach($esx in (Get-View -Id $cluster.Host -Property VM)){

        if($esx.VM){

            Get-View -Id $esx.VM -Property Name,Guest.IPAddress | where {$_.guest.IpAddress -like "10.10.10.*"} |

            Select Name,@{N='Cluster';E={$cluster.Name}},@{N='IP';E={$_.guest.IPAddress}}

        }

    }

}

If I wanted to filter by Vlan name or VlanID would I be able to swap out the Guest.IpAddress property for a related VLAN property?

Thank you for the help!!!

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Yes, Id and Property can go on the same Get-View, at least that is what the Synatx of the cmdlet is telling me.

Which PowerCLI version are you using ? Do a Get-PowerCLIVersion

I don't think the VLANid can be obtained directly from the VirtualMachine object.

So you would have to add some logic.

From the VM you can get the networkname (portgroupname), and from there you should be able to obtain the VLANid


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

Reply
0 Kudos
piselloa
Contributor
Contributor
Jump to solution

PowerCLI Version

----------------

   VMware vSphere PowerCLI 5.0 build 435427

---------------

Snapin Versions

---------------

   VMWare AutoDeploy PowerCLI 5.0 build 575

   VMWare ImageBuilder PowerCLI 5.0 build 575

   VMware License PowerCLI 5.0 build 395016

   VMWare vSphere PowerCLI 5.0 build 435427

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

I think you're up for an update (we're currently at 6.3) :smileygrin:


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

Reply
0 Kudos
piselloa
Contributor
Contributor
Jump to solution

After upgrading to 6.3 I get a different error message

Get-View : Parameter set cannot be resolved using the specified named parameters.

At line:24 char:13

+             Get-View -Id $esx.VM -Property Name,Guest.IPAddress -Filter @{"Guest ...

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

    + CategoryInfo          : InvalidArgument: (:) [Get-View], ParameterBindingException

    + FullyQualifiedErrorId : AmbiguousParameterSet,VMware.VimAutomation.ViCore.Cmdlets.Commands.DotNetInterop.GetVIView

But as far as the VLAN goes, using the portgroup name would be acceptable. Do you happen to know the Get-View property name for portgroupname? I've tried NetworkName but that doesnt seem to be it.

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

You can't mix Id and Filter I'm afraid.

get-view.jpg

Depending if you use VSS or VSD, that should be

Get-View -ViewType Network

Get-View -ViewType DistributedVirtualPortgroup


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

piselloa
Contributor
Contributor
Jump to solution

Thanks for the help on this one, I believe I have what I need for now.

Also, I just stumbled upon this function that does exactly what I need (but with portgroup instead of IP) for anyone else looking for something similar.

vNugglets: Get VMs by Virtual Port Group with PowerCLI

Thanks LucD‌ !!