Jul 09 2013

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“

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 ping

    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 […]

    Leave a Reply

    Your email address will not be published. Required fields are marked *

    You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">