PowerCLI

 View Only
Expand all | Collapse all

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

sureshasai

sureshasaiSep 16, 2017 02:24 PM

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

    Posted Aug 06, 2017 03:48 PM

    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!!



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

    Posted Aug 06, 2017 08:56 PM

    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"?



  • 3.  RE: Guide for the script need to take clone on every day per schedule time

    Posted Aug 07, 2017 04:02 PM

    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



  • 4.  RE: Guide for the script need to take clone on every day per schedule time
    Best Answer

    Posted Aug 07, 2017 05:12 PM

    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



  • 5.  RE: Guide for the script need to take clone on every day per schedule time

    Posted Aug 09, 2017 07:52 AM

    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,



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

    Posted Aug 09, 2017 07:55 AM

    Not sure what you mean.

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



  • 7.  RE: Guide for the script need to take clone on every day per schedule time

    Posted Aug 09, 2017 12:35 PM

    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 



  • 8.  RE: Guide for the script need to take clone on every day per schedule time

    Posted Aug 09, 2017 12:38 PM

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

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



  • 9.  RE: Guide for the script need to take clone on every day per schedule time

    Posted Aug 10, 2017 03:02 AM

    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,



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

    Posted Aug 10, 2017 05:10 AM

    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



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

    Posted Aug 12, 2017 09:11 PM

    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

    }



  • 12.  RE: Guide for the script need to take clone on every day per schedule time

    Posted Aug 12, 2017 09:51 PM

    Hi Luc,

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

    kindly verify the below script which ran.

    $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



  • 13.  RE: Guide for the script need to take clone on every day per schedule time

    Posted Aug 12, 2017 10:25 PM

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



  • 14.  RE: Guide for the script need to take clone on every day per schedule time

    Posted Aug 13, 2017 12:18 AM

    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



  • 15.  RE: Guide for the script need to take clone on every day per schedule time

    Posted Aug 13, 2017 06:53 AM

    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?



  • 16.  RE: Guide for the script need to take clone on every day per schedule time

    Posted Aug 13, 2017 06:46 PM

    Hi Luc,

    Attached the screenshot for what i did in task scheduler.



  • 17.  RE: Guide for the script need to take clone on every day per schedule time

    Posted Feb 21, 2024 08:15 PM

    LucD,

    I was looking through this thread and am trying to figure out how to clone a VM on a monthly basis. We want to clone the template shortly after it has received Windows updates, then store it in the content library to distribute to our other vCenters. I am doing this in Powershell/PowerCLI and will schedule a task to run on a VM every month

    1: I have connected to the vCenter and ran a simple get-vm on the template VM

    2: Now I want to clone it

    3: Modify the notes with the date it was cloned

    4: I also want to make sure CPU and Memory hot add are enabled

    Any help is appreciated. Thanks



  • 18.  RE: Guide for the script need to take clone on every day per schedule time

    Posted Feb 21, 2024 08:28 PM

    Are you talking about a VM or a Template?

    Point 1 and 2 can be done through the New-Template cmdlet with the Template parameter.

    For 3 and 4 you will have to convert, temporarily, the Template to a VM, change the required settings, and convert it back to a Template.



  • 19.  RE: Guide for the script need to take clone on every day per schedule time

    Posted Feb 21, 2024 08:29 PM

    So I want to take a VM we have running and clone it to a template so its up to date in patching



  • 20.  RE: Guide for the script need to take clone on every day per schedule time

    Posted Feb 21, 2024 08:32 PM

    Then you first clone the VM, change the setting on the clone, and then convert the new VM to a Template.



  • 21.  RE: Guide for the script need to take clone on every day per schedule time

    Posted Feb 28, 2024 02:06 PM

    Morning, 

    I got my script working and cloning to a new template. I currently have the new template I want to upload to our content library and I am unsure how to do so. Could you please guide me of which cmdlet would be best.

    Thanks!



  • 22.  RE: Guide for the script need to take clone on every day per schedule time

    Posted Feb 28, 2024 02:23 PM

    Use the New-ContentLibraryItem cmdlet with the VMTemplate switch.



  • 23.  RE: Guide for the script need to take clone on every day per schedule time

    Posted Feb 28, 2024 02:47 PM

    I got it working mostly, but is there a way to replace an existing library item and replace it? I could just create new ones every time, but there would be no cleanup, so saving over is preferred.



  • 24.  RE: Guide for the script need to take clone on every day per schedule time

    Posted Feb 28, 2024 03:21 PM

    Afaik, there is no "replace" cmdlet, only the Remove-ContentLibraryItem cmdlet.



  • 25.  RE: Guide for the script need to take clone on every day per schedule time

    Posted Sep 16, 2017 02:24 PM

    Hi Luc,

    its working good.
    Thanks,



  • 26.  RE: Guide for the script need to take clone on every day per schedule time

    Posted Dec 09, 2022 07:19 AM

    I have a script Remove VM before Task Clone VM running. But i don't know create Task Scheduler on vCenter.

    Can you help me, please!!!

    Set-PowerCLIConfiguration -Scope Session -InvalidCertificateAction Prompt -Confirm:$false -DefaultVIServerMode Multiple

    Connect-VIServer -Server vcenter.local -User administrator@vcenter.local -Password ****

    $srcVMName = 'T_DC'

    $vmName_old = "$($srcVMName)-clone"

    $VM_RM = Get-VM -Name $vmName_old

    if ($VM_RM) {
    if ($VM_RM.PowerState -eq "PoweredOn") {Stop-VM $VM_RM -Confirm:$false}
    Remove-VM -VM $VM_RM -DeletePermanently -Confirm:$false -RunAsync
    }