VMware Cloud Community
steveschofield
Enthusiast
Enthusiast

Add Machines to DRS group

I've hacked together a script based on other examples, this works with no problem.  The links and script are included below, hope it helps someone. 


What I'm looking to answer the "add" operation seems to add to an in-memory Array (Powershell based), then that is what gets committed to vCenter.  The add / edit / remove operations don't call into vCenter directly.   If I wanted to use the add operation, I'd need another function to first build the array then use the add operation, then commit the updated array using ReconfigureComputeResource 

#https://communities.vmware.com/message/1666605#1666605

#http://pubs.vmware.com/vsphere-50/index.jsp?topic=%2Fcom.vmware.wssdk.apiref.doc_50%2Fvim.cluster.Vm...

#https://www.vmware.com/support/developer/vc-sdk/visdk41pubs/ApiReference/vim.cluster.GroupSpec.html

# Loading PowerCLI Snapins

function LoadSnapin{

  param($PSSnapinName)

  if (!(Get-PSSnapin | where {$_.Name   -eq $PSSnapinName})){

    Add-pssnapin -name $PSSnapinName

  }

}

LoadSnapin -PSSnapinName   "VMware.VimAutomation.Core"

#Function for updating the Resource VM Groups

function updateDrsVmGroup ($clusterName,$VMFilter,$groupVMName)

{

#Get Cluster Information

  $cluster = Get-Cluster -Name $clusterName

  $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

#Perform your VM selection here.

  Get-VM -Name $VMFilter | % {$groupVM.Info.VM += $_.Extensiondata.MoRef}

  $spec.GroupSpec += $groupVM

    #Apply the settings to the cluster

    $cluster.ExtensionData.ReconfigureComputeResource($spec,$true)

}

$CredentialID="domain\user"

$vCenterServer="vCenter1"

$cred = Get-Credential $CredentialID

Connect-VIServer -server $vCenterServer -credential $cred

# Calling the function. I've found the group names to be case sensitive, so watch for that.

updateDrsVmGroup -clusterName "ClusterLab1" -VMFilter "ABC*" -groupVMName "MachineGroup"

Disconnect-VIServer -Confirm:$False

0 Kudos
2 Replies
LucD
Leadership
Leadership

Not sure I get the question :smileygrin:

Afaik, the add and edit operation use the same object to specify the input.

The way vSphere implements the specified rules is different.

So what exactly are you trying to do ?

Change a number of existing rules or add a number of new rules ?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
steveschofield
Enthusiast
Enthusiast

Smiley Happy  probably gave too much detail, seems to happen.  My script works based on prior examples you and others posted. Thanks for that.

All I'm doing is adjusting an existing DRS VM group adding machines to it.  No new rules or no new groups.

The add and edit operations deal with in-memory powershell arrays from what I can tell.  I thought I could use just the add operation to add a new machine to an existing DRS VM group.  The add operation property doesn't apply and gets an error.  When I tried to do a single machine, the prior machines added were removed.  I thought add means to DRS group, it means to a powershell array that gets submitted and anything prior is overwritten although I needed to use the edit operation.. Hope that makes more sense

0 Kudos