With the help of a previous script from LucD http://www.lucd.info/2009/10/18/scheduled-tasks-methodaction/ I am able to successfully create a scheduled task that Powers on a VM. I need to extend this so that if the existing task is already there it will update it with the new config. I have downloaded the SDK and read through the docs to try and figure it out but it still aludes me.
$dtmDate = Get-Date
$gmtoffset = 6
$startTime = $dtmDate.AddHours($gmtoffset)
$emailAddr = "<emailaddr>"
$vm = "<vm>"
$folder = "SchedTask"
$vmview = Get-View (Get-VM $vm)
$folderview = Get-View (Get-Folder $folder)
$spec = New-Object VMware.Vim.ScheduledTaskSpec
$spec.Name = "Power On " + $vm
$spec.Description = "Power On " + $vm
$spec.Enabled = $true
$spec.Notification = $emailAddr
$spec.Scheduler = New-Object VMware.Vim.OnceTaskScheduler
$spec.Scheduler.runat = $startTime
$spec.Action = New-Object VMware.Vim.MethodAction
$spec.Action.Name = "PowerOnVM_Task"
$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, $Spec)
This should do the trick.
$dtmDate = Get-Date
$gmtoffset = 6
$startTime = $dtmDate.AddHours($gmtoffset) $emailAddr = "lucd@lucd.info"
$vm = "MyVM"
$folder = "SchedTask"
$vm = Get-VM $vm $spec = New-Object VMware.Vim.ScheduledTaskSpec
$spec.Name = "Power On " + $vm.Name $spec.Description = "Power On " + $vm.Name $spec.Enabled = $true
$spec.Notification = $emailAddr
$spec.Scheduler = New-Object VMware.Vim.OnceTaskScheduler
$spec.Scheduler.runat = $startTime
$spec.Action = New-Object VMware.Vim.MethodAction
$spec.Action.Name = "PowerOnVM_Task" $stMgr = Get-View ScheduledTaskManager
$stMgr.RetrieveEntityScheduledTask($vm.ExtensionData.MoRef) | %{ $st = Get-View -Id $_ | where {$_.Info.Name -eq $spec.Name} } if($st){ $spec.Name += " Updated"
$st.ReconfigureScheduledTask($spec) } else{ $stMgr.CreateScheduledTask($vm.ExtensionData.MoRef, $Spec) }
The script sets up the $spec object as before. Then it checks if a Scheduled Task with that same name already exists for the VM.
If it does, it calls the ReconfigureScheduledTask method.
Otherwise it calls the CreateScheduledTask method.
Note that I adapted the script a bit to use some features present in the newer PowerCLI versions.
As a proof that the existing Scheduled Task gets updated, I change the name for the Scheduled Task.
But you can change whatever property in the $spec object in the same way
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
You will have to use the ReconfigureScheduledTask method.
It's a method defined on the ScheduledTask object.
Let me know if you need a sample script.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Yes, I've seen that in the SDK but I can't figure out how to make use of it in PowerCLI. A sample script would be great!
This should do the trick.
$dtmDate = Get-Date
$gmtoffset = 6
$startTime = $dtmDate.AddHours($gmtoffset) $emailAddr = "lucd@lucd.info"
$vm = "MyVM"
$folder = "SchedTask"
$vm = Get-VM $vm $spec = New-Object VMware.Vim.ScheduledTaskSpec
$spec.Name = "Power On " + $vm.Name $spec.Description = "Power On " + $vm.Name $spec.Enabled = $true
$spec.Notification = $emailAddr
$spec.Scheduler = New-Object VMware.Vim.OnceTaskScheduler
$spec.Scheduler.runat = $startTime
$spec.Action = New-Object VMware.Vim.MethodAction
$spec.Action.Name = "PowerOnVM_Task" $stMgr = Get-View ScheduledTaskManager
$stMgr.RetrieveEntityScheduledTask($vm.ExtensionData.MoRef) | %{ $st = Get-View -Id $_ | where {$_.Info.Name -eq $spec.Name} } if($st){ $spec.Name += " Updated"
$st.ReconfigureScheduledTask($spec) } else{ $stMgr.CreateScheduledTask($vm.ExtensionData.MoRef, $Spec) }
The script sets up the $spec object as before. Then it checks if a Scheduled Task with that same name already exists for the VM.
If it does, it calls the ReconfigureScheduledTask method.
Otherwise it calls the CreateScheduledTask method.
Note that I adapted the script a bit to use some features present in the newer PowerCLI versions.
As a proof that the existing Scheduled Task gets updated, I change the name for the Scheduled Task.
But you can change whatever property in the $spec object in the same way
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thanks!