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
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
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
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!
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
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!
Feel free to ask when you get stuck
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Hi. LucD.
I'm having trouble getting you example to work. Here is what I have:
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!
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
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;
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!
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).
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!
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 AM | New-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 AM | New-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!
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
"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."
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
Yep, I'm connected to both hosts.
I hope you're able to see the screenshot. If not I'll upload, but bigger.
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
I think we are getting closer.
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
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.