In my most recent series on vCenter Scheduled Tasks with PowerCLI, I have provided three functions…Get-VIScheduledTasks, Get-VMScheduledSnapshots, and New-VMScheduledSnapshot. To complete the series, I have a final function Remove-VIScheduledTask. I don’t really see the need for this one, as you can easily bulk select scheduled tasks in the vCenter UI and delete them with a single click. However, in the interest of completeness, here is a rough function that will delete a vSphere scheduled task. [A more robust/efficient/complete function could be written that would accept input from the pipeline. If there is a good use case for this please leave a comment below.]
NOTE: This function requires an additional function: Get-VIScheduledTasks to find the task by name. In addition the usage example below requires Get-VMScheduledSnapshots. Both functions are available here: New vCenter Scheduled Tasks with PowerCLI (Part 1).
Function Remove-VIScheduledTask {
PARAM ([string]$taskName)
(Get-View -Id ((Get-VIScheduledTasks -Full | ?{$_.Name -eq $taskName}).ScheduledTask)).RemoveScheduledTask()
}
Usage example:
# This example will find all VM Scheduled Snapshots which
# are not scheduled to run again, then remove each one by name.
Get-VMScheduledSnapshots |
?{$_.NextRunTime -eq $null} | %{ Remove-VIScheduledTask $_.Name }