Get vCenter Scheduled Tasks with PowerCLI (Part 1)

I work with a lot of scheduled tasks. Typically, these tasks run powershell scripts from the Windows Task Scheduler. However, when working with simple VMware tasks (like creating snapshots right before a maintenance window) I like to use the Scheduled Tasks feature from right inside vCenter. The other day I thought it would be interesting if I had a PowerShell function that I could run to create these snapshot tasks. The first step in this process is to figure out what tasks we already have…and understand where some of these properties are located using PowerCLI.

The following two functions can be used to get information on Scheduled Tasks inside vCenter. The first function Get-VIScheduledTasks will simply return a list of all scheduled tasks that exist in a vCenter environment. I picked out a handful of properties that I figured would be needed and return those by default, casting the date/time values into local time. If you need all of the properties (the native .Net view of the Scheduled Task Info objects) you can include the parameter -Full and all of the properties will be returned.

[cc lang=”powershell”]
Function Get-VIScheduledTasks {
PARAM ( [switch]$Full )
if ($Full) {
# Note: When returning the full View of each Scheduled Task, all date times are in UTC
(Get-View ScheduledTaskManager).ScheduledTask | %{ (Get-View $_).Info }
} else {
# By default, lets only return common headers and convert all date/times to local values
(Get-View ScheduledTaskManager).ScheduledTask | %{ (Get-View $_ -Property Info).Info } |
Select-Object Name, Description, Enabled, Notification, LastModifiedUser, State, Entity,
@{N=”EntityName”;E={ (Get-View $_.Entity -Property Name).Name }},
@{N=”LastModifiedTime”;E={$_.LastModifiedTime.ToLocalTime()}},
@{N=”NextRunTime”;E={$_.NextRunTime.ToLocalTime()}},
@{N=”PrevRunTime”;E={$_.LastModifiedTime.ToLocalTime()}},
@{N=”ActionName”;E={$_.Action.Name}}
}
}
[/cc]

This next function calls above function, but only returns the tasks whose action is “CreateSnapshot_Task”
[cc lang = “Powershell”]
Function Get-VMScheduledSnapshots {
Get-VIScheduledTasks | ?{$_.ActionName -eq ‘CreateSnapshot_Task’} |
Select-Object @{N=”VMName”;E={$_.EntityName}}, Name, NextRunTime, Notification
}
[/cc]

Sample usage would be:

# To find all tasks that failed to execute last run
Get-VIScheduledTasks | ?{$_.State -ne 'success'}

# To find all snapshots that are not scheduled to run again:
Get-VMScheduledSnapshots | ?{$_.NextRunTime -eq $null}

I hope you find these functions useful. Be sure to check out the next post – on creating scheduled snapshots with PowerCLI!

This entry was posted in Scripting, Virtualization. Bookmark the permalink.

10 Responses to Get vCenter Scheduled Tasks with PowerCLI (Part 1)

  1. Pingback: New vCenter Scheduled Tasks with PowerCLI (Part 2) | EnterpriseAdmins.org

  2. Pingback: Remove vCenter Scheduled Tasks with PowerCLI (Part 3) | EnterpriseAdmins.org

  3. Yan says:

    Hello Brian,

    Do these scripts work in 5.1? When I run get-vischeduledtasks, nothing happens.

    Yan

  4. Yan says:

    Apologies Brian. the scripts work fine:)

  5. lior says:

    hi
    the command Get-VIScheduledTasks is not working in PowerCLI
    what am i doing wrong? it’s vcenter 6 update2a

  6. John J Wilt says:

    Same for me, I tested 3 of the commands listed, they are not recognized.

  7. These commands are Powershell functions. They act as wrappers around PowerCLI commands and are not builtin to the shell. You would either need to copy/paste the text into a PowerCLI window or add the function to your Powershell profile to make it recognized in the session. I’ve included a sample below where this function was pasted into a PowerCLI session and executed. This test connected to vCenter 6.0 with PowerCLI 6.3r1.

    PowerCLI C:\> Function Get-VIScheduledTasks {
    >> PARAM ( [switch]$Full )
    >> if ($Full) {
    >> # Note: When returning the full View of each Scheduled Task, all date times are in UTC
    >> (Get-View ScheduledTaskManager).ScheduledTask | %{ (Get-View $_).Info }
    >> } else {
    >> # By default, lets only return common headers and convert all date/times to local values
    >> (Get-View ScheduledTaskManager).ScheduledTask | %{ (Get-View $_ -Property Info).Info } |
    >> Select-Object Name, Description, Enabled, Notification, LastModifiedUser, State, Entity,
    >> @{N=”EntityName”;E={ (Get-View $_.Entity -Property Name).Name }},
    >> @{N=”LastModifiedTime”;E={$_.LastModifiedTime.ToLocalTime()}},
    >> @{N=”NextRunTime”;E={$_.NextRunTime.ToLocalTime()}},
    >> @{N=”PrevRunTime”;E={$_.LastModifiedTime.ToLocalTime()}},
    >> @{N=”ActionName”;E={$_.Action.Name}}
    >> }
    >> }
    >>
    PowerCLI C:\>
    PowerCLI C:\> Get-VIScheduledTasks | Format-Table -AutoSize

    Name Description Enabled Notification LastModifiedUser State Entity
    —- ———– ——- ———— —————- —– ——
    Power On mgmt VM True CORP\Administrator success VirtualMachine-vm-123
    Utl VM snap Snap a VM True CORP\Administrator success VirtualMachine-vm-123

  8. Lars Panzerbjørn says:

    A bit late to the party, sorry, but I just wanted to say that these functions are super useful.

    Also, when saying “This doesn’t work” or similar, it would be really useful to describe any error messages you get, or even just saying that you don’t get any (If you don’t…).

  9. Gerry Maddock says:

    These work great (running 6.7u2), thank you!!!!

    Quick question, If I want the snapshot to the virtual machines memory too, is there a switch/option for that with your script?

  10. Gerry Maddock says:

    Never mind, I figured it out. Thanks again!

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.