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?