locked
ASP.NET IIS server access to run cmd command under another userName and password RRS feed

  • Question

  • I want to run cmd commands on IIS 8 under user administrator.

    My problem is that the cmd.exe run good on IIS only without UserName and password , but i need to run by cmd A tool that requires administrative privileges.

    on  my local computer this code run succsefuly. and on iis server i get the following exception:

    Exception:Access is denied
       at System.Diagnostics.Process.StartWithCreateProcess(ProcessStartInfo startInfo)
       at System.Diagnostics.Process.Start()
       at System.Diagnostics.Process.Start(ProcessStartInfo startInfo)

    this is my code :

     
      string strFilePath = PathBatFile;
     System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo("cmd.exe");
                psi.UseShellExecute = false;
                psi.RedirectStandardOutput = true;
                psi.RedirectStandardInput = true;
                psi.RedirectStandardError = true;
                psi.UserName = "Admin";
                psi.Domain = "DomainName";
                psi.Password = GetSecurePass("AdminPass");
                psi.CreateNoWindow = true;
    
    
      // Start the process
                System.Diagnostics.Process proc = System.Diagnostics.Process.Start(psi);
    
                // Open the batch file for reading
                System.IO.StreamReader strm = System.IO.File.OpenText(strFilePath);
    
                // Attach the output for reading
                System.IO.StreamReader sOut = proc.StandardOutput;
    
                // Attach the in for writing
                System.IO.StreamWriter sIn = proc.StandardInput;
    
                // Write each line of the batch file to standard input
                while (strm.Peek() != -1)
                {
                    sIn.WriteLine(strm.ReadLine());
                }
    
                strm.Close();
    
                // Exit CMD.EXE
                string stEchoFmt = "# {0} run successfully. Exiting";
    
                sIn.WriteLine(String.Format(stEchoFmt, strFilePath));
                sIn.WriteLine("EXIT");
    
    
                // Close the process
                proc.Close();
    
                // Read the sOut to a string.
                string results = sOut.ReadToEnd().Trim();
    
                // Close the io Streams;
                sIn.Close();
                sOut.Close();

    Can someone give me some advice please?

    Thanks


    • Edited by racheli_S Monday, January 22, 2018 2:11 PM
    • Moved by Sabah ShariqMVP Monday, January 22, 2018 2:15 PM Moved From Visual C#
    Monday, January 22, 2018 2:10 PM

All replies

  • Hi racheli_S,

    This is Visual C# forum. As your issue is related to Web please ask your question into ASP.Net forum for getting quick response. I am moving your thread to off topic.

    https://forums.asp.net/

    Your understanding and cooperation will be grateful.

    Thanks,
    Sabah Shariq

    [If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click Answered "Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]

    Monday, January 22, 2018 2:15 PM
  • how can move my post to ASP.Net forum ?
    Monday, January 22, 2018 2:50 PM
  • You cannot move thread from MSDN to ASP.Net forum. You have to create a new question in https://forums.asp.net/ . Hope this will help you.

    Thanks,
    Sabah Shariq

    [If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click Answered "Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]

    Monday, January 22, 2018 3:10 PM