从NodeJS调用PowerShell

tjd*_*ett 8 javascript windows powershell node.js

我试图在Windows上获取Node.js中文件的所有者.在没有win32api的情况下,我想我会使用PowerShell命令:

powershell -Command "(get-acl test.txt).owner"
Run Code Online (Sandbox Code Playgroud)

这可以从命令行和批处理文件中完美地工作,但只是挂起Node.js exec():

var exec = require('child_process').exec;

exec('powershell -Command "(get-acl test.txt).owner"', function(err,sysout,syserr) {
    console.dir(sysout);
});
Run Code Online (Sandbox Code Playgroud)

PowerShell进程似乎刚开始并且永不终止.

有人有:

  1. 关于为什么命令不会在Node.js中返回的想法,或者最好
  2. 一个理智的方式让我在Windows上获得Node.js的文件所有者?

man*_*lds 9

当您像这样调用Powershell时,您需要关闭输入流.您可能想尝试使用spawn并使用它stdin.end().

其他选项是调用,cmd /c dir /q <file>但输出是详细的.