VMware Cloud Community
StephaneTardif
Contributor
Contributor
Jump to solution

Schedule removing a snapshot (again) with code.

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.


$snapTime = (Get-date).AddMinutes(5)
$snapRemoveTime = ($snaptime).AddMinutes(5)
$TaskID = "CHG0012345"
$snapName = "Snapshot $TaskID"
$requester = "requester Name"
$snapDescription = "Scheduled Snapshot for $Requester deletes on $snapRemoveTime"
$emailAddr = 'me.email@me.com'

$snapMemory = $false
$snapQuiesce = $true
$vmname = "ServerName"

# $vm = (get-vm $vmname).id
$vm = get-vm $vmname
$vCenterServer = $vm.Uid.Split(":")[0].Split("@")[1]

if (($vm | Measure-Object).Count -ne 1 ) { "Unable to locate a specific VM $vmName"; break }
try { $castRunTime = ([datetime]$snapTime).ToUniversalTime() } catch { "Unable to convert runtime parameter to date time value"; break }
try { $removeRunTime = ([datetime]$snapRemoveTime).ToUniversalTime() } catch { "Unable to convert runtime parameter to date time value"; break }
if ( [datetime]$snapTime -lt (Get-Date) ) { "Single run tasks can not be scheduled to run in the past. Please adjust start time and try again."; break }
if ( [datetime]$snapRemoveTime -lt ($snapTime) ) { "remove snapshot tasks can not be scheduled to run before the snapshot is taken. Please adjust start time and try again."; break }

$spec = New-Object VMware.Vim.ScheduledTaskSpec
$spec.Scheduler = New-Object VMware.Vim.OnceTaskScheduler
$spec.Scheduler.runat = $castRunTime
$spec.Name = "Scheduled Snapshot Task $(get-date -date $snapTime -format yyyy-mm-dd_hh-mm_ddd)"
$spec.Description = "Take a snapshot of $($vmname) on $(get-date -date $snapTime -format yyyy-mm-dd_hh-mm_ddd)"
$spec.Enabled = $true
$spec.Notification = $emailAddr
$spec.Action = New-Object VMware.Vim.MethodAction
$spec.Action.Name = "CreateSnapshot_Task"

$spec.action.argument = New-Object VMware.Vim.MethodActionArgument[] (4)
($spec.action.argument[0] = New-Object VMware.Vim.MethodActionArgument).Value = $snapName
($spec.action.argument[1] = New-Object VMware.Vim.MethodActionArgument).Value = $snapDescription
($spec.action.argument[2] = New-Object VMware.Vim.MethodActionArgument).Value = $snapMemory
($spec.action.argument[3] = New-Object VMware.Vim.MethodActionArgument).Value = $snapQuiesce

(Get-View -Id 'ScheduledTaskManager-ScheduledTaskManager'-server $vCenterServer ).CreateScheduledTask($vm.Id, $spec)

# this is the nebulous part
$spec = New-Object VMware.Vim.ScheduledTaskSpec
$spec.Scheduler = New-Object VMware.Vim.OnceTaskScheduler
$spec.Scheduler.runat = $castRunTime
$spec.Name = "Scheduled Remove Snapshot Task $(get-date -date $removeRunTime -format yyyy-mm-dd_hh-mm_ddd)"
$spec.Description = "Remove $($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 = "RemoveSnapshot_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[] (2)
($spec.action.argument[0] = New-Object VMware.Vim.MethodActionArgument).Value = $snapName
($spec.action.argument[1] = New-Object VMware.Vim.MethodActionArgument).Value = $snapDescription
 

(Get-View -Id 'ScheduledTaskManager-ScheduledTaskManager'-server $vCenterServer ).CreateScheduledTask($vm.Id, $spec)
On the RemoveSnapshot_Task because I obviously do not know the arguments since I could not really understand the online documentation.
I get
Exception calling "CreateScheduledTask" with "2" argument(s): "A specified parameter was not correct: "
 




0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

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

View solution in original post

2 Replies
LucD
Leadership
Leadership
Jump to solution

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

StephaneTardif
Contributor
Contributor
Jump to solution

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. 

0 Kudos