PowerCLI – Deploy and customize a VM in vCenter

VMware vSphere PowerCLI is a command-line interface (CLI) tool for automating vSphere and vCloud management. PowerCLI commands are executed in Windows PowerShell through using PowerShell cmdlets and the PowerShell syntax. You can use and integrate PowerCLI cmdlets with all other PowerShell cmdlets. This makes PowerCLI a very powerful tool for all your vSphere and vCloud automation needs.
In this blog I’ll show you how to deploy and configure a Windows VM through using a PowerShell/PowerCLI script. You can customize this script to suit your needs. What will this script do?

  • Deploy a Windows VM, using a preconfigured Windows template and a customization specification in vCenter
  • Configure a fixed IP address
  • Join an Active Directory Domain
  • Run every possible PowerShell command afterwards to configure the Windows VM

Installation of PowerCLI
Version 6.5 and higher of PowerCLI can be installed and configured in PowerShell 5.0 and higher using the following commands:

Find-Module VMware.PowerCli | install-module

Set-PowerCLIConfiguration -Scope User -ParticipateInCEIP $false (or $true if you want to participate in the Customer Experience Improvement Program)

Import-Module VMware.PowerCLI

Set-PowerCLIConfiguration -InvalidCertificateAction ignore -confirm:$false

Use of the PowerCLI Script
Configure the script for your own needs. The next sections explains the configuration process. After you configured the script, you can start the script in Powershell in order to install a Windows VM that is customized to your requirements.

Configuration of the PowerCLI Script

To define all credentials, create a textfile credentials.txt in the same folder as the PowerCLI script. The content of this textfile is as follows:

DomainAdmin = "<domain admin username>"
DomainAdminPassword = "<password domain admin user>"
LocalUser = "<local admin username>"
LocalPassword = "<password local admin user>"
vCenterUser = "administrator@vsphere.local"
vCenterPass = "<password vCenter admin>"

User Defined Variables
In the section User Defined Variables you can configure the following parameters:

Parameter Value
$Domain Domainname to join, e.g. “test.local”
$vCenterInstance vCenter hostname or IP address to deploy VM, e.g. “vcenter.domain.local”
$Cluster vCenter cluster to deploy VM, e.g. “cluster1”
$VMTemplate vCenter VM template to deploy, e.g. “Windows2016”
$CustomSpec Customization Specification to use, e.g. “Windows2016”
$Location Folder in vCenter to deploy VM, e.g. “folder1”
$DataStore Name of datastore to deploy VM, e.g. “NFS01”
$DiskStorageFormat “thin” or “thick”
$NetworkName Name of Portgroup to use, e.g. “LAN”
$Memory Amount of memory for VM in GB, e.g. 4
$CPU Number of vCPUs for VM, e.g. 2
$DiskCapacity Disksize of VM in GB, e.g. 40
$SubnetLength Subnetlength IP address to use (24 means /24 or for VM, e.g. 24
$GW Gateway to use for VM, e.g. “”
$IP_DNS IP address DNS server to use, e.g. “”

Powershell Scripts
In the section Define Powershell scripts to run in VM after deployment, you can define all Powershell scripts you would like to run once the VM has been deployed. You can add a Powershell script with the function Add-Script. The syntax of this function is:

Add-Script '<powershell script %1, … %n>' @(variable 1, …, variable n) $true

If you want to insert the value of one or more variables (for instance, the variable with the value of the domain admin user) to the Powershell script that will be executed in the VM, you can use variable replacements. To use a parameter replacement, add %1, %2, …, %n in the Powershell script and add every variable after the Powershell script in the format @(variable 1, … variable n). For example:

Add-Script "New-NetIPAddress -InterfaceIndex 2 -IPAddress %1 -PrefixLength %2 -DefaultGateway %3" @($IP, $SubnetLength, $GW)

If it is necessary to reboot the VM after the Powershell script has been executed, add $true after the Add-Script function. If you don’t need variable replacements or a reboot, you can use the Add-Script function to solely define the powershell script. For example:

Add-Script 'Import-Module NetSecurity; Set-NetFirewallRule -DisplayGroup "File and Printer Sharing" -enabled True'

You can use as many add-script functions as you want.

The PowerCLI Script

5 oktober 2017
Ronald van Vugt