Sunday, 12 March 2017

Automate Virtual Machines creation using vSphere PowerCLI

Or, How to create VM automatically using vSphere PowerCLI
Or, Easiest way to automate Virtual Machines creation
Or, Automating creation of Virtual Machines in VMware

Descriptions: Being a VMware administrator this is something a routine job to create Virtual Machines. When it is one or two VM, that is easy but what if you have to create at least 10-20 VMs in next 10-15 minutes? That is not possible to achieve manually.

In this scenario, you must be having some automation in place to do this job for you, off-course in 10-15 minutes depending on your Infrastructure capabilities. There are multiple ways to automate this task depending on your requirement.

Important Note: There are many articles on the web where some people have suggested creating VM using New-VM cmdlet without using any template but my friends that is not the real VM creation. It is just a VM instance without Guest OS where you have to install Guest OS manually, which is time taking process, as you already know and this is the one pain area you want to be automated.

In this article, we will be using VM template to automate the VM creation with one click approach.

The only few things you need to do after VM creation are:
1. Change Computer/Host Name inside Guest OS
2. Assign appropriate network port group to the newly created VM

1. You must have fully patched VM template created in your VM Infra
2. Basic understanding of vSphere PowerCLI
3. vSphere PowerCLI Installed on your machine
4. Administrative privileges to complete this task
5. Following Information handy (Template Name, Datastore Name and Cluster Name)

Open vSphere PowerCLI > Type the command below and hit enter

Connect-VIServer IPAddressOfvCenterServer

To prepare for the automation, please run below commands sequentially:
    $MyTemplate = Get-Template -Name MyTestTemplateVM
    $myDatastore = Get-Datastore -Name MyTestDatastore
    $myCluster = Get-Cluster -Name MyTestCluster

Note: Please replace MyTestTemplateVM, MyTestDatastore, and MyTestCluster with your VM Template Name, Datastore name and Cluster name respectively.

That’s it, your automation environment is ready to go… just a single command (when repeating just up arrow and one click) to create your virtual machine automatically.

Now run the below given command to start creation of your Virtual machine:

New-VM -Name NewVMName -Template $MyTemplate -Datastore $myDatastore -ResourcePool $myCluster

Note: Replace NewVMName with your VM Name.

Cheers, Please write me back if you have any query or feedback on this.

No comments:

Post a comment