VMware

This Question is Answered

13 Replies Last post: Sep 3, 2009 12:51 AM by harkamal  

struggling with scheduled task creation posted: Jun 5, 2008 4:07 PM

Click to view halr9000's profile Master 813 posts since
Jun 7, 2007
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)

Re: struggling with scheduled task creation

1. Jun 6, 2008 12:22 AM in response to: halr9000
Click to view LucD's profile Champion 2,430 posts since
Oct 31, 2005
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

Re: struggling with scheduled task creation

2. Jun 6, 2008 1:22 AM in response to: LucD
Click to view ykalchev's profile Hot Shot 68 posts since
Mar 5, 2008

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

Re: struggling with scheduled task creation

3. Jun 6, 2008 1:51 AM in response to: ykalchev
Click to view LucD's profile Champion 2,430 posts since
Oct 31, 2005
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

Re: struggling with scheduled task creation

4. Jun 6, 2008 4:23 AM in response to: LucD
Click to view ykalchev's profile Hot Shot 68 posts since
Mar 5, 2008
As you know Get-View MoRef parameter can take ManagedObjectReference or string in format <type>-<value>. 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 :)

Re: struggling with scheduled task creation

6. Jun 6, 2008 5:34 AM in response to: ykalchev
Click to view LucD's profile Champion 2,430 posts since
Oct 31, 2005
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. :-(

Re: struggling with scheduled task creation

9. Jun 7, 2008 1:48 AM in response to: halr9000
Click to view LucD's profile Champion 2,430 posts since
Oct 31, 2005
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 ;-)

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.

Re: struggling with scheduled task creation

10. Aug 7, 2008 3:26 PM in response to: LucD
Click to view mbanusife's profile Novice 14 posts since
Mar 28, 2006
Where do we find this MOB Browser?

Re: struggling with scheduled task creation

11. Aug 7, 2008 3:36 PM in response to: mbanusife
Click to view LucD's profile Champion 2,430 posts since
Oct 31, 2005
Is included in the VC.
Just do
https://<VC-server>/mob


and logon with a VC account with the required permissions

Re: struggling with scheduled task creation

12. Aug 7, 2008 8:27 PM in response to: LucD
Click to view mbanusife's profile Novice 14 posts since
Mar 28, 2006

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

Re: struggling with scheduled task creation

13. Sep 3, 2009 12:52 AM in response to: LucD
Click to view harkamal's profile Hot Shot 219 posts since
Mar 19, 2009
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( <<<< $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 = me@v.com
$tSpec.Scheduler = $dTScheduler

#Create Scheduled Task

$serviceInstance = get-view ServiceInstance
$stMgr = Get-View ($serviceInstance.Content.ScheduledTaskManager)
$stMgr.CreateScheduledTask( $dc.MoRef , $tSpec)

VMware Developer

SDKs, APIs, Videos, Learn and much more in the Developer community.

Learn More

Developer Sample Code

Increase your developer productivity with VMware API sample code.

Learn More

VMworld Sessions & Labs

Online access to the latest VMworld Sessions & Labs and online services.

Learn more

Purchase PSO Credits Online

Purchase credits to redeem training and consulting services online.

Buy Now

Community Hardware Software

View reported configurations or report your own.

Learn More

VMware vSphere

Come witness the next giant leap in virtualization.

Register Today

Communities