Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/431.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 从NodeJS调用PowerShell_Javascript_Windows_Node.js_Powershell - Fatal编程技术网

Javascript 从NodeJS调用PowerShell

Javascript 从NodeJS调用PowerShell,javascript,windows,node.js,powershell,Javascript,Windows,Node.js,Powershell,我正在尝试获取Windows上Node.js中文件的所有者。在没有win32api的情况下,我想我应该使用PowerShell命令: powershell -Command "(get-acl test.txt).owner" 这在命令行和批处理文件中都能正常工作,但在Node.jsexec()中挂起: var exec=require('child_process')。exec; exec('powershell-Command'(get acl test.txt).owner“),函数(e

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

powershell -Command "(get-acl test.txt).owner"
这在命令行和批处理文件中都能正常工作,但在Node.js
exec()
中挂起:

var exec=require('child_process')。exec;
exec('powershell-Command'(get acl test.txt).owner“),函数(err,sysout,syserr){
console.dir(sysout);
});
PowerShell进程似乎只是启动,从未终止

有人有:

  • 关于命令为什么不会在Node.js中返回的想法,最好是
  • 对于我来说,在Windows上使用Node.js获得文件所有者是一种明智的方法吗

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


    另一个选项是调用
    cmd/c dir/q
    ,但输出很详细。

    我使用了
    child=exec(…);child.stdin.end()并且它工作得很好。谢谢。这个答案对我也有用。非常感谢。一个注意事项,供其他经历过这种情况或对此感到疑惑的人参考。只有Powershell 2及更早版本(2随Windows 7提供)没有stdin.end()调用时才会发生这种情况。对于Powershell 3和更高版本(3随Windows 8和Windows 5随10一起提供),它不是必需的,并且可以按预期工作。