简体   繁体   中英

C# - Command runs from command prompt but not from service

I have a C# (ASP Core) service running on Windows Server 2012 R2 that executes Newman test suites via the command line. The command the service executes works perfectly when run directly in the command prompt, but is not working from the service. To add insult to injury, the very same command DOES work from the service running locally on my dev machine (Windows 10 Pro). I am certain I am running the same command in all instances, as the service outputs the CLI's StandardOutput into a file, the contents of which I paste straight into the command prompt.

EDIT : The service is hosted on IIS.

The error I receive:

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

The command I'm running (all paths are the same in every instance):

"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# to build and run command:

//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 is installed globally in both environments, and some relevant AppSettings below (names modified slightly for brevity):

"_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\"",

How can an identical command find the newman module and run fine from the CLI but not from the service?

EDIT : The user the service is running under couldn't access the file, having done that I now get the following (obviously permissions based) error instead, think I know where this is going...:

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)

EDIT 2 : Created a new user for the service to run under, installed newman for that user and gave it the right permissions (didn't seem clever to give Network Service access to my profile) - all is now working!

It sounds like the service doesn't run as your user? Perhaps that's why it doesn't find the file in that location you specified.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM