PowerCLI

 View Only
Expand all | Collapse all

struggling with scheduled task creation

  • 1.  struggling with scheduled task creation

    Posted Jun 05, 2008 11:02 PM

    I am so close I can feel it! I've been trying to port an example seen in the programming guide (http://www.vmware.com/support/developer/vc-sdk/visdk25pubs/visdk25programmingguide.pdf) p. 163 "Creating a Scheduled Task". I had to leap a few hurdles and now at the end I'm stumped again. Here's the code and the error:

    # Find object on which to perform action
    $vmview = Get-View (Get-VM SDK-XPSP2).ID
    $maa = New-Object VMware.Vim.MethodActionArgument
    $maa.Value = $vmview.MoRef
    $ma = New-Object VMware.Vim.MethodAction
    $ma.Argument = $maa
    $ma.Name = "powerOnVM"
    $dTScheduler = New-Object VMware.Vim.DailyTaskScheduler
    $dTScheduler.Hour = 18
    $dTScheduler.Minute = 08
    $tSpec = New-Object VMware.Vim.ScheduledTaskSpec
    $tSpec.Description = "Start virtual machine according to schedule."
    $tSpec.Enabled = $TRUE
    $tSpec.Name = "Power On Virtual Machine"
    $tSpec.Action = $ma
    $tSpec.Scheduler = $dTScheduler
    $tSpec.Notification = "hal@halr9000.com"
    $vc = New-Object VMware.Vim.VimClient
    $stmMoRef = New-Object VMware.Vim.ManagedObjectReference
    $stm = New-Object VMware.Vim.ScheduledTaskManager $vc,$stmMoRef
    $stm.CreateScheduledTask($vmview.MoRef, $tSpec)

    The lsat four lines are what is giving me trouble. Took me a while to determine what obj types the ScheduledTaskManager wanted... But I am not satisfied that I did $vc & stmMoRef correctly. The error I receive is:

    cheduletask test.ps1'

    Exception calling "CreateScheduledTask" with "2" argument(s): "Object reference not set to an instance of an object."

    At C:\documents and settings\hrottenberg\my documents\windowspowershell\scripts\book\scheduletask test.ps1:21 char:25

    + $stm.CreateScheduledTask <<<< ($vmview.MoRef, $tSpec)

    Any pointers would be helpful.

    -hal

    Hal Rottenberg

    Co-Host, PowerScripting Podcast (http://powerscripting.net)



  • 2.  RE: struggling with scheduled task creation
    Best Answer

    Posted Jun 06, 2008 07:23 AM

    Hal, you were very close but the SDK Programming Guide put you on the wrong foot.

    This should do the trick

    # Find object on which to perform action
    $vmview = Get-View (Get-VM <guest-name>).ID
    $esxview = Get-View (Get-VMHost <ESX-hostname>).ID
    
    $maa = New-Object VMware.Vim.MethodActionArgument
    $maa.Value = $esxview.MoRef
    $ma = New-Object VMware.Vim.MethodAction
    $ma.Argument = $maa
    $ma.Name = "PowerOnVM_Task"
    $dTScheduler = New-Object VMware.Vim.DailyTaskScheduler
    $dTScheduler.Hour = 18
    $dTScheduler.Minute = 08
    $dTScheduler.Interval = 1
    $tSpec = New-Object VMware.Vim.ScheduledTaskSpec
    $tSpec.Action = $ma
    $tSpec.Description = "Start virtual machine according to schedule."
    $tSpec.Enabled = $TRUE
    $tSpec.Name = "Power On Virtual Machine"
    $tSpec.Notification = "hal@halr9000.com"
    $tSpec.Scheduler = $dTScheduler
    
    $svcRef = new-object VMware.Vim.ManagedObjectReference
    $svcRef.Type = "ServiceInstance"
    $svcRef.Value = "ServiceInstance"
    $serviceInstance = get-view $svcRef
    
    $stMgr = Get-View ($serviceInstance.Content.ScheduledTaskManager)
    
    $stMgr.CreateScheduledTask($vmview.MoRef,$tSpec)
    

    Note1: the action name, contrary to SDK Prog Guide, is "PowerOnVM_Task"

    Note2: I used the ServiceInstance to get at the ScheduledTaskManager

    Note3: the MethodActionArgument wants the ESX host on which to run the task. The name of the guest is passed in CreateScheduledTask method

    Note4: the DailyTaskScheduler.Interval property (which comes from the RecurrentTaskScheduler object) apparently can't be empty or 0



  • 3.  RE: struggling with scheduled task creation

    Broadcom Employee
    Posted Jun 06, 2008 08:23 AM

    I've just want to add 2 minor notes to LucD's script.

    1. There is no need to get VM's host view. The host MoREf is stored to VM's Runtime.Host property

    $maa = New-Object VMware.Vim.MethodActionArgument
    $maa.Value = $vmview.runtime.host
    

    2. Because API docs says that ScheduledTaskManager is a singleton object you can just do:

    $stMgr = Get-View ScheduledTaskManager-ScheduledTaskManager
    $stMgr.CreateScheduledTask($vmview.MoRef,$tSpec)
    

    Regards,

    Yasen



  • 4.  RE: struggling with scheduled task creation

    Posted Jun 06, 2008 08:51 AM

    Is the code in your 2nd note correct ?

    If yes, would you mind elaborating on this because I don't really see what you do with this statement

    $stMgr = Get-View ScheduledTaskManager-ScheduledTaskManager
    



  • 5.  RE: struggling with scheduled task creation

    Broadcom Employee
    Posted Jun 06, 2008 11:23 AM

    As you know Get-View MoRef parameter can take ManagedObjectReference or string in format &lt;type&gt;-&lt;value&gt;. For example when you get view from VIObject you use the ID string property

    $vmview = Get-View (Get-VM <vm-name>).ID  # // VM Id is sothething like VirtualMachine-vm-220
    

    So instead of

    $svcRef = new-object VMware.Vim.ManagedObjectReference
    $svcRef.Type = "ServiceInstance"
    $svcRef.Value = "ServiceInstance"
    $serviceInstance = get-view $svcRef
    

    you can just write:

    $serviceInstance = Get-View ServiceInstance-ServiceInstance
    

    The singleton API objects are like ServiceInstance, AlarmManager, SheduledTaskManager objects have MoRef's values that are unique and even more - for most of them their values are the same as their types

    So to get the instance of the singleton object you can do

    $svcRef = new-object VMware.Vim.ManagedObjectReference
    $svcRef.Type = "ServiceInstance" ["AlarmManager"] ["SheduledTaskManager"]
    $svcRef.Value = "ServiceInstance" ["AlarmManager"] ["SheduledTaskManager"]
    get-view $svcRef
    

    or

    Get-View ServiceInstance-ServiceInstance
    Get-View AlarmManager-AlarmManager
    Get-View SheduledTaskManager-SheduledTaskManager
    

    I hope I manage to light up a little this strange looking statement :smileyhappy:



  • 6.  RE: struggling with scheduled task creation

    Posted Jun 06, 2008 12:35 PM

    Thanks for sharing this info.

    No, I didn't know that Get-View accepted a string in that format.

    Can't seem to find that option in the VI Toolkit Cmdlets reference either. :smileysad:



  • 7.  RE: struggling with scheduled task creation

    Posted Jun 06, 2008 12:59 PM

    No, I didn't know that Get-View accepted a string in that format.

    Can't seem to find that option in the VI Toolkit Cmdlets reference either. :smileysad:

    Don't get used to it, stuff like that changes. :smileygrin:

    P.S. I use "get-command | select -expand parametersets | select -expand parameters" a lot.

    Hal Rottenberg

    Co-Host, PowerScripting Podcast (http://powerscripting.net)



  • 8.  RE: struggling with scheduled task creation

    Posted Jun 06, 2008 12:30 PM

    Ahh, sweet. Look for some interesting stuff to come of this, I think.

    I'm good with PowerShell but I have a lot to learn about the VI SDK... Thanks Luc & yk!

    Hal Rottenberg

    Co-Host, PowerScripting Podcast (http://powerscripting.net)



  • 9.  RE: struggling with scheduled task creation

    Posted Jun 07, 2008 12:50 AM

    $ma.Name = "PowerOnVM_Task"

    Note1: the action name, contrary to SDK Prog Guide, is "PowerOnVM_Task"

    Will this always equal the name of a method?

    Note2: I used the ServiceInstance to get at the ScheduledTaskManager

    Where's that documented, do you know?

    Hal Rottenberg

    Co-Host, PowerScripting Podcast (http://powerscripting.net)



  • 10.  RE: struggling with scheduled task creation

    Posted Jun 07, 2008 08:48 AM

    No clue. I discovered that through reverse engineering.

    Create a scheduled task with the VI client and then investigate the properties with the MOB browser - the SDK programmer's best friend :smileywink:

    The SDK Programming Guide states on p34 "The ServiceInstance managed object (Figure 2-4) is the central access point to all services and objects on the server.".

    You can use the MOB browser (p32 of the same guide) to look at this structure.



  • 11.  RE: struggling with scheduled task creation

    Posted Aug 07, 2008 10:27 PM

    Where do we find this MOB Browser?



  • 12.  RE: struggling with scheduled task creation

    Posted Aug 07, 2008 10:36 PM

    Is included in the VC.

    Just do

    https://<VC-server>/mob
    

    and logon with a VC account with the required permissions



  • 13.  RE: struggling with scheduled task creation

    Posted Aug 08, 2008 03:27 AM

    Thanks...Could you give a quick run-down of what you do with this?



  • 14.  RE: struggling with scheduled task creation

    Posted Sep 03, 2009 07:52 AM

    Hi LucD,

    Following you script, I tried to run a script as a scheduled job. This is supported in API, but I am not able to even make it work.

    Two issues..

    1. I need to apply this to datacenter (which is a managed entity, so should not be a problem), not to a host or vm

    2. I get error when I run below script

    Exception calling "CreateScheduledTask" with "2" argument(s): "A specified parameter was not correct. " At C:\HK-WorkArea\VmWare\PS-Scripts-v1.0.1\stm.ps1:27 char:27 + $stMgr.CreateScheduledTask( &lt;&lt;&lt;&lt; $dc.MoRef , $tSpec)

    -


    #Find object on which to perform action (wish to apply task to "DataCenters" object)

    $dc = Get-Datacenter | Get-View

    #RunScript Action

    $rsa = New-Object VMware.Vim.RunScriptAction

    $rsa.Script = "C:\WINDOWS\system32\WindowsPowerShell\v1.0\powershell.exe -command 'd:\test.ps1'"

    #Scheduler

    $dTScheduler = New-Object VMware.Vim.DailyTaskScheduler

    $dTScheduler.Hour = 18

    $dTScheduler.Minute = 08

    $dTScheduler.Interval = 1

    #Scheduled Task Spec

    $tSpec = New-Object VMware.Vim.ScheduledTaskSpec

    $tSpec.Action = $rsa

    $tSpec.Description = "My Script Scheduling"

    $tSpec.Enabled = $TRUE

    $tSpec.Name = "Test Sched."

    $tSpec.Notification =

    $tSpec.Scheduler = $dTScheduler

    #Create Scheduled Task

    $serviceInstance = get-view ServiceInstance

    $stMgr = Get-View ($serviceInstance.Content.ScheduledTaskManager)

    $stMgr.CreateScheduledTask( $dc.MoRef , $tSpec)