Execute schtasks in a ASP site running on IIS
Asked Answered
H

1

1

I have a site running on IIS. I need to run the schtasks.exe from the site. I tried the following

Process proc = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo();
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.FileName = @"C:\Windows\System32\schtasks.exe";
proc.StartInfo.Arguments = "/create /tn mylbat /tr MYLExe.exe /s xxx /u xxx\xxx /p xxx /sc daily;
proc.StartInfo.RedirectStandardError = true;
proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.CreateNoWindow = false;
proc.StartInfo.WindowStyle = ProcessWindowStyle.Maximized;
proc.Start();
proc.WaitForExit();

The command when executed on cmd works fine but when I tried to execute it on a website running on IIS no task is being added. And also its working on sites that are not hosted on IIS.

Edit I: its working when the site's app pool identity is set to LocalSystem but I need it to be working as a Network Service.

What is missing!!!

Heroism answered 25/2, 2013 at 12:45 Comment(4)
Execution permissions? Does the site have permission to run external processed and/or access system folders?Intolerable
@Intolerable :I am not aware of it, how is it done on IIS?Heroism
Are you running on Windows 2003, 2008 or 2008 R2?Moderator
@Kev: The OS is windows7 and the site is run on IIS7. I also have the copy of schtasks.exe in the bin folder of my website if it could be run from the site folder itself.Heroism
F
0

It is not the /u and /p which is to be used. When accessing from a remote machine it should specify the /ru and /rp and they should belong to a user with admin privilege. The command is as following:

            startInfo.UseShellExecute = false;
            startInfo.WorkingDirectory = @"C:\Windows\System32";
            startInfo.FileName = @"C:\Windows\System32\schtasks.exe";
            startInfo.Arguments = "/create /tn <taskname> /tr <theEXE> /ru <user> /rp <password> /sc daily ";
            startInfo.RedirectStandardError = true;
            startInfo.RedirectStandardOutput = true;
            startInfo.CreateNoWindow = false;
            startInfo.WindowStyle = ProcessWindowStyle.Hidden;
            proc = Process.Start(startInfo);
Followthrough answered 27/2, 2013 at 10:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.