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

by Grzegorz Kulikowski

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 ?

[sourcecode language=”powershell”]
$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
[/sourcecode]

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 :

[sourcecode language=”powershell”]
get-view -ViewType virtualmachine -filter @{‘Guest.Hostname’=’vm01.domain.corp’}
[/sourcecode]

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

[sourcecode language=”powershell”]
$SearchIndex.FindAllByDnsName($null,’vm98.domain.corp’,$true)
Type Value
—- —–
VirtualMachine vm-8254
[/sourcecode]

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

[sourcecode language=”powershell”]
get-view -id $SearchIndex.FindAllByDnsName($null,’vm98.domain.corp’,$true)
[/sourcecode]

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:

[sourcecode language=”powershell”]
get-view -id $SearchIndex.FindAllByDnsName($null,’esxi01.domain.corp’,$false)
[/sourcecode]

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:

[sourcecode language=”powershell”]
(get-view -ViewType datacenter -Filter @{‘name’=’OurDC’}).MoRef
[/sourcecode]

And use it with the SearchIndex FindAllByDnsName method

[sourcecode language=”powershell”]
get-view -id $SearchIndex.FindAllByDnsName((get-view -ViewType datacenter -Filter @{‘name’=’OurDC’}).MoRef,’ourVM01.domain.corp’,$true)
[/sourcecode]

Let’s move forward,
FindByIp

We can use Get-View with proper filter like :

[sourcecode language=”powershell”]
get-view -ViewType VirtualMachine -Filter @{‘Guest.IpAddress’=’192.168.0.1′}
[/sourcecode]

or utilize SearchIndex for that

[sourcecode language=”powershell”]
$SearchIndex.FindAByIp($null,’192.168.0.1’,$true)
[/sourcecode]

arguments:

[sourcecode language=”powershell”]
$SearchIndex.FindByIp
OverloadDefinitions
——————-
VMware.Vim.ManagedObjectReference[] FindAllByIp(
VMware.Vim.ManagedObjectReference datacenter,
string ip,
bool vmSearch
)
[/sourcecode]

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

FindByUuid

[sourcecode language=”powershell”]
$SearchIndex.FindByUuid
OverloadDefinitions
——————-
VMware.Vim.ManagedObjectReference FindByUuid(
VMware.Vim.ManagedObjectReference datacenter,
string uuid,
bool vmSearch,
System.Nullable[bool] instanceUuid
)
[/sourcecode]

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:

[sourcecode language=”powershell”]
$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
[/sourcecode]

FindByInventoryPath

[sourcecode language=”powershell”]
$SearchIndex.FindByInventoryPath

OverloadDefinitions
——————-
VMware.Vim.ManagedObjectReference FindByInventoryPath(string inventoryPath)
[/sourcecode]

Use:

[sourcecode language=”powershell”]
$SearchIndex.FindByInventoryPath(‘/Datacenter1/vm/Production/vm01’)
[/sourcecode]

FindByDatastorePath

[sourcecode language=”powershell”]
$SearchIndex.FindByDatastorePath

OverloadDefinitions
——————-
VMware.Vim.ManagedObjectReference FindByDatastorePath(VMware.Vim.ManagedObjectReference datacenter,
string path
)
[/sourcecode]

Use:

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

FindChild

[sourcecode language=”powershell”]
$SearchIndex.FindChild

OverloadDefinitions
——————-
VMware.Vim.ManagedObjectReference FindChild(
VMware.Vim.ManagedObjectReference entity,
string name
)
[/sourcecode]

Use:

[sourcecode language=”powershell”]
$cluster = (Get-View -ViewType ClusterComputeResource -Filter @{‘name’=’ClusterA’}).moref
$SearchIndex.FindChild($x,’esxi01.corp’)
-finds hostA in ClusterA
[/sourcecode]

You may also like

Leave a Reply

Chinese (Simplified)EnglishFrenchGermanHindiPolishSpanish