Automation

 View Only
  • 1.  How to move a (just created Template) to a specific datastore

    Posted Dec 03, 2008 07:45 PM

    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



  • 2.  RE: How to move a (just created Template) to a specific datastore

    Posted Dec 04, 2008 06:14 AM

    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



  • 3.  RE: How to move a (just created Template) to a specific datastore

    Posted Dec 04, 2008 09:27 AM

    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



  • 4.  RE: How to move a (just created Template) to a specific datastore

    Posted Dec 04, 2008 11:46 AM

    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



  • 5.  RE: How to move a (just created Template) to a specific datastore

    Posted Dec 05, 2008 09:50 AM

    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



  • 6.  RE: How to move a (just created Template) to a specific datastore

    Posted Dec 05, 2008 12:37 PM

    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



  • 7.  RE: How to move a (just created Template) to a specific datastore

    Posted Dec 05, 2008 02:07 PM

    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



  • 8.  RE: How to move a (just created Template) to a specific datastore

    Posted Dec 10, 2008 07:58 PM

    Hi Niket,

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