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

public StorageVMotion(objVcVirtualMachine: VcVirtualMachine, objVcStoragePod: VcStoragePod): void {

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

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

        let objVcVirtualDiskFlatVer2BackingInfo: VcVirtualDiskFlatVer2BackingInfo = objVcVirtualDisk.backing as VcVirtualDiskFlatVer2BackingInfo;

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

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

        let objVcDescription: VcDescription = objVcVirtualDisk.deviceInfo;

        let objVcDatastore: 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 + "'.");

        return objVcPodDiskLocator;
    });

    let objVcVmPodConfigForPlacement: VcVmPodConfigForPlacement = new VcVmPodConfigForPlacement();
    objVcVmPodConfigForPlacement.disk = arrVcPodDiskLocator;
    objVcVmPodConfigForPlacement.storagePod = objVcStoragePod;

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

    let objVcStorageDrsPodSelectionSpec: VcStorageDrsPodSelectionSpec = new VcStorageDrsPodSelectionSpec();
    objVcStorageDrsPodSelectionSpec.storagePod = objVcStoragePod;
    objVcStorageDrsPodSelectionSpec.initialVmConfig = arrVcVmPodConfigForPlacement;

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

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

    let objVcSdkConnection: VcSdkConnection = objVcVirtualMachine.sdkConnection;

    let objVcStorageResourceManager: VcStorageResourceManager = objVcSdkConnection.storageResourceManager;

    let objVcStoragePlacementResult: VcStoragePlacementResult = objVcStorageResourceManager.recommendDatastores(objVcStoragePlacementSpec);

    let arrVcClusterRecommendation: VcClusterRecommendation[] = objVcStoragePlacementResult.recommendations;

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

        return objVcClusterRecommendation.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 + "'.");

        let arrVcClusterAction: VcClusterAction[] = objVcClusterRecommendation.action;

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

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

        });

        if (objVcClusterRecommendation.prerequisite instanceof Array) {
            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.warningDetails instanceof VcLocalizableMessage) {
            this.objLogger.info("vCenter Cluster Recommendations - Warning Details - '" + objVcClusterRecommendation.warningDetails.message + "'.");
        }

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

    });

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

    let objVcTask: 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