PowerCLI

 View Only
  • 1.  Script Powershell to clone a VM

    Posted Oct 22, 2009 04:14 PM

    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 :/ )



  • 2.  RE: Script Powershell to clone a VM
    Best Answer

    Posted Oct 22, 2009 05:37 PM

    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.



  • 3.  RE: Script Powershell to clone a VM

    Posted Oct 23, 2009 06:59 AM

    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)

    -




  • 4.  RE: Script Powershell to clone a VM

    Posted Oct 23, 2009 08:53 AM

    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)

    -




  • 5.  RE: Script Powershell to clone a VM

    Posted Oct 23, 2009 08:57 AM

    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).



  • 6.  RE: Script Powershell to clone a VM

    Posted Oct 23, 2009 09:14 AM

    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



  • 7.  RE: Script Powershell to clone a VM

    Posted Oct 23, 2009 12:25 PM

    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)

    -




  • 8.  RE: Script Powershell to clone a VM

    Posted Nov 12, 2009 07:43 PM

    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.