VMware Cloud Community
MartijnBalink
Contributor
Contributor
Jump to solution

Specify datastore per disk on template clone

I have a template that I need to roll out multiple times through PowerCli.
The template has three seperate virtual disks.
I want the first disk to be on a DatastoreA and I want disks two and three to be stored on DatastoreB. 
I now simply clone everything using this command: 

new-vm -name MyNewVM -template MyTemplate

Can anyone point me in the right direction for specifying the datastore on a per-disk level?

Thanks!

1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Sure.
You can replace this code

$templateName = 'MyTemplate'

$osCustName = 'MyCustomization'

$esx = Get-VMHost -Name MyEsx

$template = Get-Template -Name $templateName

$osc = Get-OSCustomizationSpec -Name $osCustName

New-VM -Name TestVM -Template $templateName -VMHost $esx -OSCustomizationSpec $osc

With this code (which will also allow you specify the destination datastores of the individual disks - see sample mentioned above)

$templateName = 'MyTemplate'

$osCustName = 'MyCustomization'

$folder = Get-Folder -Name vm

$esx = Get-VMHost -Name MyEsx

$pool = Get-ResourcePool -Name Resources

$template = Get-Template -Name $templateName

$osc = Get-OSCustomizationSpec -Name $osCustName

$spec = New-Object VMware.Vim.VirtualMachineCloneSpec

$spec.Customization = $osc.ExtensionData.Spec

$loc = New-Object VMware.Vim.VirtualMachineRelocateSpec

$loc.Host = $esx.ExtensionData.MoRef

$loc.Pool = $pool.ExtensionData.MoRef

$spec.Location = $loc

$template.ExtensionData.CloneVM($folder.ExtensionData.MoRef,'TestVM',$spec)


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

View solution in original post

0 Kudos
7 Replies
LucD
Leadership
Leadership
Jump to solution

I'm afraid the cmdlet doesn't offer that functionality yet.

You'll have to use the Clone_VM method.

I provided a small skeleton in Re: Clone VM with Multiple .VMDK https://vdc-repo.vmware.com/vmwb-repository/dcr-public/6b586ed2-655c-49d9-9029-bc416323cb22/fa0b429a...

Let me know if you can work from that, or if you want more info.


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

MartijnBalink
Contributor
Contributor
Jump to solution

Hi Luc,
That did the trick, thanks!!!! :smileyplus:

Martijn

0 Kudos
MartijnBalink
Contributor
Contributor
Jump to solution

Hi Luc, One more question: 
In my opening post I cut off all additional commands and parameters from the new-vm command. 
I use customization to rename the (windows) VM, make it a member of active directory etc. 
So my actual command (used to creata VM from a template) was :

new-vm -name MyNewVM -template MyTemplate -OSCustomizationSpec myCustomizations

I have converted the master template to a VM in order to use the script you provided, but how can I pass my customization job to the $vm.ExtensionData.CloneVM commandlet?

0 Kudos
LucD
Leadership
Leadership
Jump to solution

In the VirtualMachineCloneSpec, under the Customization property, you have the CustomizationSpec object.

The information you have to provide in this object can be obtained through the CustomizationSpecManager

In there, through the Info property, you can select the desired OSCustomizationSpec via the Name property.

Does that help?
Otherwise I can provide some sample code.


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

0 Kudos
MartijnBalink
Contributor
Contributor
Jump to solution

Hi Luc, 

A small piece of sample code would be very much appreciated 🙂

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Sure.
You can replace this code

$templateName = 'MyTemplate'

$osCustName = 'MyCustomization'

$esx = Get-VMHost -Name MyEsx

$template = Get-Template -Name $templateName

$osc = Get-OSCustomizationSpec -Name $osCustName

New-VM -Name TestVM -Template $templateName -VMHost $esx -OSCustomizationSpec $osc

With this code (which will also allow you specify the destination datastores of the individual disks - see sample mentioned above)

$templateName = 'MyTemplate'

$osCustName = 'MyCustomization'

$folder = Get-Folder -Name vm

$esx = Get-VMHost -Name MyEsx

$pool = Get-ResourcePool -Name Resources

$template = Get-Template -Name $templateName

$osc = Get-OSCustomizationSpec -Name $osCustName

$spec = New-Object VMware.Vim.VirtualMachineCloneSpec

$spec.Customization = $osc.ExtensionData.Spec

$loc = New-Object VMware.Vim.VirtualMachineRelocateSpec

$loc.Host = $esx.ExtensionData.MoRef

$loc.Pool = $pool.ExtensionData.MoRef

$spec.Location = $loc

$template.ExtensionData.CloneVM($folder.ExtensionData.MoRef,'TestVM',$spec)


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

0 Kudos
MartijnBalink
Contributor
Contributor
Jump to solution

Thanks again!
I was afraid I would need to clone my template to one datastore and then move some disks around to other LUNs in order to achieve what I need, this does the trick perfectly, thanks!

0 Kudos