Build VM from Template: Interactive PowerCLI Script – VM-Builder-T.ps1

Introduction

Hi Guys,
In today’s post nothing special πŸ™‚ , I just wanted to share a script I wrote that will build a VM in VMware vSphere environment from a template.
In this post I will detail the workflow and pieces of the code.
At the end of the post, I will place a link to my GitHub repository where you will find the script.
Feel free to read the code and reuse it, if you’re happy with it.

This script contains a full help section that you can invoke by typing the following line:

Get-Help .\VM-Builder-T.ps1 -ShowWindow

As a reminder for those who know my blog πŸ˜‰ this help section is what we call in PowerShell a “Comment-based help”.
I’ve already wrote a post about “Comment-based help” section that you can find here PowerShell Scripts: Help yourself and others. I hope you will enjoy if don’t know about it. πŸ™‚

Ok! Intro is done :-), let’s start the fun part πŸ˜‰

Context

In the VMware environment I work for, we have different teams that interact with vSphere. We have a sysadmin team that is doing regular actions on VMs without having advanced experience with vSphere. Those guys complained about the number of clicks needed to build VM without talking about the vSphere UI that comes slow time to time and so on…
So I decided to write a PowerCLI script that will help them to build VM in a more straight forward way than the GUI and removing potential errors.

The script: VM-Builder-T.ps1

Interactivity

This script is interactive which gives the user a feeling of control on what’s happening. I try to render my scripts interactive especially when I need to deliver them to other teams. The interactivy, I’ve placed in the script permits the user to ingest the data required for the deployment of the VM and check it before running the commands (You know, always better to check twice πŸ™‚ !! )

Workflow

I think one of the best thing you can do when writing script is to document it by writing down the workflow. It will help you to validate the process and optimize it if needed. In other situations, it will help others to troubleshoot or prepare a new version of the script.
As I like document my work (yes it happens !! πŸ˜‰ ) and I want to render you confident to run it in your environment (if you wish). My best advice, before running it in prod, try to run it in a lab environment, just to be sure it will run as it should in your context… Even if the code is correct, the context can generate issues or failures :-/ (Welcome in IT πŸ™‚ )

So you can find here under the workflow of this script:

script workflow
  • User input is represented by : yellow parallelogram
  • User decision is represented by pink diamond
  • Script action is represented by blue rectangle

The workflow can be divided in 5 steps

script steps explained

Error Handling

To ensure the good processing of the script I’ve tackled error handling in 3 ways:

  • Do…Until loop (permits the user to review and validate his input
  • Try…Catch (permits a clear understanding of a failure in case of a terminating error)
  • Out-Gridview -passThru (prevents typo error by presenting a window to the user by selecting data instead of typing it)

As explained here above, the Out-Gridview -passThru cmdlet will retreive the data from the vCenter instead of having the user of the script typing manually the name of the concerned object.

Here is an example of the code for determining on which ESXi host the VM will be initially placed

Out-gridView cmdlet explained

User Input

To be able to deploy a VM with PowerCLI, you, as a user of the script must have specific data in your hands. Here is the list of components you need before running the script

Mandatory input to ensure the good processing of the script

The data of each component will be stored in variables via the Out-Gridview -Passthru cmdlet

Prerequisites

As always, to be able to run the script, your environment /working machine must satisfy some requirements. Here is the list identified in the version 1.0 of the script

Prerequisites for running the script

Conclusion

This script is not a very complicated one but it permits a sysadmin to create easily a VM in a vSphere environment by eleminating potential human errors and vSphere GUI issues.
I hope this post / script will help someone at least by getting ideas to complete his own script.
As mentionned above, feel free to test and use it. As always, run it first in a lab envrionment if you want to use it as is.

It has been tested and validated in a vSphere 6.7 U3 environment. No bug or issue have been reported. The script is in version 1.0, some improvement will come soon. Feel free to check regulary on my github public repository. Don’t hesitate to rate this post as it permits me to have an idea of your degree of sastisfacation of the post. It’s always a source of improvements !! πŸ™‚ πŸ™‚

Link to my script: https://github.com/v-itpassion-be/powercli/blob/main/VM-Builder-T.ps1

Thx to the community !!!

I hope this post helped you,
Dont’ hesitate to comment, like or rate,
Your host,

You can always reach me here: