You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用node-powershell在Angular展示VM磁盘空间遇类型错误求助

解决node-powershell在Angular中PowerShellOptions类型错误的问题

错误原因

你遇到的TypeScript错误是因为新版本的node-powershell模块已移除PowerShellOptions中的executionPolicy和noProfile属性,这两个配置现在需要通过shellArgs数组传递PowerShell的启动参数。

解决方案

修改HomeComponent.ts中PowerShell实例化的配置代码,将原来的独立属性改为通过shellArgs传递命令行参数:

async getDiskSpace() {
  // 替换原有PowerShell配置
  const ps = new PowerShell({
    shellArgs: ['-ExecutionPolicy', 'Bypass', '-NoProfile']
  });

  const command = PowerShell.command`
  $vms = 'vm1', 'vm2', 'vm3'
  Get-CimInstance -Class win32_logicaldisk -Filter "DeviceID='C:'" -ComputerName $vms |
      Format-Table @(
          'PSComputerName'
          'DeviceId'
          @{n='Size (GB)'; e={[math]::Round($_.Size/1GB)}} 
          @{n='FreeSpace (GB)'; e={[math]::Round($_.FreeSpace/1GB)}} 
  )
  `;

  const output = await ps.invoke(command);
  ps.dispose();
  
  this.diskSpace = output.raw;
}

补充说明

  1. 参数对应关系:

    • -ExecutionPolicy Bypass 完全等价于原配置的executionPolicy: "Bypass"
    • -NoProfile 完全等价于原配置的noProfile: true
      这些是PowerShell原生启动参数,通过shellArgs传递后,执行效果和旧版本配置一致。
  2. Angular环境限制:
    node-powershell是Node.js模块,无法直接在纯浏览器环境的Angular应用中运行。如果你的项目是纯前端Angular,需要将读取磁盘空间的逻辑抽离到Node.js后端服务,前端通过HTTP请求获取结果;如果是Electron桌面应用,则可直接使用该模块。

内容的提问来源于stack exchange,提问作者JBC

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.12 08:20:31