[python-win32] wmi, Win32_ScheduledJob and Execution User
mail at timgolden.me.uk
Fri May 2 10:39:33 CEST 2008
> I've followed Tim Golden's cookbook example to schedule a job via his Python wmi module;
> however, I need to specify the user account a scheduled job gets executed under (i.e., the
> process owner). The cookbook example will schedule a job owned by SYSTEM. I note the
> actual WMI library provides for a User and Password option, which do not appear to be visible
> options from within the python wmi module's Win32_ScheduledJob call. I am uncertain if the
> aforementioned User and Password options actually cause the scheduled job to be run as that user?
> As a workaround I schedule my job via the Win32_ScheduledJob call and follow this up with a
> Win32_Process.Create call to "schtasks.exe", in which I change the job id's run user
> (i.e., via the /RU option) to the appropriate user name.
> Is there a way I can specify the run (or execution) user of a scheduled job within the
> Win32_ScheduledJob call or via the wmi interface? The way I am doing it right now, I could
> have simply called schtasks.exe directly and had it create the process with the intended
> run user, which I don't find very eloquent.
The answer seems to be: no you can't. You're limited by whatever interface the requisite
WMI class exposes, and the .Create method has the signature:
<function Create (Command, DaysOfMonth, DaysOfWeek, InteractWithDesktop, RunRepeatedly, StartTime) => (JobId, ReturnValue)>
which is a bit of a pain. As you say, once you use schtasks.exe, you might as well have used
it in the first place. I would guess that this area in WMI has suffered from the repositioning
of the old AT service as a kind of adjunct to the newer Scheduled Tasks mechanism.
(AT basically ran as Local System or Local Network accounts with no alternative; scheduled
tasks have to be set up to run as a specific user).
As a workaround which I would think is more trouble than it was worth, you might have
your scheduled job switch its user context internally using ImpersonateLoggedOnUser
or another such mechanism.
As *another* alternative, you could use the win32com.taskscheduler module from
the pywin32 packages. It takes a bit of getting into, but I think there are some
examples in the demo directories, and I've got some helper code squirrelled away
if you're interested.
More information about the python-win32