VMware Cloud Community
Sunshine_S
Enthusiast
Enthusiast
Jump to solution

Create templates from VMs (script). What am I doing wrong?

There are multiple VMs in one folder, that I want to clone as templates to a different folder. The name of the new template should  same as the name of the VM. I have written this script but it gives an error.

Script:

$esx = Get-VMHost -Name Our_ESX_Name
$ds = Get-Datastore -Name Our_DS_Name
$folderName = 'Destination_Folder_Name'
$folder = Get-Folder -Name $folderName
$srcFolderName = 'Source_Folder_Name'
$srcFolder = Get-Folder -Name $srcFolderName

Get-VM -Location $srcFolder |

ForEach-Object -Process {

New-Template -Name "$($_.Name)" -VM $_ -Location $folder -VMHost $esx -Datastore $ds

}

 

 

Error:

New-Template : Parameter set cannot be resolved using the specified named parameters.
At C:\path\new-templates.ps1:10 char:4
+ New-Template -Name "$($_.Name)" -VM $_ -Location $folder -VMHost $ ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [New-Template], ParameterBindingException
+ FullyQualifiedErrorId : AmbiguousParameterSet,VMware.VimAutomation.ViCore.Cmdlets.Commands.NewTemplate

 

Please help me to identify the mistake in the script. Is there any other way to achieve what I am trying to do?

Labels (2)
Tags (1)
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

You are mixing parameters from different parametersets for the New-Template cmdlet.

The VM parameter is from the "default" parameterset, while the VMHost parameter is from the "clone" parameterset.
You can't mix key parameters from different parametersets.


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

View solution in original post

3 Replies
LucD
Leadership
Leadership
Jump to solution

You are mixing parameters from different parametersets for the New-Template cmdlet.

The VM parameter is from the "default" parameterset, while the VMHost parameter is from the "clone" parameterset.
You can't mix key parameters from different parametersets.


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

Sunshine_S
Enthusiast
Enthusiast
Jump to solution

Thanks a lot LucD,

I removed the VMHost parameter and the earlier error is no longer seen.

However now I am getting a different error:


A specified parameter was not correct: spec.location.host ThrowableProxy.cause The input arguments had entities that did not belong to the same datacenter.

 

It seems that I cannot use this script to clone VMs to templates across data centers. 

I have 15 VMs on Data center A, that I need to clone to templates to Data center B.

Is there any way to do this using PowerCLI?

0 Kudos
LucD
Leadership
Leadership
Jump to solution

I would vMotion these VMs to the target datacenter if possible.

Otherwise create a template in the original datacenter, convert the template to a VM, and the VMotion the VM to the other datacenter


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

0 Kudos