如何使用 childprocess 和 nodejs 执行 windows powershell 命令? [英] How to execute windows powershell command using childprocess and nodejs?
问题描述
我正在尝试通过 nodejs 脚本运行 powershell 命令.我发现以下两篇文章向我展示了类似于我想要实现的内容:使用 Nodejs 执行 Windows 命令从 nodejs 执行 powershell 脚本
I am trying to run a powershell command through a nodejs script. I have found the following two articles which have shown me something similar to what I am trying to acheive: Execute Windows Commands with Nodejs Execute powershell script from nodejs
在按钮单击事件中,我试图列出当前连接到系统的 USB 设备及其驱动器号(C、D、E 等).如果我自己在 powershell 中运行该命令,它就可以工作(尽管我无法让它显示驱动器号).但是,如果我将它作为脚本的一部分运行,则它不起作用.下面是我的代码:
On a button click event, I am trying to list the usb devices currently attached to the system along with its Drive Letter (C, D, E etc). If I run the command in the powershell on its own, it works (I am unable to get it to display the drive letter though). However, if I run it as part of my script it does not work. Below is my code:
if (process.platform === 'win32' || process.platform === 'win64') {
exec("powershell.exe",["GET-WMIOBJECT win32_diskdrive | Where { $_.InterfaceType –eq 'USB' }"], function (err, stdout, stderr) {
console.log(err);
console.log(stdout);
console.log(stderr);
});
}
我做错了什么?
推荐答案
你可以使用 Node-PowerShell.
Node-PowerShell 利用当今技术世界中存在的两个最简单、有效和简单的工具.一方面,NodeJS 在 JavaScript 领域掀起了一场革命,另一方面,PowerShell 最近推出了最初的开源、跨平台版本,并将它们连接在一起,让您能够创建您被要求的任何解决方案,无论您是程序员、IT 人员还是 DevOps 人员.
Node-PowerShell taking advantage of two of the simplest, effective and easy tools that exist in the today technology world. On the one hand, NodeJS which made a revolution in the world of javascript, and on the other hand, PowerShell which recently came out with an initial open-source, cross-platform version, and by connecting them together, gives you the power to create any solution you were asked to, no matter if you are a programmer, an IT or a DevOps guy.
这篇关于如何使用 childprocess 和 nodejs 执行 windows powershell 命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!