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

Deno调用PowerShell执行Expand-Archive遇模块加载及权限问题求助

问题描述

在PowerShell 7.5.0-rc.1中直接执行Expand-Archive -path .\test.zip -DestinationPath .可正常解压,但通过Deno脚本调用该命令时,出现两种错误:

  1. 提示无法加载Microsoft.PowerShell.Archive模块;
  2. 手动添加Import-Module Microsoft.PowerShell.Archive后,又报错系统禁用脚本无法加载模块。
    当前PowerShell执行策略:Process和LocalMachine级别为Unrestricted,其余级别为Undefined。
解决思路
  • 显式指定PowerShell执行策略
    Deno启动的PowerShell会话可能不继承当前终端的执行策略配置,需要在调用时强制指定会话级别的执行策略:

    powershell.exe -ExecutionPolicy Unrestricted -Command "Expand-Archive -path .\test.zip -DestinationPath ."
    

    或者临时为当前会话设置策略后再执行解压:

    powershell.exe -Command "Set-ExecutionPolicy Unrestricted -Scope Process -Force; Expand-Archive -path .\test.zip -DestinationPath ."
    
  • 直接加载模块的完整路径
    先在正常PowerShell中执行(Get-Module Microsoft.PowerShell.Archive -ListAvailable).Path获取模块的绝对路径,然后在Deno的命令中直接引用该路径加载模块:

    powershell.exe -ExecutionPolicy Unrestricted -Command "Import-Module 'C:\Program Files\PowerShell\7\Modules\Microsoft.PowerShell.Archive\Microsoft.PowerShell.Archive.psd1'; Expand-Archive -path .\test.zip -DestinationPath ."
    

    (替换路径为你实际获取到的模块路径)

  • 改用Deno原生解压能力
    完全跳过PowerShell依赖,直接用Deno的zip处理能力实现,示例代码:

    // 使用Deno第三方zip库示例(确保已通过deno install安装相关依赖)
    import { ZipReader } from "zip";
    
    const zipFile = await Deno.open("./test.zip");
    const reader = new ZipReader(zipFile);
    const entries = await reader.getEntries();
    
    for (const entry of entries) {
      if (entry.directory) {
        await Deno.mkdir(`./${entry.filename}`, { recursive: true });
        continue;
      }
      const outputFile = await Deno.create(`./${entry.filename}`);
      await entry.getData(outputFile);
      outputFile.close();
    }
    
    reader.close();
    zipFile.close();
    

    这种方式避免了PowerShell环境的兼容性问题,更适合Deno脚本场景。

  • 检查Deno的运行权限与会话上下文
    尝试以管理员身份启动Deno脚本,避免因权限不足无法读取PowerShell系统模块;同时执行以下命令确认Deno调用的PowerShell会话实际生效的执行策略:

    powershell.exe -Command "Get-ExecutionPolicy -List"
    

    如果输出中CurrentUser级别为Restricted,需执行Set-ExecutionPolicy Unrestricted -Scope CurrentUser -Force调整。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.15 01:30:11