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

NET8开发PowerShell CmdLet遇程序集加载失败问题求助

解决.NET 8开发PowerShell CmdLet时System.Management.Automation加载失败问题

可能的原因及修复步骤

1. 确认PowerShell版本兼容性

  • 若针对Windows PowerShell 5.1开发:.NET 8与其依赖的.NET Framework 4.x不兼容,需将项目目标框架改为.NET Framework 4.7.2+,并替换NuGet包为Microsoft.PowerShell.5.ReferenceAssemblies,移除Microsoft.PowerShell.SDK。
  • 若针对PowerShell 7.x开发:必须使用PowerShell 7.4及以上版本(它是首个基于.NET 8的PowerShell版本),低版本无法加载.NET 8编译的程序集。

2. 调整项目配置

  • 移除PowerShellStandard.Library包:.NET 8 + Microsoft.PowerShell.SDK已包含所需依赖,该标准库仅用于旧框架兼容场景。
  • 确保.csproj文件配置正确(以PowerShell 7.4 + .NET 8为例):
    <Project Sdk="Microsoft.NET.Sdk">
      <PropertyGroup>
        <TargetFramework>net8.0</TargetFramework>
        <ImplicitUsings>enable</ImplicitUsings>
        <Nullable>enable</Nullable>
        <PowerShellVersion>7.4</PowerShellVersion>
      </PropertyGroup>
      <ItemGroup>
        <PackageReference Include="Microsoft.PowerShell.SDK" Version="7.4.0" />
      </ItemGroup>
    </Project>
    

3. 验证依赖与运行环境

  • 执行dotnet list package确认Microsoft.PowerShell.SDK版本与目标PowerShell版本匹配(如7.4对应SDK 7.4.0)。
  • 运行dotnet --list-runtimes检查是否已安装**.NET 8 Runtime**(跨平台)或**.NET 8 Desktop Runtime**(Windows),PowerShell 7.4+依赖该运行时才能加载程序集。

4. 确保用正确的PowerShell终端加载模块

  • 若同时安装了Windows PowerShell和PowerShell 7,需打开PowerShell 7终端(蓝底白字图标,标题含PowerShell 7),而非Windows PowerShell(白底蓝字)。
  • 直接从PowerShell 7安装路径启动终端(通常为C:\Program Files\PowerShell\7\pwsh.exe),再执行Import-Module .\PowerShellCmdLet.dll。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.01 10:12:40