Using SearchIndex in PowerCLI , searching vms by their dns name, ip, and more

Today i want to bring closer something that might not be utilized that often.
What is SearchIndex and what can it do for us you might ask ? Well, best description is at the source 😉 so

The SearchIndex service allows a client to efficiently query the inventory for a specific managed entity by attributes such as UUID, IP address, DNS name, or datastore path. Such searches typically return a VirtualMachine or a HostSystem. While searching, only objects for which the user has sufficient privileges are considered. The findByInventoryPath and findChild operations only search on entities for which the user has view privileges; all other SearchIndex find operations only search virtual machines and hosts for which the user has read privileges. If the user does not have sufficient privileges for an object that matches the search criteria, that object is not returned.

After connecting to virtual center using PowerCLI, you want to initialize the SearchIndex first:

We start from getting ServiceInstance, from the content we take view of the SearchIndex

$si = get-view -id ServiceInstance
$SearchIndex = Get-view -id $si.Content.SearchIndex

What can do SearchIdex for us ?

$SearchIndex | gm -MemberType 'Method'

TypeName: VMware.Vim.SearchIndex

Name                Member
----                ------
Equals              Method
FindAllByDnsName    Method
FindAllByIp         Method
FindAllByUuid       Method
FindByDatastorePath Method
FindByDnsName       Method
FindByInventoryPath Method
FindByIp            Method
FindByUuid          Method
FindChild           Method

How to get a VM with dns name of X, well you can filter that vm from the all vms by looking at their properties for example. Another example is to use get-view with filter on that hostname property :

get-view -ViewType virtualmachine -filter @{'Guest.Hostname'='vm01.domain.corp'}

And now we are going to utilize SearchIndex to do the same:

Type		Value
----		-----
VirtualMachine	vm-8254

In return we have received ManagedObjectReference to a virtualmachine. If we want to see it , we would have to get its view.

get-view -id $SearchIndex.FindAllByDnsName($null,'vm98.domain.corp',$true)

The FindAllByDnsName has 3 arguments, the middle one is obviously the queried dns name. First one is for specifying the datacenter in which you want to look for it. The third, is for choosing the type, when it is set to $true, then we will be looking for a vm with that name, if $false, then we will be looking for a vmhost with that name.
For example if looking up host for specific name:

get-view -id $SearchIndex.FindAllByDnsName($null,'esxi01.domain.corp',$false)  

For the datacenter argument , what is expected is a ManagedObjectReference to a particular datacenter. First we need to get the moref for example of our datacenter:

(get-view -ViewType datacenter -Filter @{'name'='OurDC'}).MoRef

And use it with the SearchIndex FindAllByDnsName method

get-view -id $SearchIndex.FindAllByDnsName((get-view -ViewType datacenter -Filter @{'name'='OurDC'}).MoRef,'ourVM01.domain.corp',$true)

Let’s move forward,

We can use Get-View with proper filter like :

get-view -ViewType VirtualMachine -Filter @{'Guest.IpAddress'=''}

or utilize SearchIndex for that



VMware.Vim.ManagedObjectReference[] FindAllByIp(
VMware.Vim.ManagedObjectReference datacenter, 
string ip, 
bool vmSearch

First and thrid argument usage is the same as in previous exmaple.


VMware.Vim.ManagedObjectReference FindByUuid(
VMware.Vim.ManagedObjectReference datacenter, 
string uuid, 
bool vmSearch, 
System.Nullable[bool] instanceUuid

For vm searches we set the vmSearch to true, then if instanceUuid is set to true we can search for using its instanceUuid instead of bios uuid. for example:

-using VC InstanceUuid
-using SMbios Uuid



VMware.Vim.ManagedObjectReference FindByInventoryPath(string inventoryPath)





VMware.Vim.ManagedObjectReference FindByDatastorePath(VMware.Vim.ManagedObjectReference datacenter, 
string path


$SearchIndex.FindByDatastorePath((get-view -ViewType datacenter -Filter @{'name'='FirstFloorDatacenter'}).MoRef,'[DS09_FastDatastore] VMxyz/VMxyz.vmx')



VMware.Vim.ManagedObjectReference FindChild(
VMware.Vim.ManagedObjectReference entity, 
string name


$cluster = (Get-View -ViewType ClusterComputeResource -Filter @{'name'='ClusterA'}).moref
-finds hostA in ClusterA