VMware Cloud Community
Ambarish08
Contributor
Contributor

Need help to get the List of VM – Hosts which are part of affinity rules using Power cli ?

Need help to get the List of VM – Hosts which are part of affinity rules  using Power cli  ?

Tags (1)
0 Kudos
3 Replies
LucD
Leadership
Leadership

Which type of rules do you want, inter-VM affinity or VM-VMHost affinity?


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

0 Kudos
Ambarish08
Contributor
Contributor

We  have vSphere Cluster configured in  stretch so DR side & Prod side ESXi host configured in Single vSphere Cluster.  However, to avoid VM failover / Power On from PROD host to DR host  or vice versa we have defined Affinity Rule in Single Cluster. 

So here I am looking three column output

  1. First Column gives the list of VM which are part of ‘X’ Affinity rule
  2. Second Column gives the list of VM which are part of ‘Y’ Affinity rule
  3. Third Column  - List of the VM which are not part of ‘X’ and ‘Y’ affinity rule
0 Kudos
LucD
Leadership
Leadership

Are there then only 2 such rules on the cluster?

If yes, then the script could be quite simple.

Something like this for example

$clusterName = 'MyCluster'

$cluster = Get-Cluster -Name $clusterName


$vmInRule = @()


Get-DrsVMToVMHostRule -Cluster $cluster |

   ForEach-Object -Process {

   $vm = (Get-DrsClusterGroup -Cluster $cluster -Name $_.VmGroupName).Member.Name

   $vmInRule += $vm

   $obj = [ordered]@{

   RuleName = $_.Name

   VM = $vm -join '|'

   }

   New-Object psobject -Property $obj

}


$obj = [ordered]@{

   RuleName = 'Not in a rule'

   VM = (Get-VM -Location $cluster | where {$vmInRule -notcontains $_.Name}).Name -join '|'

}

New-Object psobject -Property $obj


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

0 Kudos