2 Replies Latest reply on Sep 19, 2017 9:01 PM by glowery

    Creating a Scheduled Task for a VM

    glowery Novice

      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. Re: Creating a Scheduled Task for a VM
          Zsoldier Expert
          vExpert

          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)

          }

          1 person found this helpful
          • 2. Re: Creating a Scheduled Task for a VM
            glowery Novice

            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!