从ASP.Net页运行一个批处理文件 [英] Running a batch file from an ASP.Net page

查看:140
本文介绍了从ASP.Net页运行一个批处理文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过一个ASP.Net页面,在服务器上运行一个批处理文件,它的驾驶我疯了。当我运行低于code,没什么happnes - 我可以从一些日志报表看到,这code运行,但.bat文件,我传递给函数不运行

有谁请告诉我,我在做什么错了?

 公共无效ExecuteCommand(字符串batchFileLocation)
{
   进程p =新的Process();   //创建安全​​密码
   字符串prePassword =myadminpwd;
   SecureString的passwordSecure =新SecureString的();
   的char [] = passwordChars prePassword.ToCharArray();
   的foreach(在passwordChars字符C)
   {
       passwordSecure.AppendChar(C);
   }   //设置参数的过程
   p.StartInfo.FileName = @C:\\\\ \\\\的Windows SYSTEM32 \\ cmd.exe的;
   p.StartInfo.Arguments = @/ C+ batchFileLocation;
   p.StartInfo.LoadUserProfile = TRUE;
   p.StartInfo.UserName =admin的;
   p.StartInfo.Password = passwordSecure;
   p.StartInfo.UseShellExecute = FALSE;
   p.StartInfo.CreateNoWindow = TRUE;   //运行过程,并等待它完成
   p.Start();
   p.WaitForExit();
}

在应用程序事件查看器登录服务器,每次我试图运行这个时候,下面的问题似乎出现:


  

错误的应用程序CMD.EXE,版本6.0.6001.18000,时间戳0x47918bde,错误模块KERNEL32.DLL,版本6.0.6001.18000,时间戳0x4791a7a6,异常code 0xc0000142,故障偏移0x00009cac,进程ID 0x8bc,应用程序启动时间0x01cc0a67825eda4b。


更新

以下code正常工作(其运行的批处理文件):

 进程p =新工艺();
p.StartInfo.FileName = batchFileLocation;
p.StartInfo.WorkingDirectory = Path.GetDirectoryName(batchFileLocation);
p.StartInfo.UseShellExecute = FALSE;//运行过程,并等待它完成
p.Start();
p.WaitForExit();

然而,这不(当我尝试为特定用户身份运行):

 进程p =新工艺();//创建安全​​密码
字符串prePassword =ADMINPASSWORD;
SecureString的passwordSecure =新SecureString的();
的char [] = passwordChars prePassword.ToCharArray();
的foreach(在passwordChars字符C)
{
      passwordSecure.AppendChar(C);
}p.StartInfo.FileName = batchFileLocation;
p.StartInfo.WorkingDirectory = Path.GetDirectoryName(batchFileLocation);
p.StartInfo.UseShellExecute = FALSE;
p.StartInfo.UserName =admin的;
p.StartInfo.Password = passwordSecure;//运行过程,并等待它完成
p.Start();
p.WaitForExit();


解决方案

这是错误的应用程序cmd.exe的小谷歌指出我到这的 IIS论坛

看来你不能在IIS下的背景下创建新的进程,除非你使用的 CreateProcessWithLogon 方法。 (我没有测试过这一点)。

I'm trying to run a batch file on a server via an ASP.Net page, and it's driving my crazy. When I run the below code, nothing happnes - I can see from some log statements that this code runs, but the .bat file that I pass to the function never runs.

Could anybody please tell me what I'm doing wrong?

public void ExecuteCommand(string batchFileLocation)
{
   Process p = new Process();

   // Create secure password
   string prePassword = "myadminpwd";
   SecureString passwordSecure = new SecureString();
   char[] passwordChars = prePassword.ToCharArray();
   foreach (char c in passwordChars)
   {
       passwordSecure.AppendChar(c);
   }

   // Set up the parameters to the process
   p.StartInfo.FileName = @"C:\\Windows\\System32\cmd.exe";
   p.StartInfo.Arguments = @" /C " + batchFileLocation;
   p.StartInfo.LoadUserProfile = true;
   p.StartInfo.UserName = "admin";
   p.StartInfo.Password = passwordSecure;
   p.StartInfo.UseShellExecute = false;
   p.StartInfo.CreateNoWindow = true;

   // Run the process and wait for it to complete
   p.Start();
   p.WaitForExit();
}

In the 'Application' Event Viewer log on the server, every time I try to run this, the following issue seems to occur:

Faulting application cmd.exe, version 6.0.6001.18000, time stamp 0x47918bde, faulting module kernel32.dll, version 6.0.6001.18000, time stamp 0x4791a7a6, exception code 0xc0000142, fault offset 0x00009cac, process id 0x8bc,application start time 0x01cc0a67825eda4b.

UPDATE

The following code works fine (it runs the batch file):

Process p = new Process();
p.StartInfo.FileName = batchFileLocation;
p.StartInfo.WorkingDirectory = Path.GetDirectoryName(batchFileLocation);
p.StartInfo.UseShellExecute = false;

// Run the process and wait for it to complete
p.Start();
p.WaitForExit();

This however doesn't (when i try to run as a specific user):

Process p = new Process();

// Create secure password
string prePassword = "adminpassword";
SecureString passwordSecure = new SecureString();
char[] passwordChars = prePassword.ToCharArray();
foreach (char c in passwordChars)
{
      passwordSecure.AppendChar(c);
}

p.StartInfo.FileName = batchFileLocation;
p.StartInfo.WorkingDirectory = Path.GetDirectoryName(batchFileLocation);
p.StartInfo.UseShellExecute = false;
p.StartInfo.UserName = "admin";
p.StartInfo.Password = passwordSecure;

// Run the process and wait for it to complete
p.Start();
p.WaitForExit();

解决方案

A little google on "Faulting application cmd.exe" points me to this IIS forum.

It seems that you cannot create a new process in the background under IIS, unless you use the CreateProcessWithLogon method. (I have not tested this).

这篇关于从ASP.Net页运行一个批处理文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆