• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Crazy Consultant

Focus on cloud-native, AI and virtualization technologies

  • Home
  • Knowledge Library
    • Golden Kubestronaut Study Guide
    • Nutanix Study Guide
    • Nutanix Expert Challenge
    • Enterprise Kubernetes Platform Design Dimensions
    • Kubestronaut Challenge
    • Multiple‑Choice Exam Tips
  • News
  • Onyku®

Find VM by IP via API SearchIndex PowerCLI

June 20, 2016 by Christophe Calvet 1 Comment

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.

Filed Under: PowerCLI, PowerShell, VMware

Reader Interactions

Trackbacks

  1. PowerCLI study guide – core concepts - The Crazy Consultant says:
    October 31, 2016 at 11:24 pm

    […] If you have used the vSphere client you are probably, familiar with the “Search Inventory” It is possible to use it to find matches by Virtual Machine IP address…and it is very fast. So do we have an API to perform the same search? Yes […]

    Reply

Leave a Reply Cancel reply

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

Primary Sidebar

Connect with me

  • LinkedIn

Featured Credentials

Latest News

  • Onyku® at Norwegian VMUG 19 May 2025
  • NCM-MCI exam tips
  • Open Knowledge Diagrams with Mermaid and Git
  • Start of a journey
  • Banned from Lego – customer review