简体   繁体   English

从NodeJS调用PowerShell

[英]Calling PowerShell from NodeJS

I'm attempting to get the owner of a file in Node.js on Windows. 我试图在Windows上获取Node.js中文件的所有者。 In the absence of a win32api, I thought I'd use a PowerShell command: 在没有win32api的情况下,我想我会使用PowerShell命令:

powershell -Command "(get-acl test.txt).owner"

This works perfectly from the command-line and from a batch file, but just hangs with Node.js exec() : 这可以从命令行和批处理文件中完美地工作,但只是挂起Node.js exec()

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

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

The PowerShell process just appears to start and never terminate. PowerShell进程似乎刚开始并且永不终止。

Does anybody have: 有人有:

  1. an idea on why the command won't return in Node.js, or preferably 关于为什么命令不会在Node.js中返回的想法,或者最好
  2. a sane way for me to get a file owner with Node.js on Windows? 一个理智的方式让我在Windows上获得Node.js的文件所有者?

When you are calling Powershell like that you need to the close the input stream. 当您像这样调用Powershell时,您需要关闭输入流。 You may want to try using spawn and use stdin.end() . 您可能想尝试使用spawn并使用stdin.end()

Other option is to call cmd /c dir /q <file> but that output is verbose. 其他选项是调用cmd /c dir /q <file>但该输出是详细的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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