Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I have a scheduled task which runs a batch file which does some stuff. I want the batch file to kick off another batch file which runs in user space; i.e. %username% == the currently logged-in user, with user permissions, etc. How can I run a command under the current user, given that the scheduled task runs with SYSTEM permissions?

I need this for both Windows XP and Windows 7.

share|improve this question
add comment

3 Answers

Ever since the introduction of Terminal Services, "current user" can be plural. Even XP supports fast user switching.

The closest you can get is "user connected to the console session". For this, use WTSGetActiveConsoleSessionId() + WTSQueryUserToken() + CreateEnvironmentBlock() + CreateProcessAsUser().


I wrote this: https://gist.github.com/871048 – Compile, run with full path to your batch file as arguments.

Yes, this requires .NET Runtime, but it's likely that your systems already have it. The compiler's part of the Runtime, too: %SystemRoot%\Microsoft.NET\Framework64\v3.5\csc.exe (any version starting with v2.* will work).

Note: WTSQueryUserToken() requires the program to be running as LocalSystem. (According to the docs, SeTcbPrivilege is not enough, but I haven't checked.)

share|improve this answer
    
When this answer was posted, I didn't know about psexec yet. Using it might be easier. –  grawity Aug 6 '13 at 11:40
add comment

Under "When Running Task, use the following user account:" you can set it to "BUILTIN\Users" group, it will run the task as the current logged in user.

share|improve this answer
add comment

I don't think you can set it through the task scheduler tool to dynamically use the current logged in user, but you can specify a user account to use besides System in the task itself. Both of the below options will require that you have the password for the username.

Windows 7: In the properties window for the task, there is a button in Security Options called "Change User or Group".

Windows XP: In the properties window for the task, you can enter a username in the "Run As" field.

One final option may be to create a startup script that will create the task when the user logs on, and a companion logoff script to delete it when they log off. The command is "Schtasks", which is nearly identical in XP and 7. Documentation for the command is available from MS at the following site: http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/schtasks.mspx?mfr=true

share|improve this answer
    
I'm creating the task via the command line, so I can't change the run as user. Otherwise I would simply check that "run as currently logged on user" box which is terribly convenient! And regarding the logon/logoff script suggestion, that's pretty much what I intend to do, but the schtasks command cannot check the above-mentioned checkbox; it requires an username and password, and I don't want to prompt the user for their password from a script. –  Ricket Mar 15 '11 at 14:27
1  
Windows 7 does not seem to have the password entry requirement, at least when I just tried it with UAC off. For XP, you can try using the deprecated AT command instead. –  Hyppy Mar 15 '11 at 14:33
1  
But the AT command, even with the /interactive flag, runs the command in SYSTEM privileges. I've been testing it just this morning actually and I can't get it to run in the user environment. It seems there are a multitude of ways I could accomplish this in Windows 7, but the big issue at the moment is Windows XP. –  Ricket Mar 15 '11 at 15:01
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.