VMware Cloud Community
lexi2code
Contributor
Contributor
Jump to solution

PowerCLI New-VM template location

I am trying to use PowerCLI to deploy a New VM.  I have templates with the same name in multiple locations in my Vcenter inventory.  When I run the $template code below manually, I get:  

Name

-------

LINUX7

$template=Get-Template | Select Name,

     @{N='Path';E={

         $templatePath = @()

         $parent = Get-View -Id $_.ExtensionData.Parent

         do{

             $templatePath += $parent.Name

             $parent = Get-View -Id $parent.Parent -ErrorAction SilentlyContinue

             } until($parent.Parent -eq $null)

             [array]::Reverse($templatePath)  

             $templatePath -join '/'  

    }} | Where-Object Name -eq $mytemplate | Where-Object Path -eq $datacenter/vm | Select Name

New-VM -Name $vmname -Template $template -Datastore $datastore -ResourcePool $cluster -OSCustomizationSpec $osCust -ErrorAction stop

But when the New-VM step runs, I get this error:  Cannot bind parameter 'Template'. Cannot convert the "\" value, of type System.Management.Automation.PSCustomObject to type VMware.VimAutomation.VICore.VI.Inventory.Template.

Any suggestions as to what I am doing wrong?

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Add the -NoRecursion switch on the Get-Template cmdlet


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

View solution in original post

4 Replies
LucD
Leadership
Leadership
Jump to solution

What you have in the $template variable is not a Template object, as expected by New-VM.
Due to the Select-Object you used, that is an object which is in fact intended for output.
You will have to provide an actual Template object


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

Reply
0 Kudos
lexi2code
Contributor
Contributor
Jump to solution

Thanks for that explanation.  I originally was using $template=Get-Folder -Name vm | Get-Template -Name $mytemplate and that works if there is only one template matching $mytemplate in Vcenter but that returns all the templates in every folder (I have 3 named Linux7 but I want to use the one in the root vm folder.  Any ideas on how I can do that?

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Add the -NoRecursion switch on the Get-Template cmdlet


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

lexi2code
Contributor
Contributor
Jump to solution

Thank you!

Reply
0 Kudos