SrSEshouldbeAE
Contributor
Contributor

Having issues with PowerCLI script using New-VM to create VM in one vcenter using template from another vcenter.

Having issues with PowerCLI script using New-VM to create VM in one vcenter using template from another vcenter.

This works fine in GUI but I need to script this out.  I get error

"New-VM : 7/21/2020 9:40:16 AM    New-VM        The object 'vim.VirtualMachine:vm-511408' has already been deleted or has not been completely created      At line:1 char:1 + New-VM -Server vcsrvprodlv -Template $myTemplate -Name $templatename  ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~     + CategoryInfo          : NotSpecified: (:) [New-VM], VimException     + FullyQualifiedErrorId : Core_BaseCmdlet_UnknownError,VMware.VimAutomation.ViCore.Cmdlets.Commands.NewVM"

$creds = New-Object System.Management.Automation.PSCredential("username",$(ConvertTo-SecureString "Password" -AsPlainText -Force))

Get-Module VMware* | Import-Module

#Connect to Vsphere

$Destvcs = "Server2.domain"

$Sourvcs = "Server1.domain"

$DestvcsConn = Connect-VIServer -Server $Destvcs -Credential $creds

$SourvcsConn = Connect-VIServer -Server $Sourvcs -Credential $creds

#Template Name

$templatename = "Server 2016 Core"

#Datacenter2a

$LVInewdatacenter = Get-Datacenter -Server $DestvcsConn -Name "Datacenter2a"

$LVIdatastore = Get-Datastore -Server $DestvcsConn -Name "Datastore2a"

$LVIvmhost = Get-VMHost -Server $DestvcsConn -Name "vmHost2a"

#Datacenter2b

$LVnewdatacenter = Get-Datacenter -Server $DestvcsConn -Name "Datacenter2b"

$LVdatastore = Get-Datastore -Server $DestvcsConn -Name "Datastore2b"

$LVvmhost = Get-VMHost -Server $DestvcsConn -Name "vmHost2b"

$myTemplate = Get-Template -Server $SourvcsConn -Name $templatename -Location (Get-Datacenter -server $SourvcsConn -Name "Datacenter1")

$LVInewlocation = Get-Folder -server $DestvcsConn -Name "Templates" -Location (Get-Datacenter -server $DestvcsConn -Name $LVInewdatacenter)

$LVnewlocation = Get-Folder -Server $DestvcsConn -Name "Templates" -Location (Get-Datacenter -server $DestvcsConn -Name $lvnewdatacenter)

$LVImyDs = Get-Datastore -Server $DestvcsConn -Name $LVIdatastore

$LVmyDs = Get-Datastore -Server $DestvcsConn -Name $LVdatastore

Remove-Template -Server $Destvcs (Get-Template -server $Destvcs -Name $templatename -Location (Get-Datacenter -Server $Destvcs -Name $LVInewdatacenter)) -Confirm:$false

Remove-Template -Server $Destvcs (Get-Template -Server $Destvcs -Name $templatename -Location (Get-Datacenter -Server $Destvcs -Name $LVnewdatacenter)) -Confirm:$false

New-VM -Server $DestvcsConn -Template $myTemplate -Name $templatename -Datastore $LVImyDs -Location $LVInewlocation -VMHost $LVIvmhost | Set-VM -Server $DestvcsConn -ToTemplate -Name $templatename -confirm:$false

New-VM -Server $DestvcsConn -Template $myTemplate -Name $templatename -Datastore $LVmyDs -Location $LVnewlocation -VMHost $LVvmhost | Set-VM -Server $DestvcsConn -ToTemplate -Name $templatename -confirm:$false

disconnect-viserver * -Confirm:$false

0 Kudos
1 Reply
LucD
Leadership
Leadership

I think you might be hitting a known issue.
In fact, there is the PowerCLI Idea Cross vCenter Cloning for a similar feature in the New-VM cmdlet to allow cloning accross vCenters.


Till there is an official solution I suggest you use William's Cross vCenter Clone with vSphere 6.0+  as an example.

His xNew-VM.ps1 script clones from a VM, but it shouldn't be too hard to use the same concept to create a new VM from a Template.


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

0 Kudos