相关疑难解决方法(0)

在node.js中执行并获取shell命令的输出

在node.js中,我想找到一种获取Unix终端命令输出的方法.有没有办法做到这一点?

function getCommandOutput(commandString){
    // now how can I implement this function?
    // getCommandOutput("ls") should print the terminal output of the shell command "ls"
}
Run Code Online (Sandbox Code Playgroud)

shell command-line-interface node.js

91
推荐指数
6
解决办法
9万
查看次数

如何使用childprocess和nodejs执行Windows powershell命令?

我正在尝试通过 nodejs 脚本运行 powershell 命令。我发现以下两篇文章向我展示了与我想要实现的目标类似的内容:\n使用 Nodejs 执行 Windows 命令\n从 Nodejs 执行 powershell 脚本

\n\n

在按钮单击事件中,我尝试列出当前连接到系统的 USB 设备及其驱动器号(C、D、E 等)。如果我在 powershell 中单独运行该命令,它就可以工作(但我无法让它显示驱动器号)。但是,如果我将它作为脚本的一部分运行,它就不起作用。下面是我的代码:

\n\n
if (process.platform === \'win32\' || process.platform === \'win64\') {\n    exec("powershell.exe",["GET-WMIOBJECT win32_diskdrive | Where { $_.InterfaceType \xe2\x80\x93eq \'USB\' }"], function (err, stdout, stderr) {\n        console.log(err);\n        console.log(stdout);\n        console.log(stderr);\n    });\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

我究竟做错了什么?

\n

javascript windows powershell node.js

5
推荐指数
2
解决办法
2万
查看次数