Execute schtasks in a ASP site running on IIS

拥有回忆 提交于 2019-12-24 09:16:01

问题


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


回答1:


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);


来源:https://stackoverflow.com/questions/15067300/execute-schtasks-in-a-asp-site-running-on-iis

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!