PowerCLI - Analyze a Cluster to Move VMs to a Resource Pool
After someone deployed a bunch of VMs, we let them know about the Resource Pool they were supposed to be deployed to. Oops. To correct this, and to avoid a couple hours of dragging and dropping VMs into a resource pool, I was able to create a script that detects if a VM is outside of a Resource Pool and then move it to the specified Resource Pool.
When you run the script, it will ask for the Cluster name and the Resource Pool name so you don’t have to modify the code.
#Analyze the Clusters and check for systems outside of Resource Pools
$InputCluster = Read-Host "Cluster Name"
$InputRP = Read-Host "Resource Pool Name"
#Cluster which will be analyzed
$cluster = Get-Cluster $InputCluster
#Resource Pool where VMs should be moved
$rp = Get-ResourcePool $InputRP
#Detection of VMs not in Resource Pools
$norp = Get-Cluster $cluster | Get-VM | where {$_.ResourcePool.Name -eq "Resources"}
foreach ($i in $norp) {
Write-Host "Moving $i to Resource Pool $rp"
Get-VM $i -Location $cluster | Move-VM -Destination $rp | Out-Null
Write-Host "Move complete" Write-Host ""
}
Note: this was a script that worked in my environment. There is no warranty or support with this script, please use at your own risk.