VMware Cloud Community
glowery
Enthusiast
Enthusiast
Jump to solution

Creating a Scheduled Task for a VM

Currently I know how to create a task for a VM for a scheduled  recurring nightly task through the Web client, but I can't seem to figure out a way to create a task via PowerCLI.  Hopefully I'm just missing something really obvious.

Ultimately what I'd like to do is be able to something like search for a VM using Get-VM -VM $vmname and pipe that to return any scheduled tasks that vm has and if it currently doesn't have one then I'd love to be able to set a new task. In the context of my current use case, just simply scheduling a nightly reboot.

Sorry if I'm just missing something obvious, I'm just starting to dive into PowerCLI and loving what I can do so far.

Thanks!

1 Solution

Accepted Solutions
Zsoldier
Expert
Expert
Jump to solution

Don't feel bad, this one was a hard one to figure out.

function Get-vCenterScheduledTask{

   <#

   .SYNOPSIS

  Retrieve vCenter Scheduled Tasks.

   .DESCRIPTION

  Retrieve vCenter Scheduled Tasks.

   .NOTES

  Source: Automating vSphere Administration

  Authors: Luc Dekens, Arnim van Lieshout, Jonathan Medd,

  Alan Renouf, Glenn Sizemore

    .EXAMPLE

  Get-vCenterScheduledTask | Select-Object Name,Description,NextRunTime,PrevRunTime,State,Notification

  #>

   $si = Get-View ServiceInstance

   $scheduledTaskManager = Get-View $si.Content.ScheduledTaskManager

   $tasks = $scheduledTaskManager.RetrieveEntityScheduledTask($null)

   $scheduledtasks = foreach ($task in $tasks){(Get-View $task).info}

   $scheduledtasks

}

In your particular case, you can pull for specific VM by using the the "RetrieveObjectScheduledTask" Method like so:

$si = Get-View ServiceInstance

$scheduledTaskManager = Get-view $si.content.ScheduledTaskManager

$tasksforVM = $scheduledTaskManager.RetrieveObjectScheduledTask($vm.extensiondata.moref)

$scheduledtasksforVM = foreach ($task in $tasksforVM){(get-view $task).info}

$scheduledtasksforVM

Example to reconfigure an existing scheduled task:

vmsch = Get-vCenterScheduledTask | where {$_.Name -match "something completely idiotic so nothing bad happens by running this without knowing what they are doing"}

Foreach ($task in $vmsch)

{

$spec = New-Object VMware.Vim.ScheduledTaskSpec

$spec.name = $task.name

$spec.description = $task.description

$spec.enabled = $true

$spec.scheduler = New-Object VMware.Vim.OnceTaskScheduler

$spec.scheduler.runAt = $task.scheduler.runAt

$spec.action = New-Object VMware.Vim.MethodAction

$spec.action.name = "CreateSnapshot_Task"

$spec.action.argument = New-Object VMware.Vim.MethodActionArgument[] (4)

$spec.action.argument[0] = New-Object VMware.Vim.MethodActionArgument

$spec.action.argument[0].value = ""

$spec.action.argument[1] = New-Object VMware.Vim.MethodActionArgument

$spec.action.argument[1].value = ""

$spec.action.argument[2] = New-Object VMware.Vim.MethodActionArgument

$spec.action.argument[2].value = $true

$spec.action.argument[3] = New-Object VMware.Vim.MethodActionArgument

$spec.action.argument[3].value = $false

$spec.notification = ""


$_this = Get-View -Id $task.ScheduledTask

$_this.ReconfigureScheduledTask($spec)

}

Chris Nakagaki (中垣浩一)
Blog: https://tech.zsoldier.com
Twitter: @zsoldier

View solution in original post

3 Replies
Zsoldier
Expert
Expert
Jump to solution

Don't feel bad, this one was a hard one to figure out.

function Get-vCenterScheduledTask{

   <#

   .SYNOPSIS

  Retrieve vCenter Scheduled Tasks.

   .DESCRIPTION

  Retrieve vCenter Scheduled Tasks.

   .NOTES

  Source: Automating vSphere Administration

  Authors: Luc Dekens, Arnim van Lieshout, Jonathan Medd,

  Alan Renouf, Glenn Sizemore

    .EXAMPLE

  Get-vCenterScheduledTask | Select-Object Name,Description,NextRunTime,PrevRunTime,State,Notification

  #>

   $si = Get-View ServiceInstance

   $scheduledTaskManager = Get-View $si.Content.ScheduledTaskManager

   $tasks = $scheduledTaskManager.RetrieveEntityScheduledTask($null)

   $scheduledtasks = foreach ($task in $tasks){(Get-View $task).info}

   $scheduledtasks

}

In your particular case, you can pull for specific VM by using the the "RetrieveObjectScheduledTask" Method like so:

$si = Get-View ServiceInstance

$scheduledTaskManager = Get-view $si.content.ScheduledTaskManager

$tasksforVM = $scheduledTaskManager.RetrieveObjectScheduledTask($vm.extensiondata.moref)

$scheduledtasksforVM = foreach ($task in $tasksforVM){(get-view $task).info}

$scheduledtasksforVM

Example to reconfigure an existing scheduled task:

vmsch = Get-vCenterScheduledTask | where {$_.Name -match "something completely idiotic so nothing bad happens by running this without knowing what they are doing"}

Foreach ($task in $vmsch)

{

$spec = New-Object VMware.Vim.ScheduledTaskSpec

$spec.name = $task.name

$spec.description = $task.description

$spec.enabled = $true

$spec.scheduler = New-Object VMware.Vim.OnceTaskScheduler

$spec.scheduler.runAt = $task.scheduler.runAt

$spec.action = New-Object VMware.Vim.MethodAction

$spec.action.name = "CreateSnapshot_Task"

$spec.action.argument = New-Object VMware.Vim.MethodActionArgument[] (4)

$spec.action.argument[0] = New-Object VMware.Vim.MethodActionArgument

$spec.action.argument[0].value = ""

$spec.action.argument[1] = New-Object VMware.Vim.MethodActionArgument

$spec.action.argument[1].value = ""

$spec.action.argument[2] = New-Object VMware.Vim.MethodActionArgument

$spec.action.argument[2].value = $true

$spec.action.argument[3] = New-Object VMware.Vim.MethodActionArgument

$spec.action.argument[3].value = $false

$spec.notification = ""


$_this = Get-View -Id $task.ScheduledTask

$_this.ReconfigureScheduledTask($spec)

}

Chris Nakagaki (中垣浩一)
Blog: https://tech.zsoldier.com
Twitter: @zsoldier
glowery
Enthusiast
Enthusiast
Jump to solution

Awesome, thank you! I'll give this a shot, but it already looks like exactly what I was looking for.  I didn't even think to go to using the method.  I even have that book in my collection....doh!

Thanks again!

0 Kudos
boudjel38xpo
Contributor
Contributor
Jump to solution

Hi,

i can't figure out how to use this script to create a new scheduled task for a VM using this script

my goal is to reboot on a weekly basis every sunday night with a task in vcenter

i have roughly 700 VM and i need to add a reboot task in vcenter for each vm

any help will be appreciated

thanks

boudjel

0 Kudos