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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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}}
  }
}

This next function calls above function, but only returns the tasks whose action is “CreateSnapshot_Task”

1
2
3
4
Function Get-VMScheduledSnapshots {
  Get-VIScheduledTasks | ?{$_.ActionName -eq 'CreateSnapshot_Task'} |
    Select-Object @{N="VMName";E={$_.EntityName}}, Name, NextRunTime, Notification
}

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!

7 comments

  1. […] a recent post New vCenter Scheduled Tasks with PowerCLI (Part 1), I provided two functions that return information related to vCenter Scheduled Tasks. This was my […]

  2. […] 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). […]

  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

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.