Orchestrator Function: vCenter Virtual Machine Execute a Storage vMotion

by Simon Sparks · 21 February 2026

To use this function add it to the class library file named PluginVCVirtualMachineService.ts

GitHub Repository: https://github.com/SimonSparksUK/Orchestrator

Filename: PluginVCVirtualMachineService.ts

Description: Orchestrator Function to execute a Storage vMotion operation.

Private Function: StorageVMotion

publicStorageVMotion(objVcVirtualMachine:VcVirtualMachine,objVcStoragePod:VcStoragePod):void{

letarrVcVirtualDisk:VcVirtualDisk[]=this.GetAllVcVirtualDisk(objVcVirtualMachine);

letarrVcPodDiskLocator:VcPodDiskLocator[]=arrVcVirtualDisk.map((objVcVirtualDisk:VcVirtualDisk):VcPodDiskLocator=>{

letobjVcVirtualDiskFlatVer2BackingInfo:VcVirtualDiskFlatVer2BackingInfo=objVcVirtualDisk.backingasVcVirtualDiskFlatVer2BackingInfo;

letobjVcPodDiskLocator:VcPodDiskLocator=newVcPodDiskLocator();
objVcPodDiskLocator.diskId=objVcVirtualDisk.key;
//	objVcPodDiskLocator.diskMoveType = "";
//	objVcPodDiskLocator.profile = "";
//	objVcPodDiskLocator.diskBackingInfo.datastore = ;

this.objLogger.info(objVcPodDiskLocator.diskId.toString());

letobjVcDescription:VcDescription=objVcVirtualDisk.deviceInfo;

letobjVcDatastore:VcDatastore=objVcVirtualDiskFlatVer2BackingInfo.datastore;

this.objLogger.info("Virtual Machine '"+objVcVirtualMachine.name+"' has a Hard Disk Attached to"+objVcDescription.label+" which has a key of '"+objVcVirtualDisk.key+"'.");
this.objLogger.info("Virtual Machine '"+objVcVirtualMachine.name+"' has a Hard Disk Attached to"+objVcDescription.label+" which has a Capacity of '"+ (objVcVirtualDisk.capacityInBytes/1024/1024/1024)+"' GB.");
this.objLogger.info("Virtual Machine '"+objVcVirtualMachine.name+"' has a Datastore of '"+objVcDatastore.name+"' which has a Free Space of '"+objVcDatastore.freeSpace+"' GB.");
this.objLogger.info("Virtual Machine '"+objVcVirtualMachine.name+"' has a Hard Disk File Name of '"+objVcVirtualDiskFlatVer2BackingInfo.fileName+"'.");

returnobjVcPodDiskLocator;
});

letobjVcVmPodConfigForPlacement:VcVmPodConfigForPlacement=newVcVmPodConfigForPlacement();
objVcVmPodConfigForPlacement.disk=arrVcPodDiskLocator;
objVcVmPodConfigForPlacement.storagePod=objVcStoragePod;

letarrVcVmPodConfigForPlacement:VcVmPodConfigForPlacement[]= [];
arrVcVmPodConfigForPlacement.push(objVcVmPodConfigForPlacement);

letobjVcStorageDrsPodSelectionSpec:VcStorageDrsPodSelectionSpec=newVcStorageDrsPodSelectionSpec();
objVcStorageDrsPodSelectionSpec.storagePod=objVcStoragePod;
objVcStorageDrsPodSelectionSpec.initialVmConfig=arrVcVmPodConfigForPlacement;

letobjVcVirtualMachineRelocateSpec:VcVirtualMachineRelocateSpec=newVcVirtualMachineRelocateSpec();
//objVcVirtualMachineRelocateSpec.transform = VcVirtualMachineRelocateTransformation["sparse"];
//objVcVirtualMachineRelocateSpec.transform = VcVirtualMachineRelocateTransformation["flat"];

letobjVcStoragePlacementSpec:VcStoragePlacementSpec=newVcStoragePlacementSpec();
objVcStoragePlacementSpec.resourceLeaseDurationSec=180;
objVcStoragePlacementSpec.type="relocate";
objVcStoragePlacementSpec.vm=objVcVirtualMachine;
objVcStoragePlacementSpec.relocateSpec=objVcVirtualMachineRelocateSpec;
objVcStoragePlacementSpec.podSelectionSpec=objVcStorageDrsPodSelectionSpec;
//	objVcStoragePlacementSpec.resourcePool = objVcResourcePool;
//	objVcStoragePlacementSpec.host = objVcHostSystem;
//	objVcStoragePlacementSpec.folder = objVcVmFolderVRM;

letobjVcSdkConnection:VcSdkConnection=objVcVirtualMachine.sdkConnection;

letobjVcStorageResourceManager:VcStorageResourceManager=objVcSdkConnection.storageResourceManager;

letobjVcStoragePlacementResult:VcStoragePlacementResult=objVcStorageResourceManager.recommendDatastores(objVcStoragePlacementSpec);

letarrVcClusterRecommendation:VcClusterRecommendation[]=objVcStoragePlacementResult.recommendations;

letobjVcClusterRecommendation:VcClusterRecommendation=arrVcClusterRecommendation.find((objVcClusterRecommendation:VcClusterRecommendation):boolean=>{

returnobjVcClusterRecommendation.reason==="storagePlacement";

});

arrVcClusterRecommendation.forEach((objVcClusterRecommendation:VcClusterRecommendation):void=>{

this.objLogger.info("vCenter Cluster Recommendations - Key - '"+objVcClusterRecommendation.key+"'.");
this.objLogger.info("vCenter Cluster Recommendations - Rating - '"+objVcClusterRecommendation.rating+"'.");
this.objLogger.info("vCenter Cluster Recommendations - Reason - '"+objVcClusterRecommendation.reason+"'.");
this.objLogger.info("vCenter Cluster Recommendations - Actions - '"+objVcClusterRecommendation.action.length+"'.");

letarrVcClusterAction:VcClusterAction[]=objVcClusterRecommendation.action;

arrVcClusterAction.forEach((objVcClusterAction:VcClusterAction):void=>{

this.objLogger.info("vCenter Cluster Recommendations - Action - Target '"+objVcClusterAction.target.sdkId+"' - Type - '"+objVcClusterAction.type+"'.");

});

if (objVcClusterRecommendation.prerequisiteinstanceofArray){
this.objLogger.info("vCenter Cluster Recommendations - Prerequisite - '"+objVcClusterRecommendation.prerequisite.length+"'.");
}

this.objLogger.info("vCenter Cluster Recommendations - Reason Text - '"+objVcClusterRecommendation.reasonText+"'.");
this.objLogger.info("vCenter Cluster Recommendations - Target - '"+objVcClusterRecommendation.target.sdkId+"'.");
this.objLogger.info("vCenter Cluster Recommendations - Time - '"+objVcClusterRecommendation.time+"'.");
this.objLogger.info("vCenter Cluster Recommendations - Type - '"+objVcClusterRecommendation.type+"'.");

if (objVcClusterRecommendation.warningDetailsinstanceofVcLocalizableMessage){
this.objLogger.info("vCenter Cluster Recommendations - Warning Details - '"+objVcClusterRecommendation.warningDetails.message+"'.");
}

this.objLogger.info("vCenter Cluster Recommendations - Warning Text - '"+objVcClusterRecommendation.warningText+"'.");

});

letarrVcClusterRecommendationKey:string[]= [];
arrVcClusterRecommendationKey.push(objVcClusterRecommendation.key);

letobjVcTask:VcTask=objVcStorageResourceManager.applyStorageDrsRecommendation_Task(arrVcClusterRecommendationKey);

this.WaitForVcTaskEnd(objVcTask,15);
}

Discover more from Cloud Build Tools

Subscribe to get the latest posts sent to your email.

You may also like