VMware Cloud Community
snandy
Contributor
Contributor

Add VM to Affinity rules using powershell

Hi All,

I need to add VMs to affinity rules.

for first I was trying to get the rules for a cluster but not able to fetch all the rules.

 

$vkcpw = Get-Content E:\vkcpw.txt | ConvertTo-SecureString -Key (Get-Content E:\V-center_aes.key)
$vcentercredential   = New-Object System.Management.Automation.PsCredential("USER",$vkcpw)
Connect-VIServer -server 192.1.8.1 -Credential $vcentercredential 
$DRSRules = Get-Cluster | where {$_.Name -eq "Standard"} | Get-DrsRule 

ForEach ($DRSRule in $DRSRules)
    { 
    "" | Select-Object-Property @{N="Name";E={$DRSRule.Name}},
    @{N="Cluster";E={$DRSRule.Cluster.Name}},
    @{N="KeepTogether";E={$DRSRule.KeepTogether}},
    @{N="VMs";E={((Get-View-Id $DRSRule.VMIds).Name)}}
    }

uotdK.png

The above code is fetching only the rules type "Separate Virtual Machines".

Please let me know how to fetch the rules type "Run VMs on Hosts"

Labels (2)
0 Kudos
6 Replies
LucD
Leadership
Leadership

Have a look at the Type parameter of the Get-DrsRule cmdlet.


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

0 Kudos
snandy
Contributor
Contributor

@LucD Checked that. tried printing all types but values are missing

snandy_0-1648043791628.png

In vCenter

Separate Virtual Machines = VMAntiAffinity

Keep Virtual Machines Togather = VMAffinity

Tags (1)
0 Kudos
LucD
Leadership
Leadership

That is not the Type I mean, it is the parameter on the Get-DrsRule cmdlet.
Have a look at the online cmdlet reference I linked to, more specifically Example 4 


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

0 Kudos
snandy
Contributor
Contributor

@LucD Thanks for the response, but the image which I mentioned in my previous reply, it does not shows the VMHostAffinity type. it is showing all rest of the rules.

snandy_0-1648045394829.png

 

0 Kudos
LucD
Leadership
Leadership

Did you actually look at Example 4?


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

0 Kudos
snandy
Contributor
Contributor

@LucD  Yes. you are talking about Get-Cluster'MyCluster1'|Get-DrsRule-TypeVMHostAffinity right?

But I got the solution using below

Get-DrsVMHostRule -Cluster "Standard"

Can you help me with how to add a VM to a specific rule?

 

0 Kudos