1 2 Previous Next 16 Replies Latest reply on Sep 16, 2017 7:24 AM by sureshasai

    Guide for the script need to take clone on every day per schedule time

    sureshasai Novice

      Hi All,

      Guide for me the script need to take the virtual machine clone on every day per schedule time.
      Conditions:

      1) Its need to be delete the previous day backup and name format it should be standard so that it could be auto delete the last day clone. 

      2) Also the clone will be choose data store automatically which having enough disk space.

      3) After completing the clone auto generated email needs to be sent. if the clone is not success failure message also need to forward.

      4) we need to exclude some data store with condition filter.

       

      Thank in advance!!

        • 1. Re: Guide for the script need to take clone on every day per schedule time
          LucD Guru
          Community WarriorsUser ModeratorsvExpert

          Some questions

          1) Can you tell us a bit more on the naming standard?

          2) Are you using DatastoreClusters for the clones?

          4) Can you be a bit more specific on what you mean by "condition filter"?

          • 2. Re: Guide for the script need to take clone on every day per schedule time
            sureshasai Novice

            Hi,

            1) name standard is nothing but like

            vm-test-clone-08072017 numeric is date it's need to be modify daily

            2) am using single cluster data store

            4) you just ignore the condition filter I just mean choosing the data store using enough free space of the cloned machine

            • 3. Re: Guide for the script need to take clone on every day per schedule time
              LucD Guru
              Community WarriorsvExpertUser Moderators

              You could do something like this.

              It will remove the clone form the day before, and create a new clone.

              For the scheduling you could use the Windows Task Scheduler to run the script every day.

              See for example Alan's RUNNING A POWERCLI SCHEDULED TASK

               

               

              $vmName = 'vm-test'

               

              $cloneName = "$($vmName)-clone-$((Get-Date).ToString('MMddyyyy'))"

              $oldCloneName = "$($vmName)-clone-$((Get-Date).AddDays(-1).ToString('MMddyyyy'))"

               

              $ds = Get-Datastore | where{$_.Type -eq 'VMFS' -and $_.ExtensionData.Summary.MultipleHostAccess} |

                  Sort-Object -Property FreeSpaceGB -Descending | select -First 1

              $esx = Get-Cluster -VM $vmName | Get-VMHost | Get-Random

               

              Get-VM -Name $oldCloneName -ErrorAction SilentlyContinue |

                  Remove-VM -DeletePermanently:$true -Confirm:$false

              New-VM -VM $vmName -Name $cloneName -Datastore $ds -VMHost $esx

               

              1 person found this helpful
              • 4. Re: Guide for the script need to take clone on every day per schedule time
                sureshasai Novice

                Hi ,

                 

                Its working fine.
                I let me know how could i change deleting command once in 6days (I could like to delete the old clone once in 6days) Also in that i have to delete the clone which i have taken first(old one to identify the old machine using the clone name date)

                Thanks,

                • 5. Re: Guide for the script need to take clone on every day per schedule time
                  LucD Guru
                  User ModeratorsvExpertCommunity Warriors

                  Not sure what you mean.

                  Delete all old clones in one go every 6 days, or delete an old clone after 6 days?

                  • 6. Re: Guide for the script need to take clone on every day per schedule time
                    sureshasai Novice

                    Hi ,

                     

                    If I take clone for  today mean the clone name it should be like vm-test-08092017.

                    I would like to delete older sixth day clone that mean vm-test-08032017. It needs to delete only the six days older clone not for not for lesser then 6age.

                    Thank you 

                    • 7. Re: Guide for the script need to take clone on every day per schedule time
                      LucD Guru
                      vExpertCommunity WarriorsUser Moderators

                      Then you only have to update one line (the -1 becomes -6)

                       

                      $oldCloneName = "$($vmName)-clone-$((Get-Date).AddDays(-6).ToString('MMddyyyy'))"

                       

                      1 person found this helpful
                      • 8. Re: Guide for the script need to take clone on every day per schedule time
                        sureshasai Novice

                        Hi ,

                         

                        Yes, I tried my self it working

                         

                        Is possible to send email after the clone successfully complete.(Like clone was successfully completed for the server vm-test)
                        If its failed any unexpected cause (Like clone was failed due to some issue) i would like to make like this after the clone success\unsuccess

                        can you guide me.

                        Thanks,

                        • 9. Re: Guide for the script need to take clone on every day per schedule time
                          LucD Guru
                          Community WarriorsUser ModeratorsvExpert

                          You could do something like this

                           

                          $vmName = 'vm-test'

                           

                          $cloneName = "$($vmName)-clone-$((Get-Date).ToString('MMddyyyy'))"

                          $oldCloneName = "$($vmName)-clone-$((Get-Date).AddDays(-1).ToString('MMddyyyy'))"

                           

                          $ds = Get-Datastore | where{$_.Type -eq 'VMFS' -and $_.ExtensionData.Summary.MultipleHostAccess} |

                              Sort-Object -Property FreeSpaceGB -Descending | select -First 1

                          $esx = Get-Cluster -VM $vmName | Get-VMHost | Get-Random

                           

                          $start = Get-Date

                          Get-VM -Name $oldCloneName -ErrorAction SilentlyContinue |

                              Remove-VM -DeletePermanently:$true -Confirm:$false

                          $vm = New-VM -VM $vmName -Name $cloneName -Datastore $ds -VMHost $esx

                           

                          $cloneEvent = Get-VIEvent -Entity $vm -Start $start |

                            where{$_ -is [VMware.Vim.VmClonedEvent] -and $_.Vm.Vm.Value -eq $vm.ExtensionData.MoRef.Value}

                           

                          $sMail = @{

                            From = 'me@domain'

                            To = 'you@domain'

                            Subject = "Clone result for VM $($vmName)"

                            Body = "VM $($vmName) was cloned to $($cloneName) with message $($cloneEvent.FullformattedMessage)"

                            SmtpServer = 'mail.domain'

                          }

                          Send-MailMessage @sMail

                           

                          • 10. Re: Guide for the script need to take clone on every day per schedule time
                            sureshasai Novice

                            Hi Luc,

                             

                            Clone is taken successfully without any issue.
                            usually we keep the clone name should be like "mailserver_backup_08122017"(date need to change automatically); mailserver_backup_

                            Server name

                             

                            $vmName = 'vm-p-mail'

                            $cloneName = "$($vmName)mailserver_backup_$((Get-Date).ToString('MMddyyyy'))"

                            $oldCloneName = "$($vmName)mailserver_backup_$((Get-Date).AddDays(-1).ToString('MMddyyyy'))"

                             

                             

                            The edit is done above is good or bad.

                            kindly let me know. I would like to save my credentila in script itself

                             

                            $cred = Get-Credential -Message "Credentials for mail server"

                            $sMail = @{

                            To = 'email@test.com'

                            From = 'email@test.com'

                            Subject = 'clone Report'

                            BodyAsHtml = $true

                            Body = $body | Out-String

                            SmtpServer = 'smtp.1and1.com'

                            UseSsl = $true

                            Credential = $cred

                            }

                            • 11. Re: Guide for the script need to take clone on every day per schedule time
                              sureshasai Novice

                              Hi Luc,

                              After the authentication email could not received getting an below error.

                              kindly verify the below script which ran.

                               

                              e.JPG

                               

                              $vmName = 'MC-P-MailServer'

                               

                              $cloneName = "$($vmname)mailserver_Backup_$((Get-Date).ToString('MMddyyyy'))"

                              $oldCloneName = "$($vmname)mailserver_Backup_$((Get-Date).AddDays(-7).ToString('MMddyyyy'))"

                               

                              $ds = Get-Datastore | where{$_.Type -eq 'VMFS' -and $_.ExtensionData.Summary.MultipleHostAccess} |

                                  Sort-Object -Property FreeSpaceGB -Descending | select -First 1

                              $esx = Get-Cluster -VM $vmName | Get-VMHost | Get-Random

                               

                              $start = Get-Date

                              Get-VM -Name $oldCloneName -ErrorAction SilentlyContinue |

                                  Remove-VM -DeletePermanently:$true -Confirm:$false

                              $vm = New-VM -VM $vmName -Name $cloneName -Datastore $ds -VMHost $esx

                               

                              $cloneEvent = Get-VIEvent -Entity $vm -Start $start |

                                where{$_ -is [VMware.Vim.VmClonedEvent] -and $_.Vm.Vm.Value -eq $vm.ExtensionData.MoRef.Value}

                               

                              $cred = Get-Credential -Message "Credentials for mail server"

                              $sMail = @{

                                  To = 'test@test.com'

                                  From = 'test@msservicesinc.com'

                                  Subject = 'Clone Report'

                                  BodyAsHtml = $true

                                  Body = $body | Out-String

                                  SmtpServer = 'smtp.1and1.com'

                                  UseSsl = $true

                                  Credential = $cred

                              }

                               

                              Send-MailMessage @sMail

                              • 12. Re: Guide for the script need to take clone on every day per schedule time
                                LucD Guru
                                User ModeratorsvExpertCommunity Warriors

                                The $body variable is empty, you should assign a value to it somewhere, or leave it out.

                                1 person found this helpful
                                • 13. Re: Guide for the script need to take clone on every day per schedule time
                                  sureshasai Novice

                                  Hi Luc,

                                   

                                  1) I have tried to save the password in script itself password saved in executed path but its not running properly

                                  2)  Also i have tried using task scheduler to schedule the task help of following link (Schedule PowerCLI script in Windows task scheduler » VCDX56)

                                  Its not working properly

                                  3) I have doubt in that action tab which path should i choose in start program path and argument path.

                                   

                                  I have attached modified script. Can you please correct me where i did mistake.

                                   

                                  Connect-VIServer xx.xx.xx.xx

                                  $vmName = 'MC-D-Mifos01'

                                   

                                  $cloneName = "$($)Varnikmail_Backup_$((Get-Date).ToString('MMddyyyy'))"

                                  $oldCloneName = "$($)Varnikmail_Backup_$((Get-Date).AddDays(-7).ToString('MMddyyyy'))"

                                   

                                  $ds = Get-Datastore | where{$_.Type -eq 'VMFS' -and $_.ExtensionData.Summary.MultipleHostAccess} |

                                      Sort-Object -Property FreeSpaceGB -Descending | select -First 1

                                  $esx = Get-Cluster -VM $vmName | Get-VMHost | Get-Random

                                   

                                  $start = Get-Date

                                  Get-VM -Name $oldCloneName -ErrorAction SilentlyContinue |

                                      Remove-VM -DeletePermanently:$true -Confirm:$false

                                  $vm = New-VM -VM $vmName -Name $cloneName -Datastore $ds -VMHost $esx

                                   

                                  $cloneEvent = Get-VIEvent -Entity $vm -Start $start |

                                    where{$_ -is [VMware.Vim.VmClonedEvent] -and $_.Vm.Vm.Value -eq $vm.ExtensionData.MoRef.Value}

                                   

                                  $cred = Get-Credential -Message "Credentials for mail server"

                                  $pwd = Get-Content c:\vSphere\scripts\powerclicred | ConvertTo-SecureString

                                  $cred = New-Object System.Management.Automation.PsCredential “mssi\sasai“, $pwd

                                  $sMail = @{

                                      To = 'asuresh@msservicesinc.com'

                                      From = 'asuresh@msservicesinc.com''nsiva@msservicesinc.com'

                                      Subject = 'Clone Report'

                                      BodyAsHtml = $true

                                      Body = $Clone Report | Out-String

                                      SmtpServer = 'smtp.1and1.com'

                                      UseSsl = $true

                                      Credential = $cred

                                  }

                                   

                                   

                                  Send-MailMessage @sMail

                                  • 14. Re: Guide for the script need to take clone on every day per schedule time
                                    LucD Guru
                                    User ModeratorsCommunity WarriorsvExpert

                                    You can't prompt for input in a scheduled task (the Get-Credential cmdlet).

                                    Also, can you share some screenshots of the scheduled task you created?

                                    1 2 Previous Next