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!!
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
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
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"?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
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
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
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
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,
Not sure what you mean.
Delete all old clones in one go every 6 days, or delete an old clone after 6 days?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
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
Then you only have to update one line (the -1 becomes -6)
$oldCloneName = "$($vmName)-clone-$((Get-Date).AddDays(-6).ToString('MMddyyyy'))"
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
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,
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
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
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
}
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
The $body variable is empty, you should assign a value to it somewhere, or leave it out.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
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
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?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Hi Luc,
its working good.
Thanks,
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
}
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
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.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference