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

未启用IncludeAllContentForSelfExtract时单文件发布EXE运行PowerShell失败

PowerShell SDK 单EXE发布运行异常

环境信息

  • 操作系统:Windows 10(已安装最新系统补丁)
  • PowerShell SDK版本:7.2.4
  • 项目框架:net6.0 控制台应用

复现代码

using System.Management.Automation;

Console.WriteLine("Test PowerShell Runner!");

var psInstance = PowerShell.Create();

psInstance.AddScript("(Get-Host).Name");

Console.WriteLine("Before Invoke.");
var returnObj = psInstance.Invoke();
Console.WriteLine("After Invoke.");

Console.WriteLine("Name: " + returnObj.First().BaseObject);

psInstance.Runspace?.Close();

Console.WriteLine("Was successful run");

项目配置(.csproj)

<Project Sdk="Microsoft.NET.Sdk">
    <PropertyGroup>
        <OutputType>Exe</OutputType>
        <TargetFramework>net6.0</TargetFramework>
        <ImplicitUsings>enable</ImplicitUsings>
        <Nullable>enable</Nullable>
    </PropertyGroup>
    <ItemGroup>
      <PackageReference Include="Microsoft.PowerShell.SDK" Version="7.2.4" />
    </ItemGroup>
</Project>

发布相关说明

  • Rider、Visual Studio 下的默认单文件发布配置均可复现问题
  • 出于代码逻辑保护需求,无法启用IncludeAllContentForSelfExtract配置(已测试开启该配置后程序可正常运行)
  • IDE内调试、非单文件模式直接运行时程序执行正常,仅单文件发布版本运行失败
  • 已尝试将PowerShell相关依赖dll放置在发布EXE同级目录,问题未解决

运行报错信息

C:\Projects\PSConsoleRunner\PSConsoleRunner\bin\Release\net6.0\publish>PSConsoleRunner.exe
Test PowerShell Runner!
Before Invoke.
Unhandled exception. System.TypeInitializationException: The type initializer for 'System.Management.Automation.ExperimentalFeature' threw an exception.
 ---> System.TypeInitializationException: The type initializer for 'System.Management.Automation.Configuration.PowerShellConfig' threw an exception.
 ---> System.ArgumentNullException: Value cannot be null. (Parameter 'path1')
   at System.IO.Path.Combine(String path1, String path2)
   at System.Management.Automation.Configuration.PowerShellConfig..ctor()
   at System.Management.Automation.Configuration.PowerShellConfig..cctor()
   --- End of inner exception stack trace ---
   at System.Management.Automation.ExperimentalFeature..cctor()
   --- End of inner exception stack trace ---
   at System.Management.Automation.Runspaces.InitialSessionState.AddVariables(IEnumerable`1 variables)
   at System.Management.Automation.Runspaces.InitialSessionState.CreateDefault()
   at System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace(PSHost host)
   at System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace()
   at System.Management.Automation.PowerShell.Worker.CreateRunspaceIfNeededAndDoWork(Runspace rsToUse, Boolean isSync)
   at System.Management.Automation.PowerShell.CoreInvokeHelper[TInput,TOutput](PSDataCollection`1 input, PSDataCollection`1 output, PSInvocationSettings settings)
   at System.Management.Automation.PowerShell.CoreInvoke[TInput,TOutput](PSDataCollection`1 input, PSDataCollection`1 output, PSInvocationSettings settings)
   at System.Management.Automation.PowerShell.Invoke(IEnumerable input, PSInvocationSettings settings)
   at System.Management.Automation.PowerShell.Invoke()
   at Program.<Main>$(String[] args) in C:\Projects\PSConsoleRunner\PSConsoleRunner\Program.cs:line 10

解决方案

该异常由PowerShell SDK 7.2.x版本对.NET单文件部署模式兼容缺陷导致:SDK初始化配置模块时无法正确获取PSHOME路径,向Path.Combine传入null参数触发空异常。可通过以下任意一种方式修复:

  • 升级PowerShell SDK至7.3.0及以上版本,官方已在新版本中修复该单文件部署兼容问题
  • 若需保留7.2.4版本,在创建PowerShell实例前手动指定PSHOME路径,代码示例:
// 手动指定PSHOME为程序运行根目录
Environment.SetEnvironmentVariable("PSHOME", AppContext.BaseDirectory);
var psInstance = PowerShell.Create();
  • 发布时将PowerShell SDK附带的配置文件、资源文件全部输出到EXE同级目录,确保SDK初始化时可正常读取配置与依赖资源

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.30 15:21:23