Hi,
I built a script to clone VMs on a regular basis around a method posted by LucD and Simon Long here or on their blogs.
Here is the part that does the actual work:
$VM = Get-VM $Machine.MasterVM
# Send Start Email
.\startmail.ps1
# Create new snapshot for clone
$CloneSnap = $VM | New-Snapshot -Name "Test Snapshot" -Quiesce
# Get managed object view
$VmView = $VM | Get-View
# Get folder managed object reference
$CloneFolder = $VmView.parent
# Build clone specification
$CloneSpec = new-object Vmware.Vim.VirtualMachineCloneSpec
$CloneSpec.Snapshot = $VmView.Snapshot.CurrentSnapshot
# Make linked disk specification
$CloneSpec.Location = new-object Vmware.Vim.VirtualMachineRelocateSpec
$CloneSpec.Location.Datastore = (Get-Datastore -Name $Machine.BackupDS).Extensiondata.MoRef
$CloneSpec.Location.Host = ( get-vmhost -Name $Machine.BackupHost).Extensiondata.MoRef
$CloneSpec.Location.Transform =[Vmware.Vim.VirtualMachineRelocateTransformation]::sparse
$CloneName = "$vm-BU-$date"
# Create clone
$vmView.CloneVM( $cloneFolder, $cloneName, $cloneSpec )
# Write newly created VM to stdout as confirmation
Get-VM $CloneName
# Remove Snapshot created for clone
$Snap=Get-Snapshot -VM (Get-VM -Name $Machine.MasterVM) -Name $CloneSnap | Remove-Snapshot -confirm:$False
#Send Complete Email
.\endmail.ps1
I want to use it in an environment consisting of 3 ESXi 4.0 servers with a local datastore each. It works well in my test environment, but not in the productive one.
There I get this error:
Exception calling "CloneVM" with "3" argument(s): "A specified parameter was not correct.
spec.location.pool"
At C:\script\vm-backup\backup.ps1:98 char:18
+ $vmView.CloneVM <<<< ( $cloneFolder, $cloneName, $cloneSpec )
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : DotNetMethodException
In a comment to his blog Simon refers to an error regarding the Datastore, but this seems fine here.
Comparing the values of the Specs to the ones in my test bed it all looks good, too.
Can any of you tell me where to look for the problem??
Thanks in advance
Christian
That is correct, 1 default resource pool per server.
You can do
Get-ResourcePool -Name Resources -Location (Get-VMHost -Name MyEsx)
to get the default pool for a specific ESX server
Cab you add a line in your original script
$CloneSpec.Location.Pool = (Get-ResourcePool -Name Resources -Location (Get-VMHost -Name MyEsx)).Extensiondata.MoRef
That should be the correct MoRef
The message seems to indicate that the target VM already partially exists.
Remove it before running your script again.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Are you sure that
get-vmhost -Name $Machine.BackupHost
returns an ESX host ?
The error message says that you need to specify a pool for the clone, but that is only required afaik when the host is not given.
See the VirtualMachineRelocateSpec object for more info.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
To me the output seems fine.
The value of $CloneSpec.Location.Host is "host-15".
There are no resource pools used up to now.
There is always a resourcepool, the hidden one
Do a
Get-ResourcePool Resources
You could try setting the Location.Pool property to this hidden pool
$CloneSpec.Location.Pool = (Get-ResourcePool Resources).Extensiondata.MoRef
See if that makes a difference
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
get-resoucepool Resources
returns:
Name Id
---- --
Resources ResourcePool-resgroup-14
Resources ResourcePool-resgroup-163
Resources ResourcePool-resgroup-9
One pool for each server I guess.
In the scriot $CloneSpec.Location.Pool stays empty.
I tried connecting to the host as well (in addition to vcenter) to use the -server argument, but then "ha-root-pool" is returned and I get this error:
Exception calling "CloneVM" with "3" argument(s): "The object has already been deleted or
has not been completely created"
At C:\script\vm-backup\backup.ps1:103 char:18
+ $vmView.CloneVM <<<< ( $cloneFolder, $cloneName, $cloneSpec )
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : DotNetMethodException
That is correct, 1 default resource pool per server.
You can do
Get-ResourcePool -Name Resources -Location (Get-VMHost -Name MyEsx)
to get the default pool for a specific ESX server
Cab you add a line in your original script
$CloneSpec.Location.Pool = (Get-ResourcePool -Name Resources -Location (Get-VMHost -Name MyEsx)).Extensiondata.MoRef
That should be the correct MoRef
The message seems to indicate that the target VM already partially exists.
Remove it before running your script again.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Yes, this seems to have done it.
Interesting that this is needed in some environments, but not in others.
Thanks a lot for your help, Luc