Managing vSphere DRS Groups and Rules with PowerShell

Manually maintaining vSphere DRS Groups can be an onerous task. VCF Automation is a great tool to help with this but a simple PowerShell script can assist just as easy.

Is this example, I needed all Windows VMs to exist on a DRS VM group. The first script provided, checks each cluster for Windows VMs and if any exist it will create a DRS VM Group, VMHost Group and VM/Host Rule. Only the Windows VMs are added to the VM Group and all hosts in the cluster to the VMHost group. The idea that the VMHost group quantity can be reduced manually depending on capacity requirements. If no Windows VMs exist in the cluster it will skip that cluster and not apply any DRS groups or rules.

  1. Copy the code and save it as a .ps1 file.
  2. Open a PowerShell window.
  3. Execute the Script: 
    .\filename.ps1 vCenter_FQDN
param ($vc)
Connect-VIServer -Server $vc
foreach ($Cluster in (Get-Cluster)) {
$vms = Get-VM -Location $cluster | where{$_.ExtensionData.Summary.Config.GuestFullName -like "*Windows*"}
if ($vms -eq $NULL){
Write-Output "`nNo Windows VMs in $cluster ---- Skipping Cluster`n"
Continue
}
$hosts = Get-VMHost -Location $cluster
$drsHostGroup = New-DrsClusterGroup -Name Licensed-Windows-Hosts -Cluster $cluster -VMHost $hosts
$drsVMGroup = New-DrsClusterGroup -Name Licensed-Windows-VMs -Cluster $cluster -VM $vms
New-DrsVMHostRule -Name Windows-VM-Placement-Rule -Cluster $cluster -VMHostGroup $drsHostGroup -VMGroup $drsVMGroup -Type MustRunOn
Write-Output "`nSettings successfully applied to $cluster`n"
}
Disconnect-VIServer -Server $vc -Confirm:$false

The second script can be ran to check if any new Windows VMs need to be added to the previously created Windows DRS VM Group. By appending
–apply the new Windows VMs can be added to the DRS VM Group.

  1. Copy the code and save it as a .ps1 file.
  2. Open a PowerShell window.
  3. Execute the Script to check:
    .\filename.ps1 vCenter_FQDN
param ($vc, $apply)
Connect-VIServer -Server $vc
foreach ($Cluster in (Get-Cluster)) {
ForEach-Object -Process {
$hashTable = @{}
$group = Get-DrsClusterGroup -Name Licensed-Windows-VMs -Cluster $cluster -ErrorAction Ignore
if ($group) {
$vms = Get-VM -Location $cluster | where{$_.ExtensionData.Summary.Config.GuestFullName -like "*Windows*"} |
ForEach-Object -Process {
$hashTable.Add($_.Name,'')
}
ForEach-Object -Process {
($group).Member |
ForEach-Object -Process {
$hashTable.Remove($_.Name)
}
}
if ($hashTable.GetEnumerator().Name -eq $NULL){
Write-Output "`nNo Windows VMs missing from VM group $group in cluster $cluster`n"
Continue
}
Write-Host "`nFollowing Windows VM(s) are not in the VM group $group in cluster $($cluster.Name)"
$hashTable.GetEnumerator().Name
if ($apply -eq "--apply"){
$group | Set-DrsClusterGroup -Add -VM $hashTable.GetEnumerator().Name
Write-Output "`nVM(s) sucessfully added to the VM group $group on cluster $($cluster.Name)`n"
} else {
Write-Output "`nNo Changes were made, append --apply to add the VM(s) to the VM group $group in cluster $($cluster.Name)`n"
}
}
if (!$group) {
Write-Host "`nVM group Licensed-Windows-VMs does not exist in cluster $cluster"
}
}
}
Disconnect-VIServer -Server $vc -Confirm:$false

Leave a comment