FrankvdS
Contributor
Contributor

How to move a (just created Template) to a specific datastore

I just started to use VI Toolkit

Trying to create a template and store it on a particular datastore.

So far I succeeded in creating the template, but I discovered it is created in the same store as used for the VM.

Because there is no option to specify a datastore within New-Template, I assume I have to move it afterwards.

Do I have to use the Move-VM cmdlet for this?

To create the Template I used follow script

$VMName = "TestSrv"

New-Template -VM (Get-VM -Name $VMName -Name $VMName + "_Template" -Location (Get-Datacenter "MyDataCenter")

Frank

Tags (1)
0 Kudos
7 Replies
Niket
Enthusiast
Enthusiast

Hi,

There is no direct way to move a template using Move-VM. In below script, first I convert the template to VM then move it to other datastore and again converted it to template.

Connect-viserver -server

$VMName = "TestSrv_1"

$tempName = "Test_Template_1"

New-Template -VM (Get-VM -Name $VMName) -Name $tempName -Location (Get-Datacenter "Datacenter")

Get-VM (Set-Template -ToVM $tempName) | Move-VM -Destination (Get-VMHost -VM $VMName) -Datastore (get-datastore "Target_DatastoreName")

$vm = Get-VM $tempName | Get-View

$vm.MarkAsTemplate()

I hope it helps you.

Thanks

Niket

0 Kudos
FrankvdS
Contributor
Contributor

Hi Niket,

Thanks for the reply.

The procedure given was the 'work arround' I was thinking of also. But then the convert to Template became an issue for me. (see other post).

But that you answered for that to!

Thanks!

Frank

0 Kudos
yboychev
Hot Shot
Hot Shot

Hi FrankvdS,

Moving templates between datastores has been identified as a feature request for the VITK. Look forward for a solution in the upcomming releases. Till then the workaround mentioned looks like a good temporary solution.

\Yavor

0 Kudos
FrankvdS
Contributor
Contributor

Hi Niket,

A question about proposed script:

Get-VM (Set-Template -ToVM $tempName) | Move-VM -Destination (Get-VMHost -VM $VMName) -Datastore (get-datastore "Target_DatastoreName")

Whhy is $VMName used in Get-VMHost i.s.o.

$tempName; assuming we want to Get the host containing the just to VM converted template.

Frank

0 Kudos
yboychev
Hot Shot
Hot Shot

Hi Frank,

It's the same host anyway, doesn't matter how you'll retrieve it.

Now I have done a simple script that let you create the template on the desired datastore. The script uses the .Net part of the VITK. You can use it to get yourself functionality that is not covered by the cmdlets and the nice part is both can interact pretty easily

Connect-VIServer -Server ...

$vmToClone = Get-Vm -Name VmToclone

$dc = Get-Datacenter -Name TargetDatacenter

#This is the vm folder of the datacenter

$dcVmFolder = (Get-View -Id $dc.Id).VmFolder

$ds = Get-Datastore -Name TargetDatastore

#This is the .Net view object of the virtual machine object

$vmToCloneView = Get-View -Id $vmToClone.Id

$cloneSpec = New-Object VmWare.Vim.VirtualMachineCloneSpec

$cloneSpec.Location = New-Object Vmware.Vim.VirtualMachineRelocateSpec

$cloneSpec.Location.Datastore = (Get-View -Id $ds.Id).Moref

$cloneSpec.template = $true

#you can use the CloneVm_Task method instead for Async mode

$result = $vmToCloneView.CloneVm($dcVmFolder, "templateName", $cloneSpec)

#if you want to get the vi object

$template = Get-ViObjectByViView $result

0 Kudos
Niket
Enthusiast
Enthusiast

Hi,

I am breaking the line of code you mentioned.

#Converting the Template $tempName to VM

Get-VM (Set-Template -ToVM $tempName)

  1. Moving this template VM to another datastore

Move-VM -Destination (Get-VMHost -VM $VMName) -Datastore (get-datastore "Target_DatastoreName")

In Move-VM cmdlet -Destination is mandtory paramenter which accepts an object. When we create the template it created the the same host so to get the reference of that host we are referrring $VMName in Get-VMHost. You source machine is already coming from the pipe input of previous statement.

I hope it clears your query.

Thanks

Niket

0 Kudos
FrankvdS
Contributor
Contributor

Hi Niket,

Thanks again for the help. My script for moving templates runs just fine now.

0 Kudos