我正在尝试使用 Visual Studios 2005 中的 Windows Process.start 服务来调用调用 bat 文件的 Windows 任务计划程序 (schtasks)。该过程工作正常,只是 bat 文件接受参数,但当我尝试将参数传递到 bat 文件中时,它不会工作。
public string RunSchtasks(string MachineName)
{
ErrorMessage = null;
Process myProcess = new Process();
ProcessStartInfo myProcessStartInfo = new ProcessStartInfo("schtasks");
myProcessStartInfo.UseShellExecute = false;
myProcessStartInfo.RedirectStandardError = true;
myProcessStartInfo.RedirectStandardOutput = true;
myProcessStartInfo.Arguments = "/run /s Machinename /tn mytest ParameterToPass2Bat";
myProcess.StartInfo = myProcessStartInfo;
try
{
myProcess.Start();
StreamReader myStreamReader1 = myProcess.StandardOutput;
string QueryResult = myProcess.StandardOutput.ReadToEnd();
}
我的代码在没有 ParameterToPass2Bat 部分的情况下运行良好。如果我想把这个参数传入bat文件中,它就不会接受。有谁知道如何做到这一点,以便bat文件通过Schtasks接收参数?
谢谢!!!
最佳答案
schtasks.exe没有任何命令行功能将参数传递到计划任务中。一种可能的方法是将参数文本保存在其自己的(临时)bat 文件中,其中包含专门设计的内容以将其设置为环境变量。然后您所要做的就是从计划任务bat文件中调用参数bat文件,并且环境变量将被设置。
参数bat文件的内容如下所示:
set parm=ParameterToPass2
计划任务bat文件的内容将执行参数bat文件:
C:\wherever\argument.bat
关于batch-file - 如何通过asp.net schtasks向bat文件传入参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13982981/