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.
SearchIndex
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:

$SearchIndex.FindAllByDnsName($null,'vm98.domain.corp',$true)
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,
FindByIp

We can use Get-View with proper filter like :

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

or utilize SearchIndex for that

$SearchIndex.FindAByIp($null,'192.168.0.1',$true)

arguments:

$SearchIndex.FindByIp
OverloadDefinitions
-------------------
VMware.Vim.ManagedObjectReference[] FindAllByIp(
VMware.Vim.ManagedObjectReference datacenter, 
string ip, 
bool vmSearch
)

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

FindByUuid

$SearchIndex.FindByUuid
OverloadDefinitions
-------------------
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:

$SearchIndex.FindByUuid($null,'50394f6f-8408-5340-13e5-0ba59be0c952',$true,$true)
-using VC InstanceUuid
$SearchIndex.FindByUuid($null,'42398a02-5987-483b-211e-2bb7b4ab844f',$true,$false)
-using SMbios Uuid

FindByInventoryPath

$SearchIndex.FindByInventoryPath

OverloadDefinitions
-------------------
VMware.Vim.ManagedObjectReference FindByInventoryPath(string inventoryPath)

Use:

$SearchIndex.FindByInventoryPath('/Datacenter1/vm/Production/vm01')

FindByDatastorePath

$SearchIndex.FindByDatastorePath

OverloadDefinitions
-------------------
VMware.Vim.ManagedObjectReference FindByDatastorePath(VMware.Vim.ManagedObjectReference datacenter, 
string path
)

Use:

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

FindChild

$SearchIndex.FindChild

OverloadDefinitions
-------------------
VMware.Vim.ManagedObjectReference FindChild(
VMware.Vim.ManagedObjectReference entity, 
string name
)

Use:

$cluster = (Get-View -ViewType ClusterComputeResource -Filter @{'name'='ClusterA'}).moref
$SearchIndex.FindChild($x,'esxi01.corp')
-finds hostA in ClusterA
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s