VMware Cloud Community
Dalebert
Contributor
Contributor
Jump to solution

Copying templates between datacenters

Hello,

I have a couple VM's that are being cloned to templates. Those templates need to be copied to other VMWare datacenters. I have everything running in a PowerShell script using PowerCLI. As I understand it I can't just do a simple copy from one datacenter to another. I first need to download the template then upload to the target datacenter to get those templates where they need to be.

Does anyone have an idea of how I could do this task and is it able to be accomplished using PowerCLI?

TIA

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Correct, you could do something along these lines.

$vmName = 'MyVM'

$templateName = 'template'

$tgtDC = 'dc'

$dc = Get-Datacenter -Name $tgtDC

# Get the source VM

$vm = Get-VM -Name $vmName

# Clone VM and move it to the target DC

New-VM -VM $vm -Name $templateName | Move-VM -Destination $dc

# Convert the cloned VM to a template

Get-VM -Name $templateName | Set-Template


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

View solution in original post

0 Kudos
26 Replies
LucD
Leadership
Leadership
Jump to solution

You can convert the template to a VM, then do a Move-VM and convert back to a template.


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

Dalebert
Contributor
Contributor
Jump to solution

So, instead of cloning to a template and trying to copy that template where ever, you're saying I can copy the VM to the different datacenters and then clone to a template?

As it stands now the process goes like this:

Source VM is updated > clone to template > provision new VM's from template

Your suggestion would like this:

Source VM is updated > Copy to other datacenters > clone to template on respective datacenters > delete copied VM's > provision new VM's from template

Correct?

If this is what you are suggesting then it makes sense. How would that be accomplished using PowerCLI?

Thanks again!

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Correct, you could do something along these lines.

$vmName = 'MyVM'

$templateName = 'template'

$tgtDC = 'dc'

$dc = Get-Datacenter -Name $tgtDC

# Get the source VM

$vm = Get-VM -Name $vmName

# Clone VM and move it to the target DC

New-VM -VM $vm -Name $templateName | Move-VM -Destination $dc

# Convert the cloned VM to a template

Get-VM -Name $templateName | Set-Template


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

0 Kudos
Dalebert
Contributor
Contributor
Jump to solution

Oh, I think this makes sense. I'm fairly new to PowerCLI, so I'll have to play around with it, but I see the logic.

Thanks for the advice!

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Feel free to ask when you get stuck


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

0 Kudos
Dalebert
Contributor
Contributor
Jump to solution

Hi. LucD.

I'm having trouble getting you example to work. Here is what I have:

  1. Import PowerCLIEnvironment.ps1
  2. Import a CSV that hold all the variables.
  3. Connect to the virtual hosts using the Connect-VIServer command
  4. use a ForEach loop to go through the CSV and copy the source VM to a target VM cluster

Here is the command and where it's breaking:

New-VM -VM {Soucre_VM_To_Copy} -Location {Name of the folder to put the new VM} -Name {Rename the source machine name} -VMHost {Target name of the VM cluster to copy the VM to}

Everything seems to run just fine until the line (above). It states "NEW-VM Could not find VMHost with name 'Target name of the VM cluster to copy the VM to'

So I'm confused on what it's looking for.

I've used the Move-VM and New-VM in the PowerCLI documents as a reference, but it doesn't make sense as to what it needs. Would you have some insight you could share with me?

Thanks!

0 Kudos
LucD
Leadership
Leadership
Jump to solution

You are connected to a vCenter and not an ESXi I assume ?

Which PowerCLI version are you using ?

Do a Get-PowerCLIVersion

Could you perhaps attach the script, and show a screenshot of the errors you are getting ?

And the layout of the CSV file you are using would also help in determining the cause of the problem (feel free to change the values, I just want to see the layout of the columns)


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

0 Kudos
Dalebert
Contributor
Contributor
Jump to solution

Hi, LucD.

The PowerCLI version is 5.5 Release 1 Build 1295336.

So here's what I've done. I used the Connect-VIServer to connect to two different hosts. It does connect to both and I know this because if I do a "Get-VM -Server {hostname} | FT Name", it returns the names of the VM's on each specified host.

So then I;

  1. Created a new var, "$Source = Get-VM -Server {Source VMHost name} -VM {VM Name}
  2. Tried the New-VM command: "New-VM -VM $Source -Name Copied_VM -VMHost {Target VMHost}

The output is where I get confused, and that is;

"New-VM Could not find a VMHost with name {Target VMHost}"

So, is this a limitation to PowerCLI that it is unable to create the new VM on a different virtual cluster? If not, where did I go wrong?

Thanks!

0 Kudos
Prakas
Enthusiast
Enthusiast
Jump to solution

I think you are trying to use Cluster name in -VMHost switch (which won't work).

Try using -ResourcePool switch instead of -VMHost. (ResourcePool switch accepts cluster as input parameter).

0 Kudos
Dalebert
Contributor
Contributor
Jump to solution

Hello, Prakash.

I was using the virtual host name that the VM should reside on, not the cluster name. I will try using the Resource Pool switch and report back the results.

Thanks!

0 Kudos
Dalebert
Contributor
Contributor
Jump to solution

Prakash/LucD,

I have used the ResourcePool switch in command rather than the VMHost, and it still errors. Here is my command and the output.

================================================================================

New-VM -VM "SRC_VM_W_12_R2_H" -Name "TPL_Windows_2012a" -ResourcePool "Hostname.domain.com"

New-VM : 4/27/2015 8:33:21 AMNew-VM    Could not find VIContainer with name 'Hostname.domain.com'.

At line:1 char:1

+ New-VM -VM "SRC_VM_W_12_R2_H" -Name "TPL_Windows_2012a" -ResourcePool Hostna ...

+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

+ CategoryInfo      : ObjectNotFound: (vb01vh006.vdc.cpr.ca:String) [New-VM], VimException

+ FullyQualifiedErrorId : Core_ObnSelector_SelectObjectByNameCore_ObjectNotFound,VMware.VimAutomation.ViCore.Cmdlets.Commands.NewVM

===============================================================================

*******************************************************************************************************************************

New-VM -VM "SRC_VM_W_12_R2_H" -Name "TPL_Windows_2012a" -VMHost "Hostname.domain.com"

New-VM : 4/27/2015 8:47:25 AMNew-VM    Could not find VMHost with name 'Hostname.domain.com'.

At line:1 char:1

+ New-VM -VM "SRC_VM_W_12_R2_H" -Name "TPL_Windows_2012a" -VMHost Hostname.do ...

+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

+ CategoryInfo      : ObjectNotFound: (Hostname.domain.com:String) [New-VM], VimException

+ FullyQualifiedErrorId : Core_ObnSelector_SelectObjectByNameCore_ObjectNotFound,VMware.VimAutomation.ViCore.Cmdlets.Commands.NewVM

******************************************************************************************************************************

Now, this is trying to create the new VM from one cluster to another. I don't know if that has anything to do with the above errors or not, though.

Any other ideas?

Thanks!

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Is Hostname.domain.com a resourcepool or a VMHost ?

You can do a Get-ResourcePool to check.


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

0 Kudos
Dalebert
Contributor
Contributor
Jump to solution

"Hostname.domain.com" is a host name of the cluster node that I'm trying to get the VM to. Here is what a Get-VM returns for that host:

PowerCLI C:\WINDOWS\system32> get-resourcepool -server Hostname.domain.com

Name                 CpuSharesL CpuReserva CpuLimitMH MemSharesL MemReservationG MemLimitGB

                     evel       tionMHz    z          evel       B

----                 ---------- ---------- ---------- ---------- --------------- ----------

Resources            Custom     42867      42867      Custom     498.983         498.983

Now, I changed my command to:

New-VM -VM "TPLSRC_CPP_W_12_R2_H" -Name "TPL_Windows_2012a" -ResourcePool "custom" -vmhost "Hostname.domain.com"

That was a failure as well stating it couldn't find "Hostname.domain.com."

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Can you do a Get-VMHost, just to make sure you are using the correct name ?


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

0 Kudos
Dalebert
Contributor
Contributor
Jump to solution

Yep, I'm connected to both hosts.

2015-04-27 10_49_19-VMware vSphere PowerCLI 5.5 Release 1.jpg

I hope you're able to see the screenshot. If not I'll upload, but bigger.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Ok, try like this

$vm = Get-VM -Name 'TPLSRC_CPP_W_12_R2_H'

$esx = Get-VMHost -Name 'host1.domain.com'

$respool = Get-ResourcePool -Name 'Resources'

New-VM -VM $vm -Name 'TPL_Windows_2012a' -VMHost $esx -ResourcePool $respool


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

0 Kudos
Dalebert
Contributor
Contributor
Jump to solution

I think we are getting closer.

2015-04-27 15_07_53-VMware vSphere PowerCLI 5.5 Release 1.jpg

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Could it be that you're connected to the ESXi host(s), and not the vCenter ?

Do a

$global:DefaultVIServers


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

0 Kudos
Dalebert
Contributor
Contributor
Jump to solution

OK, I think I'm confused about your question.

I have connected to the hosts where the source VM resides and the target of where I wanted to copy it to.

I decided to change it to this:

Connect-VIServer  -server 'ClusterSrcName.vm.domain.com' -User 'ServiceAcct' -Password 'Password'

Connect-VIServer  -server 'ClusterTgtName.vm.domain.com' -User 'ServiceAcct' -Password 'Password'

$vm = get-vm -name 'tplsrc_cpp_w_12_r2_h'

$esx = get-vmhost -Name 'VMSrcHost.vm.domain.com'

$ResPool = get-resourcepool -name Resources -server $esx

new-vm -VM $vm -Name 'TPL_Windows_2012a' -VMHost $esx -ResourcePool Resources

Result: This fails because the ResPool var is picking up 10 different resource pools (presumably from the cluster nodes).

Rerun without the Resouce pool switch:

new-vm -VM $vm -Name 'TPL_Windows_2012a' -VMHost $esx

Result: The clone begins and it creates the new VM. However, it stays on the source VMHost and does not get created on the target. So now I have the source VM and it's clone under a different name on the same host.

I don't understand why this is not cloning to the target VM host.

0 Kudos