A Tale of Licensing – Part 3: A Script to automatically update DRS Groups

See Part 1 Here

See Part 2 Here

 

What a pain this was. As of September 2014, there are no good cmdlets in PowerShell or PowerCLI to manage DRS groups and rules. I wanted a script that I could schedule to run once a day to update our DRS groups with any Windows Servers so we would not have to manage this manually.

I found this blog post which references this script created by Niklas Akerlund and tweaked it to my liking.

Here are the results:

*You will have to enter your information anywhere you see a:  <enter name>

*You will also have to run this as a user that has appropriate permissions in vCenter

_____________________________________________________________________

 
#

# Script to automatically update DRS Groups
#

# vCenter Server specification
$vCenter = "<enter name>"

# Cluster specification
$devcluster = "<enter name>"
$prodcluster = "<enter name>"

# DRS Group specification
$devClusterGroup = "Windows Servers"
$prodClusterGroup = "Windows Servers"

add-pssnapin VMware.VimAutomation.Core

Write-Output "Stand by, connecting to $vcenter…."
Connect-VIServer -Server $vCenter

# 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"
#Operation edit will replace the contents of the GroupVMName with the new contents selected below.
    $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)
    }
#end of function

#——————————————–
# Specify VMs here
#——————————————–

$VMcluster1 = Get-Cluster $devcluster | Get-vm | where {($_.extensiondata.config.Guestfullname -like “*Microsoft Windows Server*”)}
# Add non-windows exceptions
$VMcluster1 += Get-VM <enter name>

$VMcluster2 = Get-Cluster $prodcluster | Get-vm | where {($_.extensiondata.config.Guestfullname -like “*Microsoft Windows Server*”)}
# Add non-windows exceptions
$VMcluster2 += Get-VM <enter name>

#——————————————
# VM selection ends here
#——————————————

# Update the DRS rules – add one line per vSphere cluster where you want to run the script.
DRSrule -cluster $devcluster -VMs $VMcluster1 -groupVMName $devClusterGroup
DRSrule -cluster $prodcluster -VMs $VMcluster2 -groupVMName $prodClusterGroup

# Disconnect from vCenter Server

Write-Output "Stand by, finishing the DRS group update and disconnection from $vCenter"
Disconnect-VIServer $vCenter -Confirm:$False

 

____________________________________________________________________

 

Change Log (to the best of my memory):

-Removed email portion

-removed hard coded credentials for connecting to vcenter server (will passthrough credentials of user running PowerShell)

-changed variable names to make more sense

-added lines to add exceptions to the VM list being queried (see # Add non-windows exceptions)

-changed query to look for OS -like "*Microsoft Windows Server*"

-cleaned up excess comments

 

akers8806

2 Comments

  1. Is there a way to make this script update the DRS Group instead of having it re-create the group each time? Any help is appreciated. Thanks!

    • Not that I know of… there are not a lot of resources out there (or cmdlets) for modifying DRS groups with Powershell/CLI. I was lucky to come across this script. If you find a way, please let me know!

Leave a Reply

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