Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
5 tasks done
RobinGFT opened this issue May 14, 2024 · 2 comments
Open
5 tasks done
Labels
Needs-Triage The issue is new and needs to be triaged by a work group.

Comments

@RobinGFT
Copy link

RobinGFT commented May 14, 2024

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

@RobinGFT RobinGFT added the Needs-Triage The issue is new and needs to be triaged by a work group. label May 14, 2024
@samuel-weber-nc
Copy link

Having exactly the same issue!

@kasini3000
Copy link

dotnet publish MyPowerShellApp -r linux-x64 -c release --self-contained true /p:PublishSingleFile=true
dotnet publish MyPowerShellApp -r linux-x64 -c release --self-contained false /p:PublishSingleFile=true
Having exactly the same issue!


normal without "/p:PublishSingleFile=true"


Environment data

.NET 8
linux-x64
PowerShell.SDK 7.4.2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Needs-Triage The issue is new and needs to be triaged by a work group.
Projects
None yet
Development

No branches or pull requests

3 participants