«

»

Update vSphere DRS rules using PowerCLI

I was asked by one customer last week to create a script that adds virtual machines to a vSphere DRS group, “VM DRS Group”, based on virtual machine guest operating. My customer runs vSphere 5.1 and a licensing issue was the underlying cause of the request. My customer have two vSphere clusters running 16 ESXi hosts each but only 4 ESXi hosts per vSphere cluster are licensed for a particular application.

The only virtual machines in the customer environment running Linux as their guest operating system are the once i need to manage in the script. This means i can easily determine what virtual machine to add to the vSphere cluster “VM DRS Group” rules by selecting the virtual machines based on their guest operating system.

There are three different DRS rule types:

  • Keep Virtual Machines Together
  • Separate Virtual Machines
  • Virtual Machines to Hosts – This DRS rule type got one type definition when adding or editing the rule and one rule type definition when you display the DRS Rules.Screen Shot 2013-07-07 at 20.21.43

The customer requires the script to:

I started by creating the required vSphere cluster:

  • VM DRS Group (one per vSphere cluster).
    Screen Shot 2013-07-08 at 09.56.14
  • Host DRS Group (one per vSphere cluster)
    Screen Shot 2013-07-08 at 09.59.37
  • DRS rule (one per vSphere cluster)
    Screen Shot 2013-07-08 at 10.05.10

My first thought was to use the PowerCLI cmdlet Set-DrsRule but i couldn’t make it work when using the “Virtual Machine to Hosts” DRS rule type. When running the Get-DrsRule PowerCLI command the “Virtual Machine to Hosts” based vSphere DRS rule/rules does not count.
Screen Shot 2013-07-08 at 13.16.15
When adding a vSphere DRS rule using the type “Keep Virtual Machines Together” it is listed using the Get-DrsRule command:
Screen Shot 2013-07-08 at 13.15.16

My former colleague, Niklas Åkerlund @vNiklas, wrote a function to manage DRS rules back in 2012, blog post available here, so i decided to use his function for my purpose.

Use the below script to update your Virtual Machine DRS Group rules as required.

Change the following parameters in the below script to match your environment:

  • $vCenter = “vc-demo01.home.local”
  • $vCenteruser =”home\magnus”
  • $vCenterUserPasswd=”not secret”
  • $cluster1=”SiteA”
  • $cluster2=”SiteB”
  • $Cluster1Rule=”VM-License-Linux”
  • $Cluster2Rule=”VM-License-Linux”
  • $sendFrom = “vc-demo01@home.local”
  • $sendTo = “magnus@home.local”
  • $smtp = “smtp.honme.local“
# Script to automatically update DRS Rules
# Magnus Andersson, RTS 2013-07-03
# DRS update function developed by Niklas Åkerlund, http://vniklas.djungeln.se/2012/06/28/vsphere-cluster-host-vm-rule-affinity-with-powercli
#
#--------------------------------------------------------------------------
# Customer specific script parameter section starts here
#--------------------------------------------------------------------------
#
# vCenter Server specification
$vCenter = "vc-demo01.home.local"
$vCenteruser ="home\magnus"
$vCenterUserPasswd="not secret"
#
# Cluster specification
$cluster1="SiteA"
$cluster2="SiteB"
#
# DRS rule specification
$Cluster1Rule="VM-License-Linux"
$Cluster2Rule="VM-License-Linux"
#
# SMTP information
$sendFrom = "vc-demo01@home.local"
$sendTo = "magnus@home.local"
$smtp = “smtp.honme.local“
#--------------------------------------------------------------------------
# Customer specific script parameter section ends here
#--------------------------------------------------------------------------
#
"add-pssnapin VMware.VimAutomation.Core"
#
Write-Output "Stand by, connecting to $vcenter…."
#
Connect-VIServer -Server $vCenter -user $vCenterUser -password $vCenterUserPasswd
#
# Define the Update DRS rule function
#
function DRSrule {
param (
    $cluster,
    $VMs,
    $groupVMName)
    $cluster = Get-Cluster $cluster
    $spec = New-Object VMware.Vim.ClusterConfigSpecEx
    $groupVM = New-Object VMware.Vim.ClusterGroupSpec
    $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)
                             }
#--------------------------------------------
# Customer specific VM selection starts here
#--------------------------------------------
$VMcluster1=Get-Cluster $cluster1 |Get-vm | where {($_.extensiondata.config.Guestfullname  -like "*Linux*")}
$VMcluster2=Get-Cluster $cluster2 |Get-vm | where {($_.extensiondata.config.Guestfullname  -like "*Linux*")}
#
#------------------------------------------
# Customer specific VM selection ends here
#------------------------------------------
#
# Update the DRS rules - add one line per vSphere cluster where you want to run the script.
DRSrule -cluster $cluster1 -VMs $VMcluster1 -groupVMName $Cluster1Rule
DRSrule -cluster $cluster2 -VMs $VMcluster2 -groupVMName $Cluster2Rule
#
#
# Create e-mail body and send e-mail information about what VMs are included in what DRS rule.
$VMbody1=$VMcluster1 | select -ExpandProperty name | out-string
$VMbody2=$VMcluster2 | select -ExpandProperty name | out-string
$s1 = "The following Virtual Machine DRS Group:`n$Cluster1Rule`nRunning in the vSphere Cluster:`n$Cluster1`nContains the following virtual machines:`n"
$s1 += "$VMbody1`n`n"
$s1 += "The following DRS Rule: $Cluster2Rule`nRunning in the vSphere Cluster: $Cluster2`nContains the following virtual machines:`n"
$s1 += "$VMbody2"
#
#
send-mailmessage -to $sendTo -from $sendFrom -Subject “$vCenter DRS rule report” -smtpserver $smtp -body $s1
#
# Disconnect from vCenter Server
Write-Output "Stand by, finishing the DRS group update and disconnection from $vCenter"
Disconnect-VIServer $vCenter -Confirm:$False

When running the script you will get the following email when the script is finished:

Screen Shot 2013-07-08 at 11.26.39

1 pings

  1. A Tale of Licensing - Part 3: A Script to automatically update DRS Groups | Adam Akers Blog

    […] found this blog post which references this script created by Niklas Akerlund and tweaked it to my […]

Comments have been disabled.