Remove vCenter Scheduled Tasks with PowerCLI (Part 3)

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).

[cc lang=”Powershell”]
Function Remove-VIScheduledTask {
PARAM ([string]$taskName)
(Get-View -Id ((Get-VIScheduledTasks -Full | ?{$_.Name -eq $taskName}).ScheduledTask)).RemoveScheduledTask()
}
[/cc]

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 }
This entry was posted in Scripting, Virtualization. Bookmark the permalink.

2 Responses to Remove vCenter Scheduled Tasks with PowerCLI (Part 3)

  1. Peter says:

    Hello,
    your example doesnt check if the task allready ran once.
    Does this mean it removes all jobs no matter if they ran or not?

    ..or am I missing something… 🙂

  2. Hellow @Peter,

    When a user creates a scheduled task — either in the GUI or using the function I posted — you must specify a date/time for the snapshot to execute and that value must be in the future. The example I provided at the end of this post only removes scheduled tasks where the NextRunTime value is null. From my testing it seemed safe to assume that if a task wasn’t going to run again then it had already ran all the times that it needed to. Have you seen situations where this wasn’t the case? If so, please let me know some of the details and I’ll update the post accordingly. Thanks!

Leave a Reply

Your email address will not be published. Required fields are marked *

*

Notify me of followup comments via e-mail. You can also subscribe without commenting.