Hi all
I have few esx4.1i hosts running in vcentre 4.1. storage is provided by EMC SAN. I have a base VM,
I would like to clone 50 vm's from that base vm but with a uniquename.
For example my basevm name is BASEVM. I would like to have 50 vm's created with names like
newvm1
newvm2
...
...
newvm50
Can someone please let me know the command to clone 50 vm's from the base vm?
Regards
Matheen
Take a look at New-VM cmdlet and Example 4.
You can use foreach loop to create 50 clones of the base vm:
$vm = Get-VM BASEVM
$vmhost = Ge-VMHost
$datastore = Get-datastore <san_datastore_name>
1..50 | % { New-VM -Name newvm$_ -VM $vm -Datastore $datastore -VMHost $vmhost }
Regards,
Yasen
Hi Yasen
Thanks for the prompt reply. I will give this a try and let you know. BTW after declaring all the variables as you mentioned in your post and if I type
1..50 | % { New-VM -Name newvm$_ -VM $vm -Datastore $datastore -VMHost $vmhost }
Will it create 50 hosts with unique name or the same name?
Regards
Matheen
$_ is a Ps variables pointning to the current element of the foreach loop. So
1..50 | % { write-Host newvm$_ }
will write
newvm1
newvm2
newvm3
....
newvm50
Thanks Yasen. Your script is perfect. I put the below script
$vm = Get-VM BASEVM
$vmhost = Ge-VMHost
$datastore = Get-datastore <san_datastore_name>
1..50 | % { New-VM -Name newvm$_ -VM $vm -Datastore $datastore -VMHost $vmhost }
it started cloning vm's starting from
newvm01
newvm02.
...
But I don't understand what 1..50 | % { write-Host newvm$_ } this command does? Please explain
Thank you so much for the script. I would like to learn power cli scripting. Can you please suggest some good sources or video trainings?
Regards
Matheen
Hi Yasen
Also quickly.. If I want to move all these newly cloned to a resource pool (all at once) what command I use. I found out from the help I can use
Move-VM -VM VM -Destination ResourcePool But this will move one vm at a time, isn't it? Please let me know
Regards
Matheen
Yes, they will be moved to the resourcepool one by one.
That has nothing to do with PowerCLI but is because of the underlying RelocateVM_Task method that is used.
That method is called on a single VirtualMachine object, not on a collection of VirtualMachine objects.
And a move to a resource pool doesn't normally take that long
If you want your script to continue, while the guests are moved in the background, you can use the RunAsync parameter
Move-VM -VM VM* -Destination ResourcePool -RunAsync
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference