Original Message:
Sent: Feb 20, 2025 08:21 AM
From: LucD
Subject: Snapshot deletion scheduled task
The trick is to look at the method the Scheduled task is calling, in this case, RemoveAllSnapshots.
That method accepts 2 arguments, the 2nd one containing the property RetentionDays.
The MethodAction object allows us to pass those arguments.
You could do something like this
$si = Get-View ServiceInstance$scheduledTaskManager = Get-View $si.Content.ScheduledTaskManager# CSV layout# Name# vm1# vm2Import-Csv -Path .\Snapshots.csv -UseCulture |ForEach-Object -Process { $vm = Get-VM -Name $_.VMName # Create a new scheduled task $spec = New-Object VMware.Vim.ScheduledTaskSpec $spec.Name = "Scheduled snapshot removal of $vm" $spec.Description = 'Scheduled snapshot removal' $spec.Enabled = $true $spec.Scheduler = New-Object VMware.Vim.WeeklyTaskScheduler $spec.Scheduler.activeTime = [Datetime]"02/23/2025 03:00" $spec.Scheduler.Sunday = $true $spec.Scheduler.Hour = (Get-Date -Hour 3).ToUniversalTime().Hour $spec.Scheduler.Interval = "1" $spec.Action = New-Object VMware.Vim.MethodAction $arg1 = New-Object VMware.Vim.MethodActionArgument $arg2 = New-Object VMware.Vim.MethodActionArgument $arg2.Value = New-Object VMware.Vim.SnapshotSelectionSpec $arg2.Value.RetentionDays = 14 $spec.Action.Argument += $arg1 $spec.Action.Argument += $arg2 $spec.Action.Name = "RemoveAllSnapshots_Task" $scheduledTaskManager.CreateScheduledTask($vm.ExtensionData.MoRef, $spec)}
------------------------------
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Original Message:
Sent: Feb 20, 2025 06:42 AM
From: Nikolay Novakov
Subject: Snapshot deletion scheduled task
Hello,
I'm trying to make a script, which will create a scheduled task for automatic snapshot deletion for shapshots older than 14 days, simmilar to the task you can create in the vCenter :
My script so far :
(actually it's not mine, but plagiarized from different scripts by LucD :) )
$si = Get-View ServiceInstance
$scheduledTaskManager = Get-View $si.Content.ScheduledTaskManager
# CSV layout
# Name
# vm1
# vm2
Import-Csv -Path .\Snapshots.csv -UseCulture |
ForEach-Object -Process {
$vm = Get-VM -Name $_.VMName
$vCenterServer = $vm.Uid.Split(":")[0].Split("@")[1]
# Create a new scheduled task
$spec = New-Object VMware.Vim.ScheduledTaskSpec
$spec.Name = "Scheduled snapshot removal of $vm"
$spec.Description = 'Scheduled snapshot removal'
$spec.Enabled = $true
$spec.Scheduler = New-Object VMware.Vim.WeeklyTaskScheduler
$spec.Scheduler.activeTime = [Datetime]"02/23/2025 03:00"
$spec.Scheduler.Sunday = $true
$spec.Scheduler.Hour = (Get-Date -Hour 3).ToUniversalTime().Hour
$spec.Scheduler.Interval = "1"
$spec.Action = New-Object VMware.Vim.MethodAction
$spec.Action.Name = "RemoveAllSnapshots_Task"
(Get-View -Id 'ScheduledTaskManager-ScheduledTaskManager'-server $vCenterServer).CreateScheduledTask($vm.ExtensionData.MoRef, $spec)
}
Everything is working fine, except that i can't figure it out how to specify the variable for "Delete snapshots older than XX days" (as it's in the screenshot).
Any ideas ?
Thanks in advance.