TheGeekery

The Usual Tech Ramblings

Using PowerCLI to find a specific guest IP

Today I got to play a little with some PowerCLI, VMware’s PowerShell modules. I was helping out our DBA building SQL clusters, when I hit an issue where the cluster software was telling me the IP address I was trying to assign this SQL instance was already in use. A quick ping, and it confirmed it was active. If you’ve read my post on IP conflicts, you’ll know that you can look at the ARP table, and figure out what kind of device is using the IP, and in this case, it turns out to be a VMWare MAC. Instead of going through each of our vmware guests, I jumped into PowerShell…

PowerCLI makes it easy for PowerShell to talk to the APIs on a vSphere server, and query the hosts, and guests, attached. In this case, we were interested in 2 functions, the connection, and the guests.

Connect-VIServer -Server vSphereServer

$match_address = "192.168.1.63"

Get-VM | %{
		$vmIPs = $_.Guest.IPAddress
		foreach($ip in $vmIPs) {
			if ($ip -eq $match_address) {
				"Found VM with matching address: {0}" -f $_.Name
			}
		}
	}

As it turns out, in this case the DBA had mistakenly used the wrong IP from our documentation for the cluster he was working on, so a quick switch and everything was good again.

This is just a tiny sample of what PowerCLI is capable of, what other things do you use it for?

Comments