gpt4 book ai didi

javascript - 如何使用子进程和nodejs执行windows powershell命令?

转载 作者:可可西里 更新时间:2023-11-01 14:13:31 37 4
gpt4 key购买 nike

我正在尝试通过 nodejs 脚本运行 powershell 命令。我发现以下两篇文章向我展示了与我想要实现的目标类似的东西: Execute Windows Commands with Nodejs Execute powershell script from nodejs

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

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 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.

关于javascript - 如何使用子进程和nodejs执行windows powershell命令?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36274084/

37 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com