User Tools

Site Tools


vrealize:code:turnsdrsonoff

How to turn SDRS on/off for a VM

StorageDRS can be turned on/off.

function CheckSDRSEnabled(POD)
{
	if (POD == null)
	{
		return false;
	}	
	if (POD.podStorageDrsEntry == null)
	{
		return false;
	}	
	if (POD.podStorageDrsEntry.storageDrsConfig == null)
	{
		return false;
	}	
	if (POD.podStorageDrsEntry.storageDrsConfig.podConfig == null)
	{
		return false;
	}	
	var Enabled =POD.podStorageDrsEntry.storageDrsConfig.podConfig.enabled;
	System.log("Storage DRS enabled: "+Enabled);
	return Enabled;
}
 
function GetStoragePod(VM)
{
	// Get the DataStore of the VM
	DS = VM.datastore;
	if (DS[0].type != "Datastore")
	{
		ResultOut = "This VM is not connected to a recognized datastore: "+DS[0].type;
		StoragePod = null;
	}	
	else
	{
		// Check if the Datastore is connected to a Storage Pod (DataStore Cluster)
		if (DS[0].parent.type != "StoragePod")
		{
			System.log("This VM is not connected to a StoragePod, no need to check for SDRS!");
			StoragePod = null;
		}	
		else
		{			
			if (CheckSDRSEnabled(DS[0].parent) == true)
			{
				StoragePod =DS[0].parent;
			}
			else
			{
				StoragePod = null;
			}
		}
	}
        return StoragePod;
}
 
 
StoragePod =GetStoragePod(VM)
 
// Get Resource Manager Object from sdkConnection of Storage Pod:
var SRM = StoragePod.sdkConnection.storageResourceManager;
 
var myVcStorageDrsVmConfigSpec = new VcStorageDrsVmConfigSpec();
myVcStorageDrsVmConfigSpec.operation = VcArrayUpdateOperation.add;
myVcStorageDrsVmConfigSpec.info = new VcStorageDrsVmConfigInfo();
if (State == true)
{
	System.log("Setting Storage DRS to disabled for this vm.");
	myVcStorageDrsVmConfigSpec.info.enabled = false;
}
else
{
	System.log("Setting Storage DRS to default setting for this vm.");
	myVcStorageDrsVmConfigSpec.info.enabled = null;
}
	myVcStorageDrsVmConfigSpec.info.vm = VM;
 
	var Spec = new VcStorageDrsConfigSpec();
	var Arr = new Array();
	Arr.push (myVcStorageDrsVmConfigSpec);
	Spec.vmConfigSpec = Arr;
	Task = SRM.configureStorageDrsForPod_Task(StoragePod,Spec,true);
vrealize/code/turnsdrsonoff.txt ยท Last modified: 2018/11/29 01:26 by admin