Follow @chaderoth

Saturday, November 11, 2017

How To Deploy Multiple VMs Using PowerCLI

As I have previously mentioned, working for an SMB and making up an Infrastructure Team of one can pose some unique challenges.

I was recently asked if I could spin up a number of VMs for a new Dev/Test environment for our Development Team.

This environment would require a number of groups of VMs running Microsoft Application Request Routing (ARR), Microsoft Internet Information Services (IIS), or Microsoft SQL Server.

I had been playing with VMware vSphere PowerCLI for things like moving all the VMs from one host to another for host maintenance as I don't have access to DRS or SDRS, but I hadn't done much more with it than that up to this point. Since I also do not have access to VMware vRealize Automation, I thought I would try creating a PowerCLI script to accomplish this task and to give myself a baseline for future automated deployments of multiple VMs.

Here is my first go at the script.

I had originally planned to include a full copy of the script at the bottom of this post, but since it has grown to be rather long you can find the script in its entirety here: Create-VMs

Load The VMware PowerCLI Module

While testing the script I grew tired of having to load PowerCLI as I like to use the PowerShell ISE for this. So I decided to load the PowerCLI Script Configuration File at the beginning of the script.

Define Variables To Connect To Server

Here I am prompting the user of the script for information needed to connect to an ESXi host or vCenter Server and storing the information into variables.

Define variables to Create VMs

Then prompt the user for information needed to create the VMs. I opted to create VM Templates in vCenter for the various server groups as I could patch and update the templates for easy deployment in the future. The user will be prompted for the template name for one of the variables.

Connect To Server

Connect to an ESXI host or vCenter Server using the credentials entered above.

Create Hash Table, Create VMs, and Populate Hash Table With IDs of VMs Being Created

Because I wanted to create multiple VMs at the same time I found I needed to run the PowerCLI command New-VM with the -RunAsync parameter. From the vSphere PowerCLI Cmdlets Reference, the RunAsync parameter "Indicates that the command returns immediately without waiting for the task to complete. In this mode, the output of the cmdlet is a Task object" Because the output is a Task object I needed to find a way to display the output. This led me to this blog post by
LucD: About Async Tasks, The Get-Task Cmdlet And A Hash Table.

Display VM Build Status

Here I am displaying the status of the VM build process. Again borrowing code from LucD.

Disconnect From Server

Once the tasks have completed and the hash table is empty the user is notified and the connection to the server is automatically closed.


I am still pretty new to PowerCLI, but this was definitely a fun project to tackle and will save me a bit of time in the future. I plan on cleaning up and improving the script which I will share in a later blog post. The script is still a bit of a manual process as it requires the user to manually input a number of parameters to connect to a server and for build information for the VMs. In a future version of the script I plan on trying to have the script query vCenter for this information and allow the user to pick their parameters. I hope someone out there finds this post helpful. Please feel free to comment with questions, ideas, or advice for areas of improvement!

Thanks for Reading!

No comments:

Post a Comment