I'm perhaps dreaming because I looked and I looked but could not see anything close. I'm planning to create a self-service form to request a snapshot. So far, scheduling the snapshot creation is ready. But what about scheduling a task that will remove the snapshot after X amount of time? I just made those 5 minutes apart but I need help with what I think would be the arguments for the methodAction.
The most important issue is that the RemoveSnapshot_Task method is not present on a VirtualMachine object, but on a VirtualMachineSnapshot object.
That means you will have to get the actual snapshot that you want to remove with a Scheduled Task.
That is rather hard to code (the snapshot needs to exists to get the MoRef of the snapshot), and definitely impossible to be a recurring Scheduled Task.
What I normally do is use the RemoveAllSnapshots_Task, which is a method that exists on a VirtualMachine object.
That method requires one parameter, a Boolean to indicate if Consolidate needs to be run after the snapshots are removed.
$spec = New-Object VMware.Vim.ScheduledTaskSpec
$spec.Scheduler = New-Object VMware.Vim.OnceTaskScheduler
$spec.Scheduler.runat = $castRunTime
$spec.Name = "Scheduled Remove All Snapshot Task $(get-date -date $removeRunTime -format yyyy-mm-dd_hh-mm_ddd)"
$spec.Description = "Remove all $($vmname) snapshot on $(get-date -date $removeRunTime -format yyyy-mm-dd_hh-mm_ddd)"
$spec.Enabled = $true
$spec.Notification = $emailAddr
$spec.Action = New-Object VMware.Vim.MethodAction
$spec.Action.Name = "RemoveAllSnapshots_Task"
#this is plain not working as I don't really know what the arguments it needs. I made an educated guess by
#specifying only the snapshot name and description.
$spec.action.argument = New-Object VMware.Vim.MethodActionArgument[] (1)
($spec.action.argument[0] = New-Object VMware.Vim.MethodActionArgument).Value = $true
(Get-View -Id 'ScheduledTaskManager-ScheduledTaskManager'-server $vCenterServer ).CreateScheduledTask($vm.Id, $spec)
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
The most important issue is that the RemoveSnapshot_Task method is not present on a VirtualMachine object, but on a VirtualMachineSnapshot object.
That means you will have to get the actual snapshot that you want to remove with a Scheduled Task.
That is rather hard to code (the snapshot needs to exists to get the MoRef of the snapshot), and definitely impossible to be a recurring Scheduled Task.
What I normally do is use the RemoveAllSnapshots_Task, which is a method that exists on a VirtualMachine object.
That method requires one parameter, a Boolean to indicate if Consolidate needs to be run after the snapshots are removed.
$spec = New-Object VMware.Vim.ScheduledTaskSpec
$spec.Scheduler = New-Object VMware.Vim.OnceTaskScheduler
$spec.Scheduler.runat = $castRunTime
$spec.Name = "Scheduled Remove All Snapshot Task $(get-date -date $removeRunTime -format yyyy-mm-dd_hh-mm_ddd)"
$spec.Description = "Remove all $($vmname) snapshot on $(get-date -date $removeRunTime -format yyyy-mm-dd_hh-mm_ddd)"
$spec.Enabled = $true
$spec.Notification = $emailAddr
$spec.Action = New-Object VMware.Vim.MethodAction
$spec.Action.Name = "RemoveAllSnapshots_Task"
#this is plain not working as I don't really know what the arguments it needs. I made an educated guess by
#specifying only the snapshot name and description.
$spec.action.argument = New-Object VMware.Vim.MethodActionArgument[] (1)
($spec.action.argument[0] = New-Object VMware.Vim.MethodActionArgument).Value = $true
(Get-View -Id 'ScheduledTaskManager-ScheduledTaskManager'-server $vCenterServer ).CreateScheduledTask($vm.Id, $spec)
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
I understand now that you can't schedule to remove a snapshot in advance of it existing. This code will probably be working for me. I just have to keep in mind that if there are multiple snapshots, they will all be removed.