«

»

Create multiple VMs from existing vSphere VM using PowerCLI

I recently published the blog post “Create multiple VMs using PowerCLI” and as i mentioned in the post i thought the next request would be a PowerCLI script to create multiple VMs from an existing vSphere virtual machine (VM). It just took a few days before the request reached my email inbox. I actually received a few email from my readers requesting the same thing and also a comment on the previous blog post.

The previous PowerCLI script used the following sequence:

  1. Issued the command for creating a VM.
  2. Waited until the VM was created.
  3. Powered the VM on.

When all three above steps were finished the next VM was created. The new request included an additional sequence where the script should continue as soon as the vCenter Server has accepted the “create new VM command”.
This means we will put much more pressure on the storage array and my customer wanted to verify the VAAI functionality.
And yes, the VM we used as the base VM had its disk in format “Thick Eager Zeroed”

There were a couple of requirements for the script as well.

  • Specify the number of VMs to be created.
  • Specify a customization specification to be used.
  • The VMs must be placed on all ESXi hosts in the vSphere cluster.
  • Specify a vCenter Server Folder in the Virtual Machine & Templates view where the VMs will be placed.
  • Specify VM datastore/datastore cluster placement.
  • Specify the VM prefix.
  • Use numeric incremental number after the VM prefix.

If you want to use the same sequence described in the “Create multiple VMs using PowerCLI” you can:

  • Remove the -RunASync from the below script.
  • Add the following lines, after the “New-VM” line but before the } sign:
    • write-host “Power On of the  VM $VM_name initiated”  -foreground green
    • Start-VM -VM $VM_name -confirm:$false -RunAsync

The script were tested using the following software versions:

  • VMware vCenter Server 5.5
  • VMware ESXi 5.5
  • VMware PowerCLI 5.5 (32-bit mode)
  • Microsoft PowerShell 2.0

Change the following parameters for the below script:

  • $vCenter=”vc-demo01.vcdx56.com”
  • $vCenterUser=”vcdx56@a_magnus”
  • $vCenterUserPassword=”notsecret”
  • $vm_count = “100”
  • $clone = “VCDX56-base-vm”
  • $customspecification=”VCDX56-customization”
  • $ds = “DSC-VDX56-01”
  • $Folder = “Scripttest”
  • $Cluster = “CLU-VCDX56-01”
  • $VM_prefix = “VCDX56-“
#
# PowerCLI to create VMs from existing vSphere VM
# Version 1.0
# Magnus Andersson RTS
#
# Specify vCenter Server, vCenter Server username and vCenter Server user password
$vCenter="vc-demo01.vcdx56.com"
$vCenterUser="vcdx56@a_magnus"
$vCenterUserPassword="notsecret"
#
# Specify number of VMs you want to create
$vm_count = "100"
#
# Specify the VM you want to clone
$clone = "VCDX56-base-vm"
#
# Specify the Customization Specification to use
$customspecification="VCDX56-customization"
#
# Specify the datastore or datastore cluster placement
$ds = "DSC-VDX56-01"
#
# Specify vCenter Server Virtual Machine & Templates folder
$Folder = "Scripttest"
#
# Specify the vSphere Cluster
$Cluster = "CLU-VCDX56-01"
#
# Specify the VM name to the left of the - sign
$VM_prefix = "VCDX56-"
#
# End of user input parameters
#_______________________________________________________
#
write-host "Connecting to vCenter Server $vCenter" -foreground green
Connect-viserver $vCenter -user $vCenterUser -password $vCenterUserPassword -WarningAction 0
1..$vm_count | foreach {
$y="{0:D1}" -f + $_
$VM_name= $VM_prefix + $y
$ESXi=Get-Cluster $Cluster | Get-VMHost -state connected | Get-Random
write-host "Creation of VM $VM_name initiated" -foreground green
New-VM -Name $VM_Name -VM $clone -VMHost $ESXi -Datastore $ds -Location $Folder -OSCustomizationSpec $customspecification -RunAsync
}

 

Important: The customspecification works in 32-bit mode only so make sure you run the script using the PowerCLI 32-mode version.

36 pings

Skip to comment form

  1. Create multiple VMs using PowerCLI | VCDX56

    […] 2014-02-25 – As i suspected, here you can find a post in which i create multiple VMs, based on one existing vSphere VM, using […]

  2. SQL Server Templates with VMware – Dude Where’s My Drive Letters? | Long White Virtual Clouds

    […] After the VM reboots you can log in and review the lot files diskpartlog.txt and powercfglog.txt to ensure the output is as you expect. You can also review the drive letters and that your SQL Database starts as expected. The first time you run through this you should verify that everything is as you expect it, then you can have confidence when you clone many more VM’s everything will work. If you want to clone lots of VM’s you might like to check out an article by Magnus Andersson (VCDX56) – Create multiple VMs from existing vSphere VM using PowerCLI. […]

  3. Newsletter: January 17, 2014 | Notes from MWhite

    […] you want to spin up a bunch of VM’s there is a nice script here that can help.  Nice […]

Comments have been disabled.