PowerCLI

 Create Scheduled task for all VMs on vmware with powercli

Jump to  Best Answer
Mustafa Dilbaz's profile image
Mustafa Dilbaz posted Dec 03, 2024 03:48 AM

Hello everyone;

I need powercli code to create a vcenter scheduled task that will run at 00:01 every night for all my virtual machines and delete snapshots older than 7 days.

LucD's profile image
LucD  Best Answer

You will have to call the method directly, no cmdlet yet AFAIK.

#Requires -Modules @{ ModuleName="VMware.PowerCLI"; ModuleVersion="13.3.0" }

$vmName = 'MyVM'
$retentionDays = 7
$startTask = '12/06/2024 00:01:00'

$vm = Get-VM -Name $vmName

$spec = New-Object VMware.Vim.ScheduledTaskSpec
$spec.Scheduler = New-Object VMware.Vim.DailyTaskScheduler
$spec.Scheduler.ActiveTime = [System.DateTime]::Parse($startTask)
$spec.Scheduler.Hour = 0
$spec.Scheduler.Minute = 1
$spec.Scheduler.Interval = 1
$spec.Notification = ''         # No email notification
$spec.Name = "$vmName - Delete snapshots older than $retentionDays days"
$spec.Action = New-Object VMware.Vim.MethodAction
$arg1 = New-Object VMware.Vim.MethodActionArgument
$spec.Action.Argument += $arg1
$arg2 = New-Object VMware.Vim.MethodActionArgument
$arg2.Value = New-Object VMware.Vim.SnapshotSelectionSpec
$arg2.Value.RetentionDays = $retentionDays
$spec.Action.Argument += $arg2
$spec.Action.Name = 'RemoveAllSnapshots_Task'
$spec.Description = "Create scheduled task on $vmName to delete snapshots older than $retentionDays days"
$spec.Enabled = $true
$schedTaskMgr = Get-View ScheduledTaskManager
$schedTaskMgr.CreateScheduledTask($vm.ExtensionData.MoRef, $spec)
LucD's profile image
LucD

That will be a problem with a VC Scheduled Task I'm afraid.
In such a Task you can call a method (like RemoveSnapshot_Task), but no logic is available.
In other words, you can't select all snapshots older than 7 days.

You could run such a script as a scheduled task on a station where PS is available.
You can then schedule that script in the local task scheduler, such as for example Windows Task Scheduler or crontab.



Pawel Koc's profile image
Pawel Koc

I would recommend using Aria Orchestrator Standalone to do that. 

You can build a workflow similar to this one:

https://blanketvm.com/2021/09/26/vro-remove-vm-snapshots-older-than/

And schedule a workflow in Aria Orchestrator.

Mustafa Dilbaz's profile image
Mustafa Dilbaz
Unfortunately, we do not use vrealize. What I actually want to do is to be able to do exactly the operation that I can do from the interface in the picture, in bulk, with powercli. In other words, it is not a code that will delete snapshots older than 7 days. I need a command that will fill in the marked fields and create a scheduled task that will enter the parameters.
LucD's profile image
LucD

That is a new feature introduced in vCenter 8U3. See SnapshotSelectionSpec
Would have been nice if you mentioned that in your original question.


But ... such a scheduled task is run against a specific VM.
You would have to create a scheduled task for each VM where you want to manage the snapshots based on their age.

Mustafa Dilbaz's profile image
Mustafa Dilbaz

Sorry for not giving details. Yes, I am using 8U3.

Actually, if we can write a command to create it for a single VM, it would be possible. I can apply it to the whole system.

Mustafa Dilbaz's profile image
Mustafa Dilbaz

@LucD Thank You veeeeeery much. You're the Hero :)

Here my final script for alll vms

$vmNames = get-vm  | Where-Object { $_.Name -like '*'}
foreach($vmName in $vmnames)
{
$retentionDays = 7
$startTask = '12/06/2024 00:01:00'
$emailAddr = 'email@domain.com'
$vm = Get-VM -Name $vmName

$spec = New-Object VMware.Vim.ScheduledTaskSpec
$spec.Scheduler = New-Object VMware.Vim.DailyTaskScheduler
$spec.Scheduler.ActiveTime = [System.DateTime]::Parse($startTask)
$spec.Scheduler.Hour = 10
$spec.Scheduler.Minute = 10
$spec.Scheduler.Interval = 1
$spec.Notification = $emailAddr
$spec.Name = "$vmName - Delete snapshots older than $retentionDays days"
$spec.Action = New-Object VMware.Vim.MethodAction
$arg1 = New-Object VMware.Vim.MethodActionArgument
$spec.Action.Argument += $arg1
$arg2 = New-Object VMware.Vim.MethodActionArgument
$arg2.Value = New-Object VMware.Vim.SnapshotSelectionSpec
$arg2.Value.RetentionDays = $retentionDays
$spec.Action.Argument += $arg2
$spec.Action.Name = 'RemoveAllSnapshots_Task'
$spec.Description = "Create scheduled task on $vmName to delete snapshots older than $retentionDays days"
$spec.Enabled = $true
$schedTaskMgr = Get-View ScheduledTaskManager
$schedTaskMgr.CreateScheduledTask($vm.ExtensionData.MoRef, $spec)
}

If you want to create for specific group of virtual machines, you can use 

$vmNames = get-vm  | Where-Object { $_.Name -like 'VMname*'}

Here is the result