8

I'm attempting to get the owner of a file in Node.js on Windows. In the absence of a win32api, I thought I'd use a PowerShell command:

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():

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.

Does anybody have:

  1. an idea on why the command won't return in Node.js, or preferably
  2. a sane way for me to get a file owner with Node.js on Windows?

1 Answer 1

9

When you are calling Powershell like that you need to the close the input stream. You may want to try using spawn and use stdin.end().

Other option is to call cmd /c dir /q <file> but that output is verbose.

Sign up to request clarification or add additional context in comments.

2 Comments

I used child = exec(...); child.stdin.end(); and it work perfectly. Thanks.
This answer worked for me as well. Thank you. One note for the reference of others experiencing this or wondering about it. This only happens with Powershell 2 and earlier (2 ships with Windows 7) without the stdin.end() call. For Powershell 3 and later (3 ships with Windows 8 and 5 with 10), it's not necessary and works as expected.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.