I have a script to create linked clones (credit goes to members of this forum for helping me get it right) but i'm having issues with the customization as the script tries to apply the customization before the clone has completed and i get the following:
Script:
connect-viserver "vCenter"
$strtemplate = Read-Host "Please Provide a Template to Create Linked Clones From: "
$strVMName = Read-Host "Please Provide a Naming Convention, (Ex: CTX#)"
$strStart = Read-Host "Please Provide a Starting Naming Convention Number (EX: 1 = CTX1) "
$strEnd = Read-Host "Please Provide an Ending Naming Convention Number (EX: 3, will create CTX1, CTX2, CTX3) "
$intStart = [System.Convert]::ToInt32($strStart)
$intEnd = [System.Convert]::ToInt32($strEnd) + 1
for ($i=$intStart; $i -lt $intEnd; $i++) {
$sourceVM = Get-VM $strtemplate| Get-View
$cloneName = "$strVMNAME$i"
$cloneFolder = $sourceVM.parent
$cloneSpec = new-object Vmware.Vim.VirtualMachineCloneSpec
$cloneSpec.Snapshot = $sourceVM.Snapshot.CurrentSnapshot
$cloneSpec.Location = new-object Vmware.Vim.VirtualMachineRelocateSpec
$cloneSpec.Location.DiskMoveType = [Vmware.Vim.VirtualMachineRelocateDiskMoveOptions]::createNewChildDiskBacking
$sourceVM.CloneVM_Task( $cloneFolder, $cloneName, $cloneSpec )
Sleep 10
Set-VM "$strVMNAME$i" -OSCustomizationSpec "CTX-Customization" -confirm:$false
Start-VM "$strVMNAME$i" -RunAsync -confirm:$false
}
The error:
Set-VM : 1/25/2012 5:13:24 PM Set-VM The object has already been dele
ted or has not been completely created
At C:\Users\lpalacio\Documents\Scripts\VMware PowerCLI\Create-Linked-Ask.ps1:21
char:12
+ Set-VM <<<< "$strVMNAME$i" -OSCustomizationSpec "CTX-Customization" -co
nfirm:$false
+ CategoryInfo : NotSpecified: (:) [Set-VM], VimException
+ FullyQualifiedErrorId : Core_BaseCmdlet_UnknownError,VMware.VimAutomatio
n.ViCore.Cmdlets.Commands.SetVM
Is there a way to tell the script to wait until the clone is finished? i know i can use a sleep command but not all the clones will take the same amount of time to complete.
Thank you.