开发者

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

开发者 https://www.devze.com 2023-03-03 21:58 出处:网络
I\'m trying to run a batch file on a server from an ASP.NET page. The batch file needs to run under a certain user account in order to be successful.

I'm trying to run a batch file on a server from an ASP.NET page. The batch file needs to run under a certain user account in order to be successful.

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. Also, if I login to the physical machine and run the .bat file manually under the account, it works fine.

Here's my code:

Process m_oProc = new Process();
ProcessStartInfo oInfo = new ProcessStartInfo(batchFileLocation);

oInfo.WorkingDirectory = Path.GetDirectoryName(batchFileLocation);
oInfo.UseShellExecute =开发者_如何学JAVA false;
oInfo.RedirectStandardOutput = true;
oInfo.RedirectStandardError = true;

// ------ This code seems to cause the exception ------ //

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?


As this is an internally facing application, I just set the Identity of the Application Pool to the account I want to run under - this resolved my issues.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号