11 Replies Latest reply on Feb 6, 2019 2:46 PM by LucD

    Clone a VM with a script

    marco2601 Novice

      Hello friends

       

       

      You could help me with example script to clone a VM on a certain date and that it runs automatically on an ESXI host

        • 1. Re: Clone a VM with a script
          LucD Guru
          User ModeratorsvExpertCommunity Warriors

          Do you have anymore specifics?
          Schedule? How, where?
          Clone? From an existing VM?

           

          What do you already have?

          And where did you get stuck?

          • 2. Re: Clone a VM with a script
            marco2601 Novice

            Well I have a cluster in vCenter and I want to clone an existing Linux VM.

             

            Cloning, for example, a Master VM every 3 days but I do not know how to clone on certain dates and the previous ones are eliminated

            • 3. Re: Clone a VM with a script
              LucD Guru
              Community WarriorsUser ModeratorsvExpert

              But where do you intend to run the schedule?

              In the vCenter Scheduler or on a Windows box with the Windows Task Scheduler or on a Linux box with a crontab?

              • 4. Re: Clone a VM with a script
                ryanrpatel Enthusiast
                vExpert

                Are you looking for something like this?

                 

                New-VM -Name $NewVMName -VM $SourceVM -VMHost $TargetESXiHost -Datastore $TargetDatastore -RunAsync

                • 5. Re: Clone a VM with a script
                  marco2601 Novice

                  I have the following initial script:

                   

                  Where you connect to vCenter, clone the machine, disconnect the network adapter from the source VM, and then start the cloned VM.

                   

                  Now what stuck me is that I'm not sure how to do so that cloning is generated every week but does not eliminate the already cloned machine that is generated as a new clone

                   

                  ##Clonar VM

                  ##Variables

                  $vcenter_server ="172.16.6.10"

                  $vcenter_user ="administrator@taurus.local"

                  $vcenter_pwd ="T4urus.2018.v1Center"

                  $Hostesxi = "172.16.1.51"

                  $VMsource = "Ubuntu"

                  $VMclone = "Ubuntu-clone"

                   

                   

                  ##Connect to vCenter

                  connect-viserver -server $vcenter_server -User $vcenter_user -Password $vcenter_pwd

                   

                  ##Clone VM, disco virtual tipo thick y carpeta de almacenaiento de la VM

                  New-VM -VM $VMsource -Name $VMclone -VMHost $Hostesxi -DiskStorageFormat Thick -Location "Laboratorios" -Notes "Clone creado $dateofclone by Marco Lopez"

                   

                  ##Desconect Network Adapter source VM

                  Get-VM $VMsource | Get-NetworkAdapter | Set-NetworkAdapter -connected:$false -startconnected:$false

                   

                  ##Iniciar VM clonada

                  GET-VM -Name $VMclone| Start-VM -Confirm:$False

                  • 6. Re: Clone a VM with a script
                    LucD Guru
                    vExpertCommunity WarriorsUser Moderators

                    The cloning process is not the big deal (see for example Re: Guide for the script need to take clone on every day per schedule time ), but you would first have to decide how you want to do the scheduling.
                    This has some impact on how the cloning and removal can be done.

                    When you use the vCenter scheduler, the cloning can be builtin.
                    when you use the Windows Task scheduler, you can run the PowerCLI script from a Windows box.

                    • 7. Re: Clone a VM with a script
                      ryanrpatel Enthusiast
                      vExpert

                      If you can confirm the script to work manually, I'd setup a scheduled task to run that file based on your Timeline.

                      • 8. Re: Clone a VM with a script
                        marco2601 Novice

                        I helped a lot that refedence practically the part of the cloning I have solved.

                         

                        Now if you can guide me in the following:

                         

                        The cloning was done from a source VM, what I need to do is how do I do that when the source VM does not have a connection, basically I do not fear ping for example to 172.16.6.90 that the ip in my source VM disconnect the network adapter from that VM and then turn on the last clone of said VM

                        • 9. Re: Clone a VM with a script
                          LucD Guru
                          User ModeratorsvExpertCommunity Warriors

                          If you want to delete the VM in $sourceVM, you can just do a Remove-VM.

                           

                          Get-VM -Name $sourceVM | Remove-VM -DeletePermanently -Confirm:$false

                           

                          Or do you mean something else?

                          • 10. Re: Clone a VM with a script
                            marco2601 Novice

                            I express myself badly apparently

                             

                            I understand that it would be another Script, where I do a connection check to a VM if I do not have a connection to that VM that turns on another VM

                            • 11. Re: Clone a VM with a script
                              LucD Guru
                              Community WarriorsUser ModeratorsvExpert

                              I'm afraid you lost me.