VMware Cloud Community
wtarves
Contributor
Contributor
Jump to solution

New-DrsRule - Anti-affinity Rule Max 2 VM's

Hello All,

I Using vSphere and PowerCli 4.1 to import and export vSphere objects and wondering if anyone could give me the low-down on DRS Anti-Affinity rules.

Specificly if the number of VM limit is still capped at two.

The reason I ask is that when I attempt to create a anti-affinity rule for three VM's from the command line with the following command:

New-DrsRule -Cluster (Get-Cluster -name $DRSR.cluster) -Name DRSR.Name -Enabled $True -KeepTogether $False -VM (Get-VM $DRSR.vms)

I get the following error:

New-DrsRule : 7/26/2011 3:30:24 PM    New-DrsRule        You need to specify 2 or more VMs if KeepTogether option is set and only 2 VMs if KeepTogether option is not set.   
At line:1 char:12
+ New-DrsRule <<<<  -Cluster (Get-Cluster -name $DRSR.cluster) -Name DRSR.Name -Enabled $True -KeepTogether $False -VM (Get-VM $DRSR.vms)
    + CategoryInfo          : InvalidArgument: (:) [New-DrsRule], VimException
    + FullyQualifiedErrorId : Client20_ComputeResourceServiceImpl_TryValidateDrsRuleVMCount_InvalidVmCount,VMware.VimAutomation.ViCore.Cmdlets.Commands.NewDrsRule

However I am able to create the rule through vCenter.

What am I missing here? Is the rule I create in vCenter not actually being applied?

Anyone out there able to set me stright?

Thanks!

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Thanks.

You are right, the New-DrsRule cmdlet when used for an anti-affinity rule limits the number of guests to 2.

I suspect that this is due to historical reasons.

In pre-4.1 environments I think that was indeed a limitation, even in the vSphere client.

So yes, I'm afraid you will have to adapt the script to create the anti-affinity rules 2-by-2.

Luckily you can use a simple nested loop to create the rules.

Something like this for example

$vms = "vm1","vm2","vm3","vm4","vm5","vm6"

$j = 1
1..($vms.Count-1) | %{     $i = $_
    ($i - 1)..0 | %{         New-DrsRule -Cluster (Get-Cluster -Name MyCluster) `
        -Name ("Rule" + $j) `
        -KeepTogether $false -VM (Get-VM -Name $vms[$i],$vms[$_])         $j++
    } }


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

View solution in original post

Reply
0 Kudos
7 Replies
LucD
Leadership
Leadership
Jump to solution

I suspect that

Get-VM $DRSR.vms

returns more than 2 guests.


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

Reply
0 Kudos
wtarves
Contributor
Contributor
Jump to solution

Thanks for the response LucD, (and the awesome book!)

Yes, You are correct that Get-VM $DRSR.vms returns more then two guests.

I guess my real issue is that using the GUI, vCenter allows a dis-affinity rule with more then two guests to be created, however the CLI (correctly?) does

not.

So when I export the DRS rules I received data with more then two guests but when I try to import them I get the above error.

Is it safe to assume that the the dis-affinity rule created in the GUI isn't really being properly implemented, that is to say isn't necessarily keeping all three servers separated?

Should I rewrite the import script to create the additional rules that would accomplish the intended separation?

VM1<->VM2

VM1<->VM3

VM2<->VM1

VM2<->VM3

VM3<->VM1

VM3<->VM2

Thanks again!

w

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Thanks.

You are right, the New-DrsRule cmdlet when used for an anti-affinity rule limits the number of guests to 2.

I suspect that this is due to historical reasons.

In pre-4.1 environments I think that was indeed a limitation, even in the vSphere client.

So yes, I'm afraid you will have to adapt the script to create the anti-affinity rules 2-by-2.

Luckily you can use a simple nested loop to create the rules.

Something like this for example

$vms = "vm1","vm2","vm3","vm4","vm5","vm6"

$j = 1
1..($vms.Count-1) | %{     $i = $_
    ($i - 1)..0 | %{         New-DrsRule -Cluster (Get-Cluster -Name MyCluster) `
        -Name ("Rule" + $j) `
        -KeepTogether $false -VM (Get-VM -Name $vms[$i],$vms[$_])         $j++
    } }


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

Reply
0 Kudos
wtarves
Contributor
Contributor
Jump to solution

That explains it, Thanks a lot!

Awesome work-around too that'll work great!

I'm new to powershell and scripting in general, any recommendations for books that would help me get a deeper understanding of concepts like the one you just demonstrated? Like do you have a list of 'must-reads' or anything?

I've read a lot of powershell stuff on the Internet and I'm making progress just hacking away but some of the concepts I find in your book and on the net are still a bit too complex for me. I'm just looking for a good next step.

Thanks again for taking the time to help!

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

I created a post, called My PS library, some time ago, that is still valid. I'll have to do an update soon :smileygrin:

As a general PowerShell intro I would advise Tobias's free ebook.

For a PowerCLI intro, you can use Hal's book.


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

Reply
0 Kudos
donfede
Contributor
Contributor
Jump to solution

This limitation, of only being able to separate 2x VMs via PowerCLI New-DrsRule, is corrected with vSphere 5 (and PowerCLI 5).

Using: VMware-PowerCLI-5.1.0-793510.exe, and I am now able to successfully create DRS Rules to separate more than 2 VMs.

donfede

Reply
0 Kudos
raffic_ncc
Enthusiast
Enthusiast
Jump to solution

Is there any  limit to the number of affinity rules that can be set in a cluster ??

Mohammed Raffic VCP4,VCP5,VCAP4-DCA,VCAP5-DCA,VCP-Cloud, MCSA.MCTS,CCA http://www.vmwarearena.com
Reply
0 Kudos