Scheduled Tasks - SetFlags

K

kj7ny

How do I enable/disable a scheduled task using Python?

I can get to a task:

self.ts=pythoncom.CoCreateInstance
(taskscheduler.CLSID_CTaskScheduler,None,pythoncom.CLSCTX_INPROC_SERVER,taskscheduler.IID_ITaskScheduler)
self.ts.SetTargetComputer(u'\\\\SomeServer')
self.tasks=self.ts.Enum()
for task in self.tasks:
self.t=self.ts.Activate(task)
if self.t.GetAccountInformation().lower().find('SomeUser')
print self.t.GetFlags()

I can find if a task is enabled or not and toggle it:

disabled=self.t.GetFlags() &
taskscheduler.TASK_FLAG_DISABLED == taskscheduler.TASK_FLAG_DISABLED
print 'Originally Disabled: %s'%(disabled,)
if disabled:
self.t.SetFlags(self.t.GetFlags() &
~taskscheduler.TASK_FLAG_DISABLED)
else:
self.t.SetFlags(self.t.GetFlags() |
taskscheduler.TASK_FLAG_DISABLED)
disabled=self.t.GetFlags() &
taskscheduler.TASK_FLAG_DISABLED == taskscheduler.TASK_FLAG_DISABLED
print 'Recheck Disabled: %s'%(disabled,)

.... "Recheck Disabled" shows that the value "says" it has been
disabled.

The problem is that if I run it again and again, "Originally Disabled"
is always the same. In other words, "Recheck Disabled" does NOT
stick. It's like I'm missing a .commit()

For example:

First run ...
8388
Originally Disabled: True
Recheck Disabled: False

Second run ...
8388
Originally Disabled: True
Recheck Disabled: False

How do I disable and enable a scheduled task in Windows using python?
 
R

Roger Upole

kj7ny said:
How do I enable/disable a scheduled task using Python?

I can get to a task:

self.ts=pythoncom.CoCreateInstance
(taskscheduler.CLSID_CTaskScheduler,None,pythoncom.CLSCTX_INPROC_SERVER,taskscheduler.IID_ITaskScheduler)
self.ts.SetTargetComputer(u'\\\\SomeServer')
self.tasks=self.ts.Enum()
for task in self.tasks:
self.t=self.ts.Activate(task)
if self.t.GetAccountInformation().lower().find('SomeUser')
print self.t.GetFlags()

I can find if a task is enabled or not and toggle it:

disabled=self.t.GetFlags() &
taskscheduler.TASK_FLAG_DISABLED == taskscheduler.TASK_FLAG_DISABLED
print 'Originally Disabled: %s'%(disabled,)
if disabled:
self.t.SetFlags(self.t.GetFlags() &
~taskscheduler.TASK_FLAG_DISABLED)
else:
self.t.SetFlags(self.t.GetFlags() |
taskscheduler.TASK_FLAG_DISABLED)
disabled=self.t.GetFlags() &
taskscheduler.TASK_FLAG_DISABLED == taskscheduler.TASK_FLAG_DISABLED
print 'Recheck Disabled: %s'%(disabled,)

... "Recheck Disabled" shows that the value "says" it has been
disabled.

The problem is that if I run it again and again, "Originally Disabled"
is always the same. In other words, "Recheck Disabled" does NOT
stick. It's like I'm missing a .commit()

Your missing commit is spelled

pf=t.QueryInterface(pythoncom.IID_IPersistFile)
pf.Save(None,1)

Roger
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top