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)
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
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
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
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
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
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
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
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
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)
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.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
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.
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)
$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)
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.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Where do we find this MOB Browser?
Is included in the VC.
Just do
https://<VC-server>/mob
and logon with a VC account with the required permissions
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thanks...Could you give a quick run-down of what you do with this?
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.Scheduler = $dTScheduler
#Create Scheduled Task
$serviceInstance = get-view ServiceInstance
$stMgr = Get-View ($serviceInstance.Content.ScheduledTaskManager)
$stMgr.CreateScheduledTask( $dc.MoRef , $tSpec)