In 1495, Leonardo da Vinci conceptualized what many consider one of the first robots: a mechanical knight. Though it was built primarily for entertainment, this early automaton could move its arms, sit down, and even open its mouth—an impressive feat for its time. While it wasn’t preparing meals or cleaning up, it clearly shows that the desire to automate tasks is nothing new.
Today, we have far more advanced tools at our disposal—and automation is more accessible than ever. In this article, I’ll walk you through two simple automation tasks you can start using right away to streamline your daily routine.
Create a New Folder in vCenter
One of the most basic but useful automation tasks is creating a new folder in vCenter. This can be implemented either as a scriptable task or, preferably, as a reusable action element.
To perform this task, you only need two variables: datacenter and folderName. Their types are indicated in the JSDoc comments:
/**
* datacenter {VC:DatacenterFolder}
* folderName {string}
*/
function createFolder(datacenter, folderName) {
var folder = datacenter.createFolder(folderName);
System.log(“Folder created: ” + folder.name);
return folder;
}
// Usage
var folder = createFolder(datacenter, “NewFolderName”);
Running this script will create a new folder named NewFolderName in the specified datacenter.
Clone a Virtual Machine (VM)
While I generally recommend avoiding cloning production VMs, there are many scenarios where cloning is useful and appropriate. Here’s how you can automate the cloning process.
To clone a VM, you’ll need to create instances of two vCenter classes:
– VcVirtualMachineCloneSpec
– VcVirtualMachineRelocateSpec
While it’s fairly intuitive that you need a clone specification, the relocate specification might not be as obvious. The key detail is that the clone spec requires a location property, which tells vCenter where to place the new VM. If you want to clone the VM to the same location as the original, you can simply initialize the relocate spec without defining any new parameters—it will inherit the source VM’s settings.
Here’s the code:
/**
* vm {VC:VirtualMachine}
* folder {VcFolder}
* cloneName {string}
*/
function cloneVM(vm, cloneName, folder) {
var cloneSpec = new VcVirtualMachineCloneSpec();
cloneSpec.powerOn = false;
cloneSpec.location = new VcVirtualMachineRelocateSpec();
var clone = vm.cloneVM_Task(folder, cloneName, cloneSpec);
System.getModule(“com.vmware.library.vc.basic”).vim3WaitTaskEnd(clone, true, 2);
System.log(“Cloning task started for: ” + cloneName);
}
// Usage
cloneVM(vm, “CloneVMName”, folder);
Summary
Even small, reusable functions like these can make a big difference in your daily workflow. Investing a little time in automation can save you countless hours in the long run.
💡 Enjoyed this post? Consider sharing it with a friend or colleague! Your support helps grow the community and brings in more like-minded tech enthusiasts.
I’d also love to hear your feedback! Drop a comment to let me know:
– What topics you’d like to see covered in future posts
– What you found most helpful or interesting in this one
I read and respond to every comment—thanks for being part of the journey!