Automation

 View Only
Expand all | Collapse all

How to modify an existing Virtual Machine DRS -Group?

  • 1.  How to modify an existing Virtual Machine DRS -Group?

    Posted Feb 15, 2011 12:59 PM

    Hi,

    I use DRS-Rules to place VMs to a Group of selected Hosts within a Cluster.

    The poblem is, that I didn't find any methods to modify (add new VMs) to an existing "Virtual Machine DRS Group".

    AFAIK, the configuration is stored under  $ClusterView.ConfigurationEx.Group

    Any ideas?

    Thanks!



  • 2.  RE: How to modify an existing Virtual Machine DRS -Group?

    Posted Feb 15, 2011 02:47 PM

    You can add virtual machines to a DRS affinity rule with the Set-DrsRule cmdlet. Take a look at example 1 of the Set-DrsRule help:

    C:\PS>$vm = Get-VM DrsRuleVM1*

    Set-DrsRule -Rule $affinityRule -VM $vm -Enabled $true;


    Updates the list of virtual machines that might be referenced by the DRS rule stored in the $affinityRule  variable and enables the rule.



    Regards, Robert



  • 3.  RE: How to modify an existing Virtual Machine DRS -Group?

    Posted Feb 15, 2011 03:08 PM

    Hi Robert,

    thanks for the reply but it doesn't solve my problem.

    I use the "Virtual Machnes to Hosts" rule-type (not the affinity rule/anti-affinity rule).

    Regards,

    Willibald



  • 4.  RE: How to modify an existing Virtual Machine DRS -Group?

    Posted Feb 15, 2011 05:49 PM

    Hi Willibald,

    take a look at http://communities.vmware.com/message/1666605#1666605. The script presented there by Luc creates a "Run VMs on Hosts" type DRS Rule.

    Regards, Robert



  • 5.  RE: How to modify an existing Virtual Machine DRS -Group?

    Posted Feb 15, 2011 07:40 PM

    Hi again,

    I check that already but the script creates a new DRS-Rule (it's the same rule-type which I use).

    What i like to do is to add additional VMs to that rule.

    Regards,

    Willibald



  • 6.  RE: How to modify an existing Virtual Machine DRS -Group?
    Best Answer

    Posted Feb 16, 2011 09:04 AM

    Hi Willibald,

    I did some investigation in the VMware vSphere SDK and made two functions Get-DrsGroup and Add-VmToDrsGroup to add one or more virtual machines to a ClusterVmGroup. Some examples of how to use these function are in the code. If you have added these functions to your PowerCLI session, you can use Get-Help to get information about these functions. And also to get the examples. E.g. Get-Help Add-VmToDrsGroup -Full.

    Function Get-DrsGroup {
    <#
    .SYNOPSIS
    Retrieves DRS groups from a cluster.
    
    .DESCRIPTION
    Retrieves DRS groups from a cluster.
    
    .PARAMETER Cluster
    Specify the cluster for which you want to retrieve the DRS groups
    
    .PARAMETER Name
    Specify the name of the DRS group you want to retrieve.
    
    .EXAMPLE
    Get-DrsGroup -Cluster $Cluster -Name "VMs DRS Group"
    Retrieves the DRS group "Vms DRS Group" from cluster $Cluster.
    
    .EXAMPLE
    Get-Cluster | Get-DrsGroup
    Retrieves all the DRS groups for all clusters.
    
    .INPUTS
    ClusterImpl
    
    .OUTPUTS
    ClusterVmGroup
    ClusterHostGroup
    
    .COMPONENT
    VMware vSphere PowerCLI
    #>
    
      param([parameter(Mandatory=$true, ValueFromPipeline=$true)]$Cluster,
            [string] $Name="*")
    
      process {
        $Cluster = Get-Cluster -Name $Cluster
        if($Cluster) {
          $Cluster.ExtensionData.ConfigurationEx.Group | `
          Where-Object {$_.Name -like $Name}
        }
      }
    }
      
    Function Add-VMToDrsGroup {
    <#
    .SYNOPSIS
    Adds a virtual machine to a cluster VM DRS group.
    
    .DESCRIPTION
    Adds a virtual machine to a cluster VM DRS group.
    
    .PARAMETER Cluster
    Specify the cluster for which you want to retrieve the DRS groups
    
    .PARAMETER DrsGroup
    Specify the DRS group you want to retrieve.
    
    .PARAMETER VM
    Specify the virtual machine you want to add to the DRS Group.
    
    .EXAMPLE
    Add-VMToDrsGroup -Cluster $Cluster -DrsGroup "VM DRS Group" -VM $VM
    Adds virtual machine $VM to the DRS group "VM DRS Group" of cluster $Cluster.
    
    .EXAMPLE
    Get-Cluster MyCluster | Get-VM "A*" | Add-VMToDrsGroup -Cluster MyCluster -DrsGroup $DrsGroup
    Adds all virtual machines with a name starting with "A" in cluster MyCluster to the DRS group $DrsGroup of cluster MyCluster.
    
    .INPUTS
    VirtualMachineImpl
    
    .OUTPUTS
    Task
    
    .COMPONENT
    VMware vSphere PowerCLI
    #>
    
      param([parameter(Mandatory=$true)] $Cluster,
            [parameter(Mandatory=$true)] $DrsGroup,
            [parameter(Mandatory=$true, ValueFromPipeline=$true)] $VM)
            
      begin {
        $Cluster = Get-Cluster -Name $Cluster
      }
      
      process {
        if ($Cluster) {
          if ($DrsGroup.GetType().Name -eq "string") {
            $DrsGroupName = $DrsGroup
            $DrsGroup = Get-DrsGroup -Cluster $Cluster -Name $DrsGroup
          }
          if (-not $DrsGroup) {
            Write-Error "The DrsGroup $DrsGroupName was not found on cluster $($Cluster.name)."
          }
          else { 
            if ($DrsGroup.GetType().Name -ne "ClusterVmGroup") {
              Write-Error "The DrsGroup $DrsGroupName on cluster $($Cluster.Name) doesn't have the required type ClusterVmGroup."
            }
            else {
              $VM = $Cluster | Get-VM -Name $VM
              If ($VM) {
                $spec = New-Object VMware.Vim.ClusterConfigSpecEx
                $spec.groupSpec = New-Object VMware.Vim.ClusterGroupSpec[] (1)
                $spec.groupSpec[0] = New-Object VMware.Vim.ClusterGroupSpec
                $spec.groupSpec[0].operation = "edit"
                $spec.groupSpec[0].info = $DrsGroup
                $spec.groupSpec[0].info.vm += $VM.ExtensionData.MoRef
    
                $Cluster.ExtensionData.ReconfigureComputeResource_Task($spec, $true)
              }
            }
          }
        }
      }
    }
    
    

    Regards, Robert

    I changed the logic of the error handling in the Add-VMToDrsGroup function.

    Message was edited by: RvdNieuwendijk



  • 7.  RE: How to modify an existing Virtual Machine DRS -Group?

    Posted Feb 16, 2011 09:23 AM

    Hi Robert,

    thanks a lot. Thats exactly what I looked for :-)

    Best regards,

    Willibald



  • 8.  RE: How to modify an existing Virtual Machine DRS -Group?

    Posted May 03, 2011 07:17 PM

    Nice functions Robert.  These or some variant of these should be added as cmdlets to the next version of PowerCLI.

    I would think Get-DRSGroup is good as is, but would like to see something more along the lines of Set-DRSGroup, Add-DRSGroup, Remove-DRSGroup as more generalized and flexible cmdlets than Add-VMToDRSGroup.



  • 9.  RE: How to modify an existing Virtual Machine DRS -Group?

    Posted Feb 08, 2013 12:11 PM

    I had to dig out this older thread =)

    I'm testing the posted script to automatically place certain VMs in specific DRS-Groups. This qorks quite well so far, but one thing bothers me. A task is created for EVERY VM to add it to the desired DRS-Group even if it's already in there. How can the function be modified so that tasks will only be created for VMs which are not yet in DRS-Group.



  • 10.  RE: How to modify an existing Virtual Machine DRS -Group?

    Posted Feb 14, 2013 03:47 PM

    Anyone having any idea?



  • 11.  RE: How to modify an existing Virtual Machine DRS -Group?

    Posted Jul 14, 2015 01:46 PM

    I'm amazed that it's 2015 and this is still not possible within the latest version of PowerCLI!



  • 12.  RE: How to modify an existing Virtual Machine DRS -Group?

    Posted Jul 14, 2015 05:56 PM

    The DRSRule module does that for you, see DRSRule – a DRS rules and groups module



  • 13.  RE: How to modify an existing Virtual Machine DRS -Group?

    Posted May 20, 2014 06:46 AM

    hi,

    excellent! thank a lot!



  • 14.  RE: How to modify an existing Virtual Machine DRS -Group?

    Posted Nov 06, 2013 07:11 PM

    Greetings.  I know this is a bit old so hopefully someone can help.

    I've been asked to generate a regular report that contains the ESXi Hosts's name of each host that is a member of a particular DRS group.

    I believe the function above 'Get-DRSGroup' just about get's me there; however, it doesn't spit the information out in a friendly way, nor with the Host's actual name.

    Any help?