Javascript Node.js和PowerShell命令/脚本

Javascript Node.js和PowerShell命令/脚本,javascript,html,node.js,Javascript,Html,Node.js,以下是my node.js项目的文件夹结构: MySampleApp\ MySampleApp\Package.json MySampleApp\Server.js MySampleApp\public: index.html - Invoked when server.js starts and shows "Click Me" button fetchprocess.js - check button click and then show in Docu

以下是my node.js项目的文件夹结构:

MySampleApp\
MySampleApp\Package.json
MySampleApp\Server.js
MySampleApp\public:
    index.html - Invoked when server.js starts and shows "Click Me" button
    fetchprocess.js - check button click and then show in DocumentElementID
    user1.ps1 - runs Get-Process command to fetch processes
fetchprocess.js:

console.log('Client-side code running');
const button = document.getElementById('myButton');
button.addEventListener('click', function(e) {
console.log('button was clicked');
document.getElementById('counter').innerHTML = "ClickedNow";

    var ps = new shell({
       executionPolicy: 'bypass',
       noProfile: true
    });

   app.get("/", function (req, res) {
   ps.addCommand("./user.ps1", [ {
   name: 'guid', value: req.params.guid
} ])
    ps.invoke().then(output => {
    res.end(output);  // This is to show the output in web browser
   })
})

});
Index.html:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>Example</title>
  </head>
 <body>
   <p id="counter">Loading button click data.</p>
   <button id="myButton">Click me!</button>
 </body>
 <script src="fetchprocess.js"></script>
</html>

例子
加载按钮点击数据

点击我!
我的目标是在有人单击“ClickMe”按钮时执行“user1.ps1”,然后在HTMLdiv(计数器)中显示数据。当我尝试运行上述代码时,它会抛出一个错误,该错误在fetchprocess.js文件中没有定义

图片:

我将在公用文件夹下有几个脚本,当有人单击web应用程序上的其他按钮时将执行这些脚本。这类项目的最佳文件夹结构是什么


非常感谢

TL;博士您无法在浏览器中执行Node.js代码

看起来你给图书馆发了信息。您正试图在浏览器中执行一些Node.js代码

 <script src="fetchprocess.js"></script>

在这里,您正在浏览器中加载脚本。事实上,浏览器JS代码无法访问平台,例如PowerShell,它根本不是Node.JS。如果需要执行Node.js代码,请使用Node.js可执行文件

UPD: 解决方法可能如下所示

  • 使用Node.js在本地启动HTTP服务器
  • 添加接收到请求后运行PS脚本的端点
  • 单击向服务器发送AJAX请求

  • 但是值得注意的是,它只在Node.js服务器运行的机器上工作。因为无法从浏览器运行PowerShell

    这里的链接解释了如何通过Node.js执行PowerShell脚本,我没有在浏览器中执行Node.js代码。我通过单击按钮使用节点powershell模块调用现有powershell脚本。我认为这是可行的。当我点击“点击我”按钮时,我收到了错误的更新问题。这是不可行的。您不能在浏览器中调用
    节点powershell
    模块,因为此模块应在node.js中执行,而不是在浏览器中执行。这就是为什么您看到
    require
    不是一个已定义的问题,因为浏览器JS代码没有全局函数
    require
    。解决方法可能是创建HTTP服务器,并在客户端调用您时从服务器调用PS1脚本。是的,您想从哪里获取
    shell
    类?浏览器没有定义的
    shell
    。)