Using PowerCLI to schedule out mass clones creations at a specific time.

Table of Contents


There are many times when we will want to schedule VM clones within our environment. Doing this for one or two VMs is easy enough through the various clients but if you are needing to do this on dozens of VMs then this can become highly tedious. I found some examples on the internet but nothing that was effective on specifying resource pool and exact time when scheduling out the work. This script allows you to specify those items and fulfills my requirements.

The script.

There are two parts to this work. First is the script itself which require only two items to be changed.

  1. $notificationEmail In this field you will enter the email address that vCenter will send communications to when the job is executed.
  2. $filename In this field you will want to specify the full file location for the .csv which contains your information.
# This requires a secondary file with information located in the fileName section below.
$notificationEmail = '' #Notifcation email that will be informed about the job completion.
$fileName = 'C:\tmp\clone.csv' #Full file location .csv that includes the details for the VMs.

#There should be no reason to edit anything below.
Import-Csv -Path $fileName -UseCulture | %{
$VM = Get-VM -Name $_.srcVMName

$ma = New-Object VMware.Vim.MethodAction
$ma.Argument = $null
$ma.Name = "CloneVM_Task"
$ar1 = New-Object VMware.Vim.MethodActionArgument
$ar1.Value = $vm.Folder.ExtensionData.MoRef
$ma.Argument += $ar1
$ar2 = New-Object VMware.Vim.MethodActionArgument
$ar2.Value = "Clone-" + $_.srcVMName
$ma.Argument += $ar2
$ar3 = New-Object VMware.Vim.MethodActionArgument
$vmSpec = New-Object VMware.Vim.VirtualMachineCloneSpec
$vmSpec.Location = New-Object VMware.Vim.VirtualMachineRelocateSpec
$vmSpec.Location.Datastore = $vm.ExtensionData.Datastore[0]
$vmSpec.Location.Pool = ($selectedHostObj | Get-ResourcePool $_.resourcepool).ExtensionData.MoRef
$vmSpec.Location.Transform = [Vmware.Vim.VirtualMachineRelocateTransformation]::sparse
$vmSpec.Config = New-Object VMware.Vim.VirtualMachineConfigSpec
$vmSPec.Template = $false
$vmSpec.PowerOn = $false
$ar3.Value = $vmSpec
$ma.Argument += $ar3

# Setting the date and time to run.
$dTScheduler = New-Object VMware.Vim.OnceTaskScheduler
$dTScheduler.runat = $_.time

# Create the scheduled task
$tSpec = New-Object VMware.Vim.ScheduledTaskSpec
$tSpec.Action = $ma
$tSpec.Description = "Clone-" + $_.srcVMName
$tSpec.Enabled = $TRUE
$tSpec.Name = "Clone-" + $_.srcVMName
$tSpec.Notification = $notificationEmail
$tSpec.Scheduler = $dTScheduler

$stMgr = Get-View ScheduledTaskManager

The CSV file.

Within the csv we are going specify the VMs, date/time the job will start, and the resource pool they should go into. The resource pool should be exactly as it is called in the vCenter. If you are using vCloud Director and the resource pool includes a long GUID then this should be included as well.

NOTE: The time is configured as UTC by default, keep this in mind when specifying the date/time in the CSV file.

srcVMName,time,resourcepool,11/25/18 21:10,Test-ResourcePool,11/25/18 21:15,Test-ResourcePool,11/25/18 21:15,Test-ResourcePool,11/25/18 21:15,Test-ResourcePool,11/25/18 21:15,Test-ResourcePool,11/25/18 21:15,Test-ResourcePool,11/25/18 21:15,Test-ResourcePool,11/25/18 21:15,Test-ResourcePool

How to use this script.

  1. Create the VM information file, located at C:\tmp\clone.csv in our example, and fill it with the data like above.
  2. Open notepad (or your other favorite text editor) and paste the above script into it. Once done save the file to a directory where your PowerCLI scripts are saved it. Ex: C:\PowerCLI\Clone-multipleVMs.ps1
  3. Open a connection to PowerCLI and authenticate to your vCenter. Ex: connect-viserver
  4. Change directories to where you created the ps1 file. Ex: cd C:\PowerCLI
  5. Execute the script by calling out the filename you created. Ex: .\Clone-multipleVMs.ps1
  6. Go to scheduled tasks in the vCenter to ensure the correct VMs are selected and they are scheduled at the appropriate times.