Skip to content

Month: January 2022

How to create Virtual Machines from CSV with PowerCLI on VMware

Whenever it’s possible I like to automate whatever I can to release myself for other activities and save a huge time that would be wasted on repeatable or time-consuming tasks.

I have been using pieces of the following script for a long time, in a “manual automation” way, let’s say. Getting blocks and loops of scripts and creating and configuring virtual machines as needed.

In the last month I put together in a way that can be shared, and even if you don’t have much experience with powershell, you can still make use of it. 🙂

Prerequisites

Install PowerCLI Module

The offline installation is available here.

Virtual Machine Template

Create a virtual machine template to be used as a base image for the deployment. No special requirements here, create accordingly to your environment.

VM Customization Specifications

Create two virtual machine customization specifications on vCenter (Linux and Windows). The only requirement is to set DNS under network section for the Linux, otherwise you might have issues after the deployment within the guest os.

CSV File

That’s the source file with all virtual machine details. There must be certain columns with specific values. These values are shown below:

  • Name
  • vCPU
  • vRAM
  • vNetwork
  • vDisk2 ..N
  • IP
  • Subnet
  • Gateway
  • Datastore
  • VMFolder
  • Specs
  • Template
  • Notes
  • OSType
  • Dns1
  • Dns2

You can download an example spreadsheet in the link below.

Each line will be specific to a virtual machine. That’s how the loop will read and create/configure accordingly.

vDisks columns can be increased as many required (vDisk2, vDisk3, vDisk4, etc.). For those VMs which doens’t use the additional disks, put “0” where not required. It starts at vDisk2 to be easily identified later, as vDisk1 will be the OSDisk from template. The vDisk format can be also changed, line 63 in the script.

The OSType value should be either “Windows” or “Linux“, that’s just a condition in order to properly run the VM creation per GuestOS.

Running the script it will always prompt for the CSV file, insert the full path location.

The next step will prompt vCenter credentials, once confirmed it won’t be prompted again if you run the script on the same powershell session.

The script then starts to read each value and deploy the VMs accordingly:

After initial deployment, the script will configure the VMs, in that section custom settings are configured, like vCPU, vRAM, vNetwork, etc.

At the end you should see a message of deployment complete and the VMs will be powered on, and probably customization specifications will be running. After that the virtual machines will be ready for use and fully customized.

Check the full script below, save as *.ps1 and use Powershell ISE or Powershell to run.

Enjoy 🙂

9 Comments