Automation

 View Only
  • 1.  PowerCLI New-VM template location

    Posted Jan 26, 2022 06:00 PM

    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?



  • 2.  RE: PowerCLI New-VM template location

    Posted Jan 26, 2022 06:41 PM

    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



  • 3.  RE: PowerCLI New-VM template location

    Posted Jan 26, 2022 08:34 PM

    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?



  • 4.  RE: PowerCLI New-VM template location
    Best Answer

    Posted Jan 26, 2022 09:10 PM

    Add the -NoRecursion switch on the Get-Template cmdlet



  • 5.  RE: PowerCLI New-VM template location

    Posted Jan 26, 2022 09:45 PM

    Thank you!