Skip navigation
VMware

This Question is Answered

1 "correct" answer available (10 pts) 2 "helpful" answers available (6 pts)
5,155 Views 7 Replies Last post: Dec 10, 2008 11:58 AM by FrankvdS RSS
FrankvdS Novice 8 posts since
Nov 19, 2005
Currently Being Moderated

Dec 3, 2008 11:44 AM

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

 

 

Niket Hot Shot VMware Employees 91 posts since
Jan 10, 2008
Currently Being Moderated
1. Dec 3, 2008 10:13 PM in response to: FrankvdS
Re: How to move a (just created Template) to a specific datastore

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

yboychev Hot Shot VMware Employees 151 posts since
Mar 5, 2008
Currently Being Moderated
3. Dec 4, 2008 3:45 AM in response to: FrankvdS
Re: How to move a (just created Template) to a specific datastore

 

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

 

 

yboychev Hot Shot VMware Employees 151 posts since
Mar 5, 2008
Currently Being Moderated
5. Dec 5, 2008 4:36 AM in response to: FrankvdS
Re: How to move a (just created Template) to a specific datastore

 

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

 

 

 

 

 

Niket Hot Shot VMware Employees 91 posts since
Jan 10, 2008
Currently Being Moderated
6. Dec 5, 2008 6:07 AM in response to: FrankvdS
Re: How to move a (just created Template) to a specific datastore

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

Bookmarked By (0)

Share This Page

Communities