Find VM by IP via API SearchIndex PowerCLI

Find a VM based on the IP extracted from VMware tools using the API “SearchIndex”

One of the hidden gem in the vSphere API is the managed object “SearchIndex“.

There are many scripts in the PowerCLI community to find a VM based on the IP address.
Many of them are based on the following workflow:
Extracting all or a subset of VMs
Identifying all IP for these VMs
Filter to keep only the VMs with the relevant IPs

However it turns out that there is a native function in the vSphere API to find a VM based on the IP address.
I have not tested the performance yet but i guess it should be faster than the traditional approach for a search in the whole inventory.

The script below has been created to use this function from PowerCLI.

Find-VmByIp

function Find-VmByIp{
<#
.SYNOPSIS
Find a VM based on the IP extracted from VMware tools using the API "SearchIndex"

.DESCRIPTION
Find a VM based on the IP extracted from VMware tools.
Based on the API managed object "SearchIndex" and the function "FindByIp"
You must be connected to one vCenter before executing this script.

.NOTES
Author: Christophe Calvet
Blog:http://thecrazyconsultant.com

.LINK
API SearchIndex
http://pubs.vmware.com/vsphere-60/index.jsp#com.vmware.wssdk.apiref.doc/vim.SearchIndex.html

.PARAMETER IpTable
The objects in the table must be of type IPv4 or IPv6

.EXAMPLE
$IpArray = @('10.0.35.1','10.0.34.1','10.0.69.1','fe80::433:c670:40ad:99a4')
find-VmByIp -iptable $IpArray | select-object -unique IP,VM | ogv
#>
	param(
	[Parameter(Mandatory=$true)]
	[ipaddress[]]$IpArray
	)
		process{
			try{
				$si = Get-View -id "SearchIndex-SearchIndex"
					$IpArray | foreach-object{
					$IP  = $_
					$SearchResult = $si.FindAllByIp($null,$IP,$true)
						if($SearchResult){
							$SearchResult | foreach-object{							
								$Report = New-Object -Type PSObject -Prop ([ordered]@{
								'IP' = $IP
								'VM' = get-viobjectByViView $_
								 })		 
								 Return $report							
							}
						}
						Else{
							$Report = New-Object -Type PSObject -Prop ([ordered]@{
							'IP' = $IP
							'VM' = 'No VM Found'
							 })		 
							 Return $report
						}
					}
			}
			Catch{
			Write-error $_
			}
		}
}

#Example
$IpArray = @('10.0.35.1','10.0.34.1','10.0.69.1','fe80::433:c670:40ad:99a4')
find-VmByIp -IPArray $IpArray | select-object -unique IP,VM | ogv

FindAllByIP or FindByIp?
FindAllByIP returns all VMs with a match and sometimes more than one match for one VM.
This behaviour is maybe linked to advanced settings within the OS – 2012 R2 NIC Teaming
This is the reason behind the “select-object -unique”

FindByIp returns only the first VM with a match.

Bonus:
The third parameter of the function “FindAllByIp” is “vmSearch”.
Use $false instead of $true to find host by IP.

The first parameter is the datacenter, it is possible to edit the script to use it as a parameter and reduce the scope of the search.

One thought on “Find VM by IP via API SearchIndex PowerCLI

  1. Pingback: PowerCLI study guide – core concepts - The Crazy Consultant

Leave a Reply

Your email address will not be published. Required fields are marked *