Skip to content

.NET App using Powershell.SDK fails to run, when building as SingeFile #23797

@RobinGFT

Description

@RobinGFT

Prerequisites

Steps to reproduce

I have a .NET 8 Application executing a lot of Powershell commands, but when I build it as a SingleFile it throws an exception.
Issue is already documented in #13540

I also opened an issue in the dotnet repo, which is to find under: dotnet/runtime #102193. There is already a very helpful comment which shows a possible way fixing the issue: Comment


<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>WinExe</OutputType>
    <TargetFramework>net8.0-windows</TargetFramework>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.PowerShell.SDK" Version="7.4.2"  />
  </ItemGroup>
</Project>
using System;
using System.Management.Automation;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            String cmd = "New-Item -Force -Type Directory -Path '%userprofile%\test123'";
             PowerShell ps1 = PowerShell.Create().AddScript(cmd);
             ps1.Invoke();
        }
    }
}

dotnet publish -r win-x64 --self-contained false /p:PublishSingleFile=true

Expected behavior

Publish and run successfully.

Actual behavior

Publish works, but application doesn' t run. At the first execution of a powershell script, following exception is thrown:

ERROR: 2024-05-14T12:33:51.8318820Z - Value cannot be null. (Parameter 'path1')
   at System.ArgumentNullException.Throw(String paramName)
   at System.IO.Path.Combine(String path1, String path2)
   at System.Management.Automation.PSSnapInReader.ReadEnginePSSnapIns()
   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.CoreInvoke[TOutput](IEnumerable input, PSDataCollection`1 output, PSInvocationSettings settings)
   at System.Management.Automation.PowerShell.Invoke(IEnumerable input, PSInvocationSettings settings)
   at System.Management.Automation.PowerShell.Invoke()

Error details

No response

Environment data

  • .NET 8
  • Win10-x64
  • PowerShell.SDK 7.4.2

Visuals

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    In-PRIndicates that a PR is out for the issue

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions