Question

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!!!

Was it helpful?

Solution

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);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top