在节点js中执行Process

时间:2017-05-18 23:36:41

标签: javascript node.js process jobs child-process

我的c#代码工作正常,在UI自动化期间在特定服务器框中运行作业。我需要节点js中的等效代码。

ProcessStartInfo startinfo = new ProcessStartInfo();
            startinfo.FileName = string.Format("schtasks.exe");

            string command = string.Format(" /run /s {0} /tn \"{1}\"", "serverbox", "Jobname");
            startinfo.Arguments = command;
            startinfo.CreateNoWindow = true;//Not much important
            startinfo.UseShellExecute = false;//Not much important
            startinfo.WindowStyle = ProcessWindowStyle.Hidden;//Not much important
            Common.Log.Info("Executing Command - " + command);
            Process proc = new Process();
            proc = Process.Start(startinfo);

我在node.js中有以下代码,它会抛出" args选项的值不正确"错误。

function executeProcess() {
  var command = new String(" /run /s {0} /tn \"{1}\"", "serverbox", "jobname");
  exec(command, function (err, stdout, stderr) {
    console.log("Running job");
    callback(err, stdout, stderr, data)
});

};

1 个答案:

答案 0 :(得分:1)

String构造函数只接受单个参数。 你的错误在这里:

var command = new String(" /run /s {0} /tn \"{1}\"", "serverbox", "jobname");

为避免错误,请使用名为string templates的JavaScript功能:

const command = ` /run /s "${serverbox}" /tn "${jobname}"`