The ability to shutdown workloads in the cloud based off of workload type, application, environment, etc which can be crazy powerful… and will save you and/or your company a few bucks.

In this video, I will walk you through the vRealize Orchestrator workflows I created to accomplish this as well as how I automatically create tags when the VMs are spun up.

Find the code below for how to power the VMs on and off. Enjoy!

 for(var i = 0; i < vmArray.length; i++){
azureVM = resourceGroupAttr.getVirtualMachineByName(vmArray[i]);

// Extract the current tags  (This can be pulled from the request form where needed)
var myVMTags = azureVM.tags

      if(myVMTags.hasOwnProperty('Environment')){
            System.log("Azure VM " + azureVM.name + " Environement is: " + myVMTags['Environment'])
            if(myVMTags['Environment'] = "Dev"){
                  var azureVMPowerState = azureVM.powerState
                  if(azureVMPowerState = "running"){
                        //Add tag to VM stating vRO powered down the box, used for PowerOn.
                        myVMTags.put("PowerControl", "vRO");
                        azureVM.tags = myVMTags;
                        //System.log("Updating Azure VM Tags for " + azureVM.name + " in Azure Cloud: STARTING");
                        var createVmResponse = connection.computeClient.virtualMachinesOperations.createOrUpdate(resourceGroupAttr.name, azureVM);

                        if (createVmResponse.error) {
                              System.log("Updating Tags failed on: " + azureVM.name +" - Code=" + createVmResponse.error.code + ", message=" + createVmResponse.error.message);
                        }
                        
                        azureVM.powerOff() // this will only power down the OS, and you will incur compute charges
                        azureVM.deallocate()      //This will stop the compute charges
                        System.sleep(1000);
                        System.log(azureVM.name + " has been safely powered down and deallocated.");
                  }
            }
      }
}