C# - 命令从命令提示符运行,但不从服务运行

时间:2017-05-03 09:38:56

标签: c# node.js command-line newman

我在Windows Server 2012 R2上运行C#(ASP Core)服务,通过命令行执行Newman测试套件。直接在命令提示符下运行时,服务执行的命令可以正常工作,但不能从服务中运行。为了增加伤害,同样的命令可以在我的开发机器(Windows 10 Pro)上本地运行的服务中运行。我确信我在所有实例中都运行相同的命令,因为服务将CLI的StandardOutput输出到一个文件中,我将其内容直接粘贴到命令提示符中。

编辑:该服务托管在IIS上。

我收到的错误:

module.js:471
    throw err;
    ^

Error: Cannot find module 'C:\Users\MyName\AppData\Roaming\npm\node_modules\newman\bin\newman.js'
    at Function.Module._resolveFilename (module.js:469:15)
    at Function.Module._load (module.js:417:25)
    at Module.runMain (module.js:604:10)
    at run (bootstrap_node.js:393:7)
    at startup (bootstrap_node.js:150:9)
    at bootstrap_node.js:508:3

我正在运行的命令(所有路径在每个实例中都相同):

  

“C:\ Program Files \ NodeJS \ node.exe”C:\ Users \ MyName \ AppData \ Roaming \ npm \ node_modules \ newman \ bin \ newman.js run https://api.getpostman.com/collections/MyCollectionURI -r cli,junit - -reporter-junit-export D:\ TestHarnessServiceLogs \ XML \ {FilenameFromDatetime} .xml -e https://api.getpostman.com/environments/MyEnvironmentURI --disable-unicode

构建并运行命令的C#:

//Build over multiple lines to make it vaguely readable, then string replace away the newlines so it runs as one command
string runTestCmd = $@"{_nodeExecutablePath} 
                    {_newmanDotJsFile} run 
                    {collectionPath} 
                    -r cli,junit 
                    --reporter-junit-export {_junitReportPathWithFilename} 
                    -e {environmentPath} 
                    --disable-unicode"
                    .Replace(Environment.NewLine, " ")
                    .Replace("\t", "");

File.WriteAllText(@"D:\TestHarnessServiceLogs\Command.txt", runTestCmd);

//Launch hidden CLI
using (Process p = new Process())
{
    p.StartInfo.FileName = @"C:\Windows\System32\cmd.exe";
    p.StartInfo.RedirectStandardInput = true;
    p.StartInfo.RedirectStandardOutput = true;
    p.StartInfo.RedirectStandardError = true;
    p.StartInfo.UseShellExecute = false;
    p.StartInfo.Arguments = _nodeVarsBatPath;
    p.Start();

    //Execute commands
    using (StreamWriter sw = p.StandardInput)
    {
        if (sw.BaseStream.CanWrite)
        {
            sw.WriteLine(runTestCmd);
        }
    }

    //Parse the various outputs
    output = p.StandardOutput.ReadToEnd();
    error = p.StandardError.ReadToEnd();
    exitCode = p.ExitCode.ToString();    //Always returns 0, think this is because it evaluates success of creating the process, not what happens inside it

    File.WriteAllText(@"D:\TestHarnessServiceLogs\output.txt", output);
    File.WriteAllText(@"D:\TestHarnessServiceLogs\error.txt", error);
    File.WriteAllText(@"D:\TestHarnessServiceLogs\exitCode.txt", exitCode);
}

Newman全球安装在两个环境中,以及下面的一些相关AppSettings(为简洁起见略微修改了名称):

"_newmanDotJsFile": "C:\\Users\\MyName\\AppData\\Roaming\\npm\\node_modules\\newman\\bin\\newman.js",
"_nodeVarsBatPath": "\"C:\\Program Files\\NodeJS\\nodevars.bat\"",
"_nodeExecutablePath": "\"C:\\Program Files\\NodeJS\\node.exe\"",

如何使用相同的命令查找newman模块并从CLI运行,但不能从服务运行?

编辑:运行该服务的用户无法访问该文件,完成后我现在得到以下(显然是基于权限的)错误,我想我知道这是怎么回事。 ..:

fs.js:994
  binding.lstat(pathModule._makeLong(path), statValues);
          ^

Error: EPERM: operation not permitted, lstat 'C:\Users\MyName'
    at Error (native)
    at Object.fs.lstatSync (fs.js:994:11)
    at Object.realpathSync (fs.js:1676:21)
    at toRealPath (module.js:133:13)
    at Function.Module._findPath (module.js:181:22)
    at Function.Module._resolveFilename (module.js:467:25)
    at Function.Module._load (module.js:417:25)
    at Module.runMain (module.js:604:10)
    at run (bootstrap_node.js:393:7)
    at startup (bootstrap_node.js:150:9)

编辑2 :为该服务创建了一个新用户,为该用户安装了newman并为其授予了正确的权限(似乎不太聪明,无法让Network Service访问我的个人资料) - 一切正在运转!

1 个答案:

答案 0 :(得分:2)

听起来这项服务并不像您的用户那样运行?也许这就是为什么它没有在您指定的位置找到该文件。