In this article we will go over a simple script that lists all the empty resource pools in a VMware Cluster. This script comes in handy and is a very nice addition to any scripting repository where there are large VMware deployments.
#Variable Declaration Param( [Parameter(Mandatory=$true)][string]$server, [Parameter(Mandatory=$true)][string]$username, [Parameter(Mandatory=$true)][string]$password, [Parameter(Mandatory=$true)][string]$cluster ) #Module Initialization Add-PSSnapin VMware.VimAutomation.Core Connect-VIServer -Server $server -Username $username -Password $password $respools = Get-Cluster $cluster | Get-ResourcePool ForEach ($respool in $respools) { $vms = Get-ResourcePool -Name $respool | Get-VM If ( $vms -eq $null) { Write-Host $respool } }
The following walkthrough will be an easy one.
The first part of the script defines the parameters that we will use when running the script. The parameters are the username and password of the administrative user, the name of the vCenter Server, and the name of the Cluster.
Afterwards we import the PowerCLI snapin to have access to the PowerCLI cmdlets and we connect to the vCenter server using the parameters we provided.
Next, we create a variable named $respools that holds the output of the Get-Cluster command. The Get-Cluster command receives the name of the cluster as its parameter and its output is sent to the Get-ResourcePool cmdlet to list all the resource pools in the cluster.
Finally, we create a ForEach loop to parse through the $respool array and then we take each resource pool from the array and pipeline the Get-VM cmdlet. If the Get-VM cmdlets does not return any output or if its value is $null (empty), then we output the name of the empty resource pool.
That’s it! A simple and effective script for your day-to-day VMware administration. Enjoy!