VMware Cloud Community
rg01
Contributor
Contributor
Jump to solution

Reconfigure Scheduled Task

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)

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

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

View solution in original post

Reply
0 Kudos
5 Replies
LucD
Leadership
Leadership
Jump to solution

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

rg01
Contributor
Contributor
Jump to solution

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!

Reply
0 Kudos
srnhpp
Enthusiast
Enthusiast
Jump to solution

                               

Hi  LucD,

Could you please give me the sample script? I need that one.

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

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

Reply
0 Kudos
rg01
Contributor
Contributor
Jump to solution

Thanks!

Reply
0 Kudos