How to get all DRS rules associated with each VM in a DRS cluster using API : Interesting API in vSphere 6.0

As we know, one VM can be associated with several DRS affinity rules. If your DRS cluster has several rules configured, it is really cumbersome to check what is the type of the rule ¬†and how many rules are associated with each VM in a DRS cluster. There is a interesting method introduced in vSphere 6.0 which can address this pain point. New method that I am talking about is “findRulesForVm()”. This method is introduced in vSphere 6.0 under “ClusterComputeResource” managed object. It is mean that you need to have this managed object in order to call this useful method. i.e. findRulesForVm().

Note: This method can list all the VM-VM affinity/anti-affinity rules associated with particular VM. This method can not return the VM-Host affinity rules associated with the VM.

Below is the complete code sample which can help to get all the VM VM DRS rules associated with each VM in Cluster. Please do modify this sample as per your need.

//:: # Author: Vikas Shitole
//:: # Website:
//:: # Product/Feature: vCenter Server/DRS
//:: # Description: Script to find all the rules associated with all the VMs in the DRS cluster

package com.vmware.yavijava;

import java.rmi.RemoteException;
import com.vmware.vim25.ArrayUpdateOperation;
import com.vmware.vim25.ClusterAffinityRuleSpec;
import com.vmware.vim25.ClusterAntiAffinityRuleSpec;
import com.vmware.vim25.ClusterConfigSpec;
import com.vmware.vim25.ClusterRuleInfo;
import com.vmware.vim25.ClusterRuleSpec;
import com.vmware.vim25.InvalidProperty;
import com.vmware.vim25.ManagedObjectReference;
import com.vmware.vim25.RuntimeFault;

public class FindRulesForVMsInCluster {

public static void main(String[] args) throws Exception {
System.out.println("Usage: FindRulesForVMInCluster url username password");

URL url = null;
url = new URL(args[0]);
} catch ( MalformedURLException urlE)
System.out.println("The URL provided is NOT valid. Please check it.");
String username = args[1];
String password = args[2];
String ClusterName = "My-Cluster"; //Your DRS cluster Name
ManagedEntity vms[] = null;

// Initialize the system, set up web services
ServiceInstance si = new ServiceInstance(url, username,
password, true);

Folder rootFolder = si.getRootFolder();

ClusterComputeResource cluster = null;
cluster = (ClusterComputeResource) new InventoryNavigator(rootFolder)
.searchManagedEntity("ClusterComputeResource", ClusterName);

System.out.println("Cluster name::"+cluster.getName());

vms = (ManagedEntity[]) new InventoryNavigator(cluster)

for(ManagedEntity vm:vms){
//New vSphere 6.0 method to find the VM affinity rules associated with particular VM
ClusterRuleInfo[] rules=cluster.findRulesForVm((VirtualMachine) vm);
System.out.println("Rules assciated with VM "+vm.getName()+" are::");
for(ClusterRuleInfo rule:rules){
System.out.println(rule.getName()+":" + rule.getEnabled());

– For the sake of simplicity, I have hard-coded DRS cluster name, you can change it based on your environment.
– You can scale the same code to all the clusters in a datacenter or multiple datacenter.

If you have still not setup your YAVI JAVA Eclipse environment:Getting started tutorial

Important tutorials to start with: Part I & Part II

Leave a Reply

Your email address will not be published.