Get VMware vCenter Scheduled Tasks with PowerCLI
January 19, 2013 1 Comment
VMware vSphere PowerCLI is missing cmdlets to work with VMware vCenter Scheduled Tasks. In a series of blog posts I will show you some PowerShell advanced functions that you can use to work with vCenter Scheduled Tasks. The first function is Get-VCScheduledTask. You can use this function to retrieve one or more scheduled tasks from your vCenter Server.
The Get-VCScheduledTask function
function Get-VCScheduledTask {
<#
.SYNOPSIS
Retrieves the scheduled tasks on a vCenter Server.
.DESCRIPTION
Retrieves the scheduled tasks on a vCenter Server.
.PARAMETER Name
Specify the name of the scheduled tasks you want to retrieve. If no value is
given to this parameter, the command returns all the scheduled tasks.
.PARAMETER Server
Specify the vCenter server to apply the command on. If no value is given to
this parameter, the command runs on the server currently specified in the
$DefaultVIServers variable.
.EXAMPLE
PS C:\> Get-VCScheduledTask -Server vCenter
Retrieves all the scheduled tasks for the vCenter server named vCenter.
.EXAMPLE
PS C:\> Get-VCScheduledTask -Name "*Update Manager*"
Retrieves all the VMware vSphere Update Manager scheduled tasks.
.INPUTS
System.String
.OUTPUTS
PSCustomObject
.NOTES
Author: Robert van den Nieuwendijk
Date: 17-1-2013
Version: 1.0
.LINK
http://rvdnieuwendijk.com/
http://twitter.com/rvdnieuwendijk
#>
[CmdletBinding()]
[OutputType([PSObject])]
# Parameter definitions
param([parameter(position=0)][string[]]$Name = "*",
[parameter(position=1)][string[]]$Server = "*"
)
# Loop through the servers
foreach ($SingleServer in $Server) {
# Loop through the scheduled task names
foreach ($SingleName in $Name) {
# Create the parameterset for the Get-View cmdlet
$GetViewParameterSet = @{
Id = "ServiceInstance"
}
if ($SingleServer)
{
$GetViewParameterSet += @{
Server = $SingleServer
}
}
# Get the ScheduledTaskManager objects
$ServiceInstance = Get-View @GetViewParameterSet
$ScheduledTaskManager = $ServiceInstance |
ForEach-Object {
if ($_)
{
Get-View -Id $_.Content.ScheduledTaskManager
}
}
# Get the scheduled tasks
if ($ScheduledTaskManager) {
Get-View -Id ($ScheduledTaskManager |
Select -ExpandProperty ScheduledTask |
Sort-Object -Unique) |
Where-Object {
$_.Info.Name -like $SingleName -and $_.Client.ServiceUrl.Split('/')[2].Split(':')[0] -like $SingleServer
} |
ForEach-Object {
if ($_)
{
$ScheduledTask = $_
# Find the server
$VIServer = $global:DefaultVIServers |
Where-Object {
$_.Name -eq $ScheduledTask.Client.ServiceUrl.Split('/')[2].Split(':')[0]
}
# Create the output objects
New-Object -TypeName PSObject -Property @{
Name = $ScheduledTask.Info.Name
Description = $ScheduledTask.Info.Description
Action = $ScheduledTask.Info.Action
EntityId = $ScheduledTask.Info.Entity
Entity = $ScheduledTask.Info.Entity |
Get-VIObjectByVIView -Server $VIServer
Enabled = $ScheduledTask.Info.Enabled
Notification = $ScheduledTask.Info.Notification
State = $ScheduledTask.Info.State
Progress = $ScheduledTask.Info.Progress
LastModifiedTime = $ScheduledTask.Info.LastModifiedTime
LastModifiedUser = $ScheduledTask.Info.LastModifiedUser
NextRunTime = $ScheduledTask.Info.NextRunTime
PrevRunTime = $ScheduledTask.Info.PrevRunTime
Scheduler = $ScheduledTask.Info.Scheduler
ExtensionData = $ScheduledTask
Id = $ScheduledTask.MoRef
Uid = "/VIServer=$($VIServer.User)@$($ScheduledTask.Client.ServiceUrl.Split('/')[2])/ScheduledTask=$($ScheduledTask.MoRef)/"
Client = $ScheduledTask.Client
} |
Select-Object -Property Name,Description,Action,EntityId,Entity,Enabled,
Notification,State,Progress,LastModifiedTime,LastModifiedUser,
NextRunTime,PrevRunTime,Scheduler,ExtensionData,Id,Uid,Client
}
}
}
}
}
}
Listing 1. The Get-VCScheduledTask function to retrieve VMware vCenter Scheduled tasks.
Annotations
Line 47-48: Both parameters are defined as [string[]]. That means that you can specify an array as the value of the parameter.
Line 55-64: A PowerShell technique called splatting is used to create the parameter set of the Get-View cmdlet used in line 67.
Line 67: The GetViewParameterSet variable is used as a parameter to the Get-View cmdlet. Notice that it is used as @GetViewParameterSet without a $ sign.
Line 117-119: The output of the New-Object cmdlet on line 96-116 can have it properties in an unsorted order. The Select-Object cmdlet orders the properties.
Sample output
The objects that the Get-VCScheduledTask function returns look like the objects that the PowerCLI Get-* cmdlets return. The objects have a Name, ExtensionData, Id, Uid and Client property.
PowerCLI C:\users\robert> Get-VCScheduledtask Name : VMware vCenter Update Manager Update Download Description : A pre-defined scheduled task to download software patch definitions. Action : VMware.Vim.CreateTaskAction EntityId : Folder-group-d1 Entity : Datacenters Enabled : True Notification : State : success Progress : LastModifiedTime : 6-11-2012 14:25:44 LastModifiedUser : com.vmware.vcIntegrity NextRunTime : 18-1-2013 03:10:00 PrevRunTime : 17-1-2013 03:10:00 Scheduler : VMware.Vim.DailyTaskScheduler ExtensionData : VMware.Vim.ScheduledTask Id : ScheduledTask-schedule-101 Uid : /VIServer=rvdnieuwendijk\robert@vcenter:443/ScheduledTask=ScheduledTask-schedule-101/ Client : VMware.Vim.VimClient Name : VMware vCenter Update Manager Check Notification Description : A pre-defined scheduled task to check update notifications. Action : VMware.Vim.CreateTaskAction EntityId : Folder-group-d1 Entity : Datacenters Enabled : True Notification : State : success Progress : LastModifiedTime : 6-11-2012 14:25:45 LastModifiedUser : com.vmware.vcIntegrity NextRunTime : 17-1-2013 20:04:00 PrevRunTime : 17-1-2013 19:04:00 Scheduler : VMware.Vim.HourlyTaskScheduler ExtensionData : VMware.Vim.ScheduledTask Id : ScheduledTask-schedule-102 Uid : /VIServer=rvdnieuwendijk\robert@vcenter:443/ScheduledTask=ScheduledTask-schedule-102/ Client : VMware.Vim.VimClient Name : Power on storage-test Description : Action : VMware.Vim.MethodAction EntityId : VirtualMachine-vm-109 Entity : storage-test Enabled : True Notification : State : success Progress : LastModifiedTime : 3-1-2013 14:03:00 LastModifiedUser : rvdnieuwendijk\robert NextRunTime : PrevRunTime : 5-1-2013 15:02:00 Scheduler : VMware.Vim.OnceTaskScheduler ExtensionData : VMware.Vim.ScheduledTask Id : ScheduledTask-schedule-201 Uid : /VIServer=rvdnieuwendijk\robert@vcenter:443/ScheduledTask=ScheduledTask-schedule-201/ Client : VMware.Vim.VimClient PowerCLI C:\users\robert>
Output 1. Sample output of the Get-VCscheduledTask function.
Have fun with scheduled tasks!

Pingback: VMTN Blog: Top 5 Planet V12N Posts – Jan 21st 2013 | Virtualization