Stretched cluster VM localisation

November 29, 2016 – 8:28 pm

A colleague asked me to help him out with an issue he had. On a stretched cluster, some VMs had storage on Site A but were powered-on on site B and that’s a no-no in a normal situation. There’s no built-in mechanism other than using DRS groups to do so. So I assembled bits and pieces I found online to create a script that would serve this purpose by creating 2 DRS groups and populating them based on which datastores the VMs were on.

Here’s the script:

# These groups must already exist
# VMs-On-SiteA
# VMs-On-SiteB
# Hosts-SiteA
# Hosts-SiteB
# And the following rules
# Bind-To-SiteA (should run)
# Bind-To-SiteB (should run)

#===================================================================
# Initialize variables
#===================================================================

$vCenter = “vCenterName.subnet192.lab”
$vCenterUser = “ServiceAccount@vsphere.local”
$vCenterPass = “MyPassword”
$ClusterName = “ClusterName”
$SiteA_Datastores = “SiteA*”
$SiteB_Datastores = “SiteB*”

$SiteA_DRSGroup = “VMs-On-SiteA”
$SiteB_DRSGroup = “VMs-On-SiteB”

#===================================================================
# Update-DrsVMGroup
# http://vniklas.djungeln.se/2012/06/28/vsphere-cluster-host-vm-rule-affinity-with-powercli/
#===================================================================

function Update-DrsVMGroup {
param (
$VMs,
$groupVMName)

$spec = New-Object VMware.Vim.ClusterConfigSpecEx
$groupVM = New-Object VMware.Vim.ClusterGroupSpec
#Operation edit will replace the contents of the GroupVMName with the new contents seleced below.
$groupVM.operation = “edit”

$groupVM.Info = New-Object VMware.Vim.ClusterVmGroup
$groupVM.Info.Name = $groupVMName

Get-VM $VMs | %{
$groupVM.Info.VM += $_.Extensiondata.MoRef
}
$spec.GroupSpec += $groupVM

#Apply the settings to the cluster
$cluster.ExtensionData.ReconfigureComputeResource($spec,$true)
}

#===================================================================
# vCenter Connection
#===================================================================

Connect-VIServer $vCenter -User $vCenterUser -password $vCenterPass -WarningAction SilentlyContinue

#===================================================================
# Housekeeping
#===================================================================

Clear

#===================================================================
# Populate the groups
#===================================================================

$Cluster = Get-Cluster $ClusterName
$AllVMs = $Cluster| Get-VM

$SiteA_VMs = Get-Datastore $SiteA_Datastores | Get-VM
$SiteB_VMs = Get-Datastore $SiteB_Datastores | Get-VM

Update-DrsVMGroup -VMs $SiteA_VMs -groupVMName $SiteA_DRSGroup
Update-DrsVMGroup -VMs $SiteB_VMs -groupVMName $SiteB_DRSGroup

#===================================================================
# Run DRS to move VMS to the proper places
#===================================================================

Get-DrsRecommendation -Cluster $Cluster -Refresh
Apply-DrsRecommendation -DrsRecommendation -RunAsync

Post a Comment