VMware Cloud Community
dcouturier
Enthusiast
Enthusiast
Jump to solution

Script Powershell to clone a VM

Hi,

I would like to perform a clone of a VM with a script powershell. I can't find a way to do this easily. I wrote a script that clon a VM1 to template then template to VM2. Is there another method ?

Thanks,

NB : Tomorrow, I'll post my script (poor script 😕 )

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

There is no direct cmdlet in the current PowerCLI build to clone from a VM.

For that you would have to use the SDK method CloneVM_Task.

There are a number of samples available in this community.


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

View solution in original post

7 Replies
LucD
Leadership
Leadership
Jump to solution

There is no direct cmdlet in the current PowerCLI build to clone from a VM.

For that you would have to use the SDK method CloneVM_Task.

There are a number of samples available in this community.


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

dcouturier
Enthusiast
Enthusiast
Jump to solution

Thanks for reply.

Here is my script. Feel free to correct me :

-


Connect-VIServer -Server localhost >$null

#Variables

$NameVM ="MachineTest"

$NameBackup="Backup_Test"

$Template ="Template01"

$Datacenter="Datacenter"

$Datastore="Backup NAS"

$ESX="192.168.9.241"

#Suppression dans l'inventaire de la dernière sauvegarde

remove-vm -VM (get-vm $NameBackup) -confirm:($false)

#Création d'un template temporaire

New-template -VM (Get-VM $NameVM) -Name $Template -Location (Get-Datacenter $Datacenter)

#Création de la machine backup à partir du template

New-VM -template (Get-template $Template) -Name $NameBackup -Datastore (Get-datastore $Datastore) -VMHost $ESX

#Suppression du template

Remove-Template (get-template $Template) -confirm:($false)

-


Reply
0 Kudos
dcouturier
Enthusiast
Enthusiast
Jump to solution

I updated my script with two method. What is the best method ?

  1. Method 1 : Deploy VM from template to backup datastore

  2. Method 2 : Convert template then move VM to backup datastor

-


Connect-VIServer -Server localhost >$null

#Variables

$NameVM ="MachineTest"

$NameBackup="Backup_Test"

$NameTemplate ="Template01"

$Datacenter="Datacenter"

$Datastore="Backup NAS"

$ESX="192.168.9.241"

$Date=get-date -uformat "%Y%m%d"

+$NameBackup=$NameBackup + $date+

#Suppression dans l'inventaire de la dernière sauvegarde

remove-vm -VM (get-vm $NameBackup) -confirm:($false)

#

################### Methode 1 - Déploiement à partir du template puis suppression du template

#Création d'un template temporaire

#New-template -VM (Get-VM $NameVM) -Name $NameTemplate -Location (Get-Datacenter $Datacenter)

#Création de la machine backup à partir du template

#New-VM -template (Get-template $NameTemplate) -Name $NameBackup -Datastore (Get-datastore $Datastore) -VMHost $ESX

#Suppression du template

#Remove-Template (get-template $Template) -DeleteFromDisk:($true) -confirm:($false)

#

################### Methode 2 - Conversion du template en machine virtuelle puis déplacement de la machine

#Création d'un template temporaire

New-template -VM (Get-VM $NameVM) -Name $NameBackup -Location (Get-Datacenter $Datacenter)

#Conversion du template temporaire en machine virtuelle

Set-template -template (Get-template $NameBackup) -ToVM

#Déplacement de la machine virtuelle sur le NAS

Move-vm -VM $NameBackup -Datastore (Get-datastore $Datastore)

-


Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Both scripts looks good.

Which one to use depends if you want to use the Template for other clones later on.

That would not be possible with the 2nd method.

Also be aware that your cloned guest will have the same SID as the original (provided you have a WIndows OS running on the guest of course).


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

Reply
0 Kudos
dcouturier
Enthusiast
Enthusiast
Jump to solution

Ok for SID but this script is for backup. A word of infrastructure in place :

One ESX 4 (x3650 M2 with 900 Gb RAID5) and a NAS (Netgear READY NAS NV+). The client want be able to run its VM on a workstation (in case of ESX crash) and run its directly from NFS. So SIDs won't be a problem in this way

I'm currently testing the time needed for method 1 and method 2 to compare. I will post the result

Reply
0 Kudos
dcouturier
Enthusiast
Enthusiast
Jump to solution

Finally, both method seems to take the same time to execute

I make some changes in script

-


Connect-VIServer -Server localhost >$null

#Variables

$NameVM ="MachineTest"

$NameBackup="BackupTest"

$NameTemplate ="Template01"

$Datacenter="Datacenter"

$Datastore="Backup NAS"

$ESX="192.168.9.241"

$Date=get-date -uformat "%Y%m%d"

#Ajout de la date au nom de la sauvegarde

+$NameBackup=$NameBackup + "_" + $date+

#

################### Methode 1 - Deploy from template ######################################

#

#Création d'un template temporaire

#New-template -VM (Get-VM $NameVM) -Name $NameTemplate -Location (Get-Datacenter $Datacenter)

#Création de la machine backup à partir du template

#New-VM -template (Get-template $NameTemplate) -Name $NameBackup -Datastore (Get-datastore $Datastore) -VMHost $ESX

#Suppression du template

#Remove-Template -template (get-template $NameTemplate) -DeleteFromDisk:($true) -confirm:($false)

###########################################################################################

#

################### Methode 2 - Convert to template and move #############################

#

#Création d'un template temporaire

New-template -VM (Get-VM $NameVM) -Name $NameBackup -Location (Get-Datacenter $Datacenter)

#Conversion du template temporaire en machine virtuelle

Set-template -template (Get-template $NameBackup) -ToVM

#Déplacement de la machine virtuelle sur le NAS

Move-vm -VM $NameBackup -Datastore (Get-datastore $Datastore)

###########################################################################################

#Suppression dans l'inventaire de la dernière sauvegarde

Remove-vm -VM (get-vm $NameBackup) -confirm:($false)

-


Reply
0 Kudos
AdamMiller
Contributor
Contributor
Jump to solution

Also be aware that your cloned guest will have the same SID as the original (provided you have a WIndows OS running on the guest of course).

This thread has helped me a ton.

However, the above quoted text from LucD....I am at a point i do want to customize my VM's during these cloning processes, however i cannot seem to figure out how to invoke the customization wizard via PowerCLI.

Any help will be appreciated.

Reply
0 Kudos