VMware Cloud Community
lpalacio
Contributor
Contributor
Jump to solution

Wait operator on Linked Clones Script.

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.

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

You can try to wait till the is finished as follows

...

$taskMoRef = $sourceVM.CloneVM_Task( $cloneFolder, $cloneName, $cloneSpec )

$task = Get-View $taskMoRef

while("running","queued" -contains $task.Info.State){

        $task.UpdateViewData("Info")

        sleep 5

}

....


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

0 Kudos
2 Replies
LucD
Leadership
Leadership
Jump to solution

You can try to wait till the is finished as follows

...

$taskMoRef = $sourceVM.CloneVM_Task( $cloneFolder, $cloneName, $cloneSpec )

$task = Get-View $taskMoRef

while("running","queued" -contains $task.Info.State){

        $task.UpdateViewData("Info")

        sleep 5

}

....


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
lpalacio
Contributor
Contributor
Jump to solution

LucD,

Thank you that worked perfectly.

and in case anyone wants the final product here it is:

connect-viserver "vCenter5"
cls
$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
$taskMoRef = $sourceVM.CloneVM_Task( $cloneFolder, $cloneName, $cloneSpec )
$task = Get-View $taskMoRef
while("running","queued" -contains $task.Info.State){
         $task.UpdateViewData("Info")
Write-Host "Please Wait, Cloning $strtemplate to $strVMName$i"
        sleep 1
}
Write-Host "Please Wait Customizing $strVMName$i"
     Set-VM "$strVMNAME$i" -OSCustomizationSpec "CTX-Customization" -confirm:$false
     Start-VM "$strVMNAME$i" -RunAsync -confirm:$false
cls
}
0 Kudos