VMware Cloud Community
vickyshiv123
Enthusiast
Enthusiast

Copy VM template from vCenter server to another using vSphere powercli

Hi Everyone,

I am in the process of copying the VM template from vCenter server to another using vSphere powercli by following below steps

  •   Connect to the source vCenter server
  • Get the OVF tool from the VMware website and Install
  • Export the template VM  as a ova using OVF tool
  • Disconnect from the source vCenter server
  • Connect to the destination vCenter server
  • Create a template place folder on the destination vCenter server
  • Import the ova as a VM from source vCenter server to the destination vCenter server
  • Convert VM as a template and stored it in template place folder

We have used  the script (.ps1) as follows

# vm2ova

function Export-VM

param

 

[parameter(Mandatory=$true,ValueFromPipeline=$true)] $vm,

[parameter(Mandatory=$true)][String] $destination

$vmd = $($vm.name)

$moref = $vm.extensiondata.moref.value

$session = Get-View -Id SessionManager

$ticket = $session.AcquireCloneTicket()

& $ovftool "--I:sourceSessionTicket=$($ticket)" "vi://$($defaultviserver.name)?moref=vim.VirtualMachine:$($moref)" "$($destination)$($vmd.substring(0,$vmd.Length-5)).ova"

write-host ' '

$ovftoolpaths = ("C:\Program Files\VMware\VMware OVF Tool\ovftool.exe")

$ovftool = ' '

foreach ($ovftoolpath in $ovftoolpaths)

 

if(test-path $ovftoolpath)

 

$ovftool = $ovftoolpath

if (!$ovftool)

 

write-host -ForegroundColor red "ERROR: OVFtool not found in it's standard path."

 

else

 

cls

cls;

write-host "SOURCE ViServer:" -foregroundcolor yellow

$SourceViServer ="Enter the source vCenter server "

$sdstore = " Enter the source datastore"

write-host "DESTINATION ViServer:" -foregroundcolor yellow

$DestinationViServer = read-host " Enter Destination vCenter Server"

$ddstore = read-host " Enter Datastore for Template Destination"

$ddatacenter = read-host " Enter Destination Data Center"

$dhost = read-host " Enter Destination VM Host"

write-host " Network Destination Assignment"

$netOVFname = read-host " Enter OVF Name"

$netTargetName = read-host " Enter Target Name"

write-host 'Connecting .... > to $SourceViServer' -foregroundcolor yellow

connect-viserver $SourceViServer

write-host 'Connecting .... > to $DestinationViServer' -foregroundcolor yellow

connect-viserver $DestinationViServer

write-host 'Creating Temp folder for Cloned Template -->' -foregroundcolor blue

New-Folder 'temp-ovftool' -Server $SourceViServer -Location '_Templates-Source'

write-host 'Creating list of Templates -->' -foregroundcolor blue

(get-template -Server $SourceViServer -Location '_Templates-Source').name > .\template2vmlist.txt

write-host 'Reading from List of Templates -->' -foregroundcolor blue

$template = gc .\template2vmlist.txt

$template | %

 

disconnect-viserver $DestinationViServer -confirm:$false

connect-viserver $SourceViServer

write-host 'Cloning Template --> ' $_ -foregroundcolor blue;

New-Template -template "$_" -Name "$_-copy" -Location "temp-ovftool" -datastore $sdstore;

write-host 'Converting Template to VM --> ' $_ -foregroundcolor blue

Set-Template -Server $SourceViServer -Template "$_-copy" -ToVM -confirm:$false;

write-host 'Exporting to OVA--> ' $_ -foregroundcolor blue

get-vm -Server $SourceViServer "$_-copy" | Export-VM -destination .\

write-host 'Deleting VM from Cloned Template --> ' $_ -foregroundcolor blue

Remove-VM -Server $SourceViServer "$_-copy" -DeletePermanently -confirm:$false

disconnect-viserver $SourceViServer -confirm:$false

connect-viserver $DestinationViServer

write-host 'Deploying OVA --> ' "$_" -foregroundcolor blue

$source = $(".\$_"+".ova");write-host $source

$session = Get-View -Server $DestinationViServer -Id SessionManager

$ticket = $session.AcquireCloneTicket()

& $ovftool "--I:targetSessionTicket=$ticket" "-vf=_Templates-Destination" "-ds=$ddstore" "--net:$netOVFname=$netTargetName" "$source" "vi://$($DestinationViServer)/$($ddatacenter)?dns=$dhost"

write-host ' '

write-host 'Deleting OVA from .\ --> ' $_ -foregroundcolor blue

del $(".\$_"+".ova")

write-host 'Converting VM to Template --> ' $_ -foregroundcolor blue

set-vm -vm $_ -Server $DestinationViServer -ToTemplate -name "$_" -confirm:$false

 

connect-viserver $SourceViServer

write-host 'Deleting Temp Folder for Cloned Template -->' -foregroundcolor blue

Remove-Folder 'temp-ovftool' -server $SourceViServer -confirm:$false

write-host 'Disconnecting .... > to server :' $SourceViServer -foregroundcolor yellow

disconnect-viserver $SourceViServer -confirm:$false

write-host 'Disconnecting .... > to server :' $DestinationViServer -foregroundcolor yellow

disconnect-viserver $DestinationViServer -confirm:$false

write-host "Completed Successfully"

write-host "`n"

write-host 'The following Templates have been copied:'

write-host "From SOURCE ViServer:" -foregroundcolor yellow

write-host (" $SourceViServer").ToUpper() -foregroundcolor green

write-host "To DESTINATION ViServer:" -foregroundcolor yellow

write-host (" $DestinationViServer").ToUpper() -foregroundcolor green

$template = gc .\template2vmlist.txt;

$counter = 0;

$template | % {

$counter++;

write-host $counter'. ' -NoNewline -foregroundcolor white; write-host $_ -foregroundcolor blue

}

I am stuck in the below portion

disconnect-viserver $DestinationViServer -confirm:$false

connect-viserver $SourceViServer

write-host 'Cloning Template --> ' $_ -foregroundcolor blue;

New-Template -template "$_" -Name "$_-copy" -Location "temp-ovftool" -datastore $sdstore;

write-host 'Converting Template to VM --> ' $_ -foregroundcolor blue

Set-Template -Server $SourceViServer -Template "$_-copy" -ToVM -confirm:$false;

write-host 'Exporting to OVA--> ' $_ -foregroundcolor blue

get-vm -Server $SourceViServer "$_-copy" | Export-VM -destination .\

write-host 'Deleting VM from Cloned Template --> ' $_ -foregroundcolor blue

Remove-VM -Server $SourceViServer "$_-copy" -DeletePermanently -confirm:$false

disconnect-viserver $SourceViServer -confirm:$false

Please advise

Thanks

Vick

Tags (1)
Reply
0 Kudos
3 Replies
LucD
Leadership
Leadership

Stuck in what sense?

Not doing what you want, errors....?


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

Reply
0 Kudos
vickyshiv123
Enthusiast
Enthusiast

Hi,

It displays below errors,

Creating list of Templates -->
Reading from List of Templates -->

cmdlet ForEach-Object at command pipeline position 1
Supply values for the following parameters:
Process[0]:

it stops here and for your kind information it is able to create the list of templates as .txt file , could able to read the template

Please advise or is this any other way to do it

Thanks

Vick

Reply
0 Kudos
LucD
Leadership
Leadership

I suspect the problem is in this line ($template | %), there is no curly brace to indicate the start of the process block that goes with the ForEach (%)


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

Reply
0 Kudos