1

I have a C# project that creates a PowerShell instance that invokes a script. However, the published output does not run.

My project definition is:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net7.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.PowerShell.SDK" Version="7.3.4" />
  </ItemGroup>

  <ItemGroup>
    <None Update="Script.ps1">
      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
    </None>
  </ItemGroup>

</Project>

My MRE c# code is:

namespace ConsoleApp1;

using System.Management.Automation.Runspaces;
using System.Management.Automation;

internal class Program
{
    private static async Task Main(string[] args)
    {
        var initialSessionState = InitialSessionState.CreateDefault();
        initialSessionState.LanguageMode = PSLanguageMode.FullLanguage;

        using var runSpace = RunspaceFactory.CreateRunspace(initialSessionState);
        using var powerShell = PowerShell.Create();

        runSpace.Open();
        powerShell.Runspace = runSpace;

        var script = await File.ReadAllTextAsync(@"Script.ps1");
        powerShell.AddScript(script);

        await powerShell.InvokeAsync().ConfigureAwait(false);
    }
}

The debug build runs fine (the packages are all in the local Nuget cache, but the published output is missing Microsoft.Management.Infrastructure.dll and it fails to execute.

My publish command is:

dotnet^
  publish^
  ConsoleApp1.csproj^
  --self-contained^
  --configuration Release^
  --runtime win-x64^
  --output pub

Does anyone know what I am doing incorrectly? According to Choosing the right PowerShell NuGet package for your .NET project, I am using the correct NuGet reference.

Ritmo2k
  • 993
  • 6
  • 17
  • I had to add, at least the folloing 3 dependencies, `dotnet add package System.Management.Automation.Runspaces --version 7.3.4 dotnet add package Microsoft.PowerShell.Commands.Management --version 7.3.4 dotnet add package Microsoft.WSMan.Management --version 7.3.4 ` before I got: `Exception has occurred: CLR/System.IO.FileNotFoundException` (because it could not find "Script1.ps1") – Luuk Apr 16 '23 at 14:23
  • Ok, this seems to solve that in 1 run: `dotnet add package Microsoft.PowerShell.SDK --version 7.3.4` – Luuk Apr 16 '23 at 15:05
  • But how did you check that "the published output does not run" ? – Luuk Apr 16 '23 at 15:06
  • Without specifying "win10-x64" , and having "[console]::beep(2000,500)" in the file Script.ps1, the application produces a BEEP. – Luuk Apr 16 '23 at 15:13
  • The debug build works as VS locates the missing libraries in the NuGet cache, but the console program cannot run when published with an appropriate RID. – Ritmo2k Apr 16 '23 at 16:10
  • Your "NuGet cache" might have different contents compare to the cache on my computer. – Luuk Apr 16 '23 at 17:27

1 Answers1

1

Found the answer in Running Powershell from .Net Core - Could not load file or assembly Microsoft.Management.Infrastructure, I was using a non-version specific RID. Updating my publish command to use win10-x64 worked.

Ritmo2k
  • 993
  • 6
  • 17