Let's just say that i have 60 VMs' to clone.
I have a .csv file with required params (with headers like this):
SourceVMName,DestVMName,DestDatastore,DestESXHost,DestVMFolder,DestVMResourcePool
and the following is the extract of cloning script that does the work.
.................
$InputFile=$args[0]
$VC="MyVC"
Connect-VIServer $VC
$counter = 0;
import-csv $InputFile | %{
$txtline = $_;
$SourceVMName = $txtline.SourceVMName;
$DESTDS= $txtline.DestDatastore;
$DESTFLDR= $txtline.DestVMFolder;
$DESTVMName=$txtline.DestVMName;
$DESTVMHost=$txtline.DestESXHost;
$DESTRESPOOL=$txtline.DestVMResourcePool
$targetfolder = get-view (get-folder -Name $DESTFLDR).ID
$VMCloneSpec = New-Object VMware.Vim.VirtualMachineCloneSpec
$VMCloneSpec.Location = New-Object VMware.Vim.VirtualMachineRelocateSpec
$VMCloneSpec.Location.datastore = (Get-View (Get-Datastore -Name $DESTDS).ID).MoRef
$VMCloneSpec.Location.host = (Get-View (Get-VMHost -Name $DESTVMHost).ID).MoRef
$VMCloneSpec.Location.pool=(Get-view (Get-ResourcePool -Name $DESTRESPOOL).ID).MoRef
$tasks= (Get-View (Get-VM -Name $SourceVMName).ID).CloneVM_Task($targetfolder.MoRef, $DESTVMName, $VMCloneSpec)
start-sleep -s 300;
$counter + = 1
} ;
# End cloning process...
write-host "Cloned '$counter' VM's. " -Fore "Green";
What i miss is a part of putting the tasks in some collection and monitoring the cloning events on vCenter. Once the cloning events reach the value i confgure (let's 10 VMs) the script should wait before continuing with cloning of the following VM. The current script executes all cloning tasks in sequence. It works but not as i need it.
Anyone done this before?
thanks in advance
A.S.
Which vSphere version and PowerCLI version are you using ?
If it is in vSphere 4.x you could do something like in my About Async tasks, the Get-Task cmdlet and a hash table post.
In the newer PowerCLI versions the Task objects returned by Async tasks have been changed and that script will require some changes.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
vCenter 4.x and esx 4.xi.
Easier sad than done. I will try to modify it. If you can help me with some (more) hints.
thx
Hmmm, It seems that i need some sort of check/control for cloning events being executed.
How do i do that?
$InputFile=$args[0]
$VC="MyVC"
Connect-VIServer $VC
$CloneEventTreshholdvalue=10
$counter = 0;
import-csv $InputFile | %{
$txtline = $_;
$SourceVMName = $txtline.SourceVMName;
$DESTDS= $txtline.DestDatastore;
$DESTFLDR= $txtline.DestVMFolder;
$DESTVMName=$txtline.DestVMName;
$DESTVMHost=$txtline.DestESXHost;
$DESTRESPOOL=$txtline.DestVMResourcePool
$targetfolder = get-view (get-folder -Name $DESTFLDR).ID
$VMCloneSpec = New-Object VMware.Vim.VirtualMachineCloneSpec
$VMCloneSpec.Location = New-Object VMware.Vim.VirtualMachineRelocateSpec
$VMCloneSpec.Location.datastore = (Get-View (Get-Datastore -Name $DESTDS).ID).MoRef
$VMCloneSpec.Location.host = (Get-View (Get-VMHost -Name $DESTVMHost).ID).MoRef
$VMCloneSpec.Location.pool=(Get-view (Get-ResourcePool -Name $DESTRESPOOL).ID).MoRef
$tasks= (Get-View (Get-VM -Name $SourceVMName).ID).CloneVM_Task($targetfolder.MoRef, $DESTVMName, $VMCloneSpec)
# missing part
# I guess i need first to ask vCenter for all running cloning events,
If amount of "VMbeingClonedEvents" -gt $CloneEventTreshholdvalue then
start-sleep -s 60;
loop
$counter + = 1
} ;
# End cloning process...
write-host "Cloned '$counter' VM's. " -Fore "Green";