none
Win32Exception when trying to run a .bat file as a specific user

    Question

  • I'm trying to run a batch file on a server (Windows Server 2003 SE SP2, unning IIS6). The batch file needs to run under a certain user account in order to be successful. The batch file runs fine if I login to the machine and run it manually.

    My code to run the batch file without it running under a specific account works fine, (when it runs under SYSTEM), but the batch file fails to execute successfully.

    What I need to be able to do is run it under a specific (Administrator) account on the server, but when I add the code to run under this account, I get an exception:

    System.ComponentModel.Win32Exception (0x80004005): Access is denied at System.Diagnostics.Process.StartWithCreateProcess(ProcessStartInfo startInfo) at System.Diagnostics.Process.Start()

    The account definetely exists (it's the only one on that machine) and has administritive rights.

    Here's my code:

    Process m_oProc = new Process(); 
    ProcessStartInfo oInfo = new ProcessStartInfo(batchFileLocation); 
     
    oInfo.WorkingDirectory = Path.GetDirectoryName(batchFileLocation); 
    
    oInfo.UseShellExecute = false; 
    oInfo.RedirectStandardOutput = true; oInfo.RedirectStandardError = true; 
    
    // ------ This code seems to cause the exception (no exception with this commented out, but batch file wont run as it's running under SYSTEM ------ // 
    
    string prePassword = "myadminpassword"; 
    SecureString passwordSecure = new SecureString(); 
    char[] passwordChars = prePassword.ToCharArray(); 
    foreach (char c in passwordChars) 
    {   
      passwordSecure.AppendChar(c); 
    } 
    
    oInfo.UserName = "admin"; 
    oInfo.Password = passwordSecure; 
    
    // ---------------------------------------------------- // 
    
    m_oProc.StartInfo = oInfo; 
    
    if (m_oProc.Start()) 
    {    
      log.Info("Debug: Process has started successfully"); 
    } 
    else 
    {    
      log.Error("Some error occured starting the process."); 
    } 
    

    Can anyone tell me what I'm doing wrong?

    Thanks in advance.

    Friday, May 06, 2011 4:48 PM

Answers

  • Hi Jimmy Collins,

    Thank you for posting. 

    From your description, Win32Exception is thrown by Process.Start method. Generally speaking, it works well as follows.   

      string strPath = @"C:\Program Files (x86)\MyTestBatch.bat";
    
    
    
      Process p = new Process();
    
    
    
      ProcessStartInfo pStart = new ProcessStartInfo(strPath);
    
    
    
      pStart.WorkingDirectory = Path.GetDirectoryName(strPath);
    
    
    
      pStart.UseShellExecute = false;
    
    
    
      pStart.RedirectStandardOutput = true;
    
    
    
      pStart.RedirectStandardError = true;
    
    
    
      p.StartInfo = pStart;
    
    
    
      if (p.Start())
    
    
    
      MessageBox.Show("Started!");
    
    
    
    

     But regarding your scenario, it looks that your question is related with accessing remote security-enhanced resources. I suggest you can check this KBfrom Microsoft. I think it helps you.

    For further more question about ASP.NET dev, I suggest you can also open a new thread into ASP.NET forum. You will get more helpful suggestions there.

    Hope this helps. If you have any finding, please feel free to let me know.

    Best Regards,


    Larcolais Gong[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

     
    • Marked as answer by Jimmy Collins Tuesday, May 10, 2011 7:43 PM
    Monday, May 09, 2011 7:31 AM

All replies

  • Are you attempting to use a domain account, or a local user account? You may also need to set oInfo.Domain to identify the correct user account.

    You may also want to set your secure string as readonly (passwordSecure.MakeReadOnly()). I doubt that is what's causing your problem, but as a general practice it's best to mark it read only once you're done initializing it.

    Friday, May 06, 2011 5:00 PM
  • 1)Does the account exist on a different domain? If yes, pass the domain name with the user name:

    oInfo.UserName = @"mydoman\admin"

    2) If the above fails, check if the second domain has trust relationship with the domain you are trying to access from.

    Friday, May 06, 2011 5:12 PM
  • This code is running from an aspx page, so it's running from the same machine as the user I'm trying to run as. I've tried your suggestion of adding the domain when I specify the username, but I still see the same issue.

    Any other suggestions? This has been driving me nuts for two days :-(

    Friday, May 06, 2011 9:24 PM
  • Hi Jimmy Collins,

    Thank you for posting. 

    From your description, Win32Exception is thrown by Process.Start method. Generally speaking, it works well as follows.   

      string strPath = @"C:\Program Files (x86)\MyTestBatch.bat";
    
    
    
      Process p = new Process();
    
    
    
      ProcessStartInfo pStart = new ProcessStartInfo(strPath);
    
    
    
      pStart.WorkingDirectory = Path.GetDirectoryName(strPath);
    
    
    
      pStart.UseShellExecute = false;
    
    
    
      pStart.RedirectStandardOutput = true;
    
    
    
      pStart.RedirectStandardError = true;
    
    
    
      p.StartInfo = pStart;
    
    
    
      if (p.Start())
    
    
    
      MessageBox.Show("Started!");
    
    
    
    

     But regarding your scenario, it looks that your question is related with accessing remote security-enhanced resources. I suggest you can check this KBfrom Microsoft. I think it helps you.

    For further more question about ASP.NET dev, I suggest you can also open a new thread into ASP.NET forum. You will get more helpful suggestions there.

    Hope this helps. If you have any finding, please feel free to let me know.

    Best Regards,


    Larcolais Gong[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

     
    • Marked as answer by Jimmy Collins Tuesday, May 10, 2011 7:43 PM
    Monday, May 09, 2011 7:31 AM
  • Hi Jimmy,

    Do you know this official Microsoft forum for IIS

    http://forums.iis.net

     


    Success
    Cor
    Monday, May 09, 2011 7:51 AM