Skip to content

Instantly share code, notes, and snippets.

@safeaim
Last active January 3, 2019 19:08
Show Gist options
  • Select an option

  • Save safeaim/05c683364d100e6c8f84ecb390667f21 to your computer and use it in GitHub Desktop.

Select an option

Save safeaim/05c683364d100e6c8f84ecb390667f21 to your computer and use it in GitHub Desktop.

Revisions

  1. safeaim renamed this gist Jan 3, 2019. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. safeaim created this gist Jan 3, 2019.
    63 changes: 63 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,63 @@
    var vCAC_Machine = Server.findAllForType("vCAC:VirtualMachine","VMUniqueID eq '" + vm.config.instanceUuid + "'");
    var virtualMachineEntity = vCAC_Machine[0].getEntity(); //to get entity
    var vmEntityProps = virtualMachineEntity.getProperties(); //to get Entity properties for the next step

    vmEntityProps.remove('VMDNSName');
    vmEntityProps.put('VMDNSName', newName);
    vmEntityProps.put("VirtualMachineName", newName);

    var hostId = virtualMachineEntity.hostId;
    var modelName = virtualMachineEntity.modelName;
    var entitySetName = virtualMachineEntity.entitySetName;
    var entityIdString = virtualMachineEntity.keyString;

    //Magic!
    var Entity_Update = System.getModule("com.vmware.library.vcac").updateVCACEntity(hostId,modelName,entitySetName,entityIdString,vmEntityProps,null,null);
    var VM_update_Hostname = System.getModule("com.vmware.library.vcac").addUpdatePropertyFromVirtualMachineEntity(vCAC_host,virtualMachineEntity,"Hostname",newName,false,false,false,false);

    //bind variables
    var workflowPath = 'Library/vCenter/Virtual Machine management/Basic';
    var workflowNameToInvoke = 'Rename virtual machine';

    //create execution properties
    var executionProperties = new Properties();
    executionProperties.put('newName',newName);
    executionProperties.put('vm',vm);
    //find worfklow catagory and get all workflows
    var workflows = Server.getWorkflowCategoryWithPath(workflowPath).allWorkflows;
    //loop over each workflow and find the workflow based on name
    for each(var workflowObject in workflows){
    if(workflowNameToInvoke == workflowObject.name){
    //execute workflow and get workflowToken
    var currentToken = workflowObject.execute(executionProperties)
    //wait for workflow completions
    var complete = false;
    while(complete == false){
    if(currentToken != null && (currentToken.state != "running" && currentToken.state != "waiting")){
    System.log("Workflow '"+currentToken.name+"' terminated with status '"+currentToken.state+"'");
    complete = true;
    }
    }
    //get output from the workflow token
    var outputPropertiesToken = currentToken.getOutputParameters()
    System.log('outputPropertiesToken.keys: ' + outputPropertiesToken.keys)
    System.log("outputPropertiesToken.get('stringOutput'): " + outputPropertiesToken.get('stringOutput'))
    break;
    }
    }

    for each(var workflowObject in workflows){
    if(workflowNameToInvoke == workflowObject.name){
    //execute workflow and get workflowToken
    var currentToken = workflowObject.execute(executionProperties)
    //wait for workflow completions
    var complete = false;
    while(complete == false){
    if(currentToken != null && (currentToken.state != "running" && currentToken.state != "waiting")){
    System.log("Workflow '"+currentToken.name+"' terminated with status '"+currentToken.state+"'");
    complete = true;
    }
    }
    break;
    }
    }