Running PowerShell Using c# within Docker, getting exception "Unable to load shared library ‘libpsrpclient’"

I’m new to Docker and was making progress moving some of my Apps from older versions Dotnet Core to 5.0

Building and running within Docker returns the below error, Runs without error in Windows environment.

System.Management.Automation.CmdletInvocationException: This parameter set requires WSMan, and no supported WSMan client library was found. WSMan is either not installed or unavailable for this system.
—> System.Management.Automation.Remoting.PSRemotingTransportException: This parameter set requires WSMan, and no supported WSMan client library was found. WSMan is either not installed or unavailable for this system.
—> System.DllNotFoundException: Unable to load shared library ‘libpsrpclient’ or one of its dependencies. In order to help diagnose loading problems, consider setting the LD_DEBUG environment variable: liblibpsrpclient: cannot open shared object file: No such file or directory
at System.Management.Automation.Remoting.Client.WSManNativeApi.WSManInitialize(Int32 flags, IntPtr& wsManAPIHandle)
at System.Management.Automation.Remoting.Client.WSManClientSessionTransportManager.WSManAPIDataCommon..ctor()

Code follows

My Test Console App Project (Keeping it very simple)

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

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net5.0</TargetFramework>
  </PropertyGroup>

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

</Project>

My first version of my Docker file (I probably brutalized this, sorry to offend those experts out there)

FROM mcr.microsoft.com/dotnet/sdk:5.0 AS base
WORKDIR /app
COPY . .

FROM mcr.microsoft.com/dotnet/sdk:5.0 AS build
WORKDIR /src
COPY *.sln ./
COPY nuget.config ./
COPY PowerShell.Office365.TestClient/*.csproj ./PowerShell.Office365.TestClient/


RUN dotnet restore
COPY . .
WORKDIR /src/PowerShell.Office365.TestClient
RUN dotnet build -c Release -o /app --no-restore

FROM build AS publish
RUN dotnet publish -c Release -o /app


FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "PowerShell.Office365.TestClient.dll"]

C# code to verify proper functioning

public void TestGetUser()
{

    try
    {

        var connectionUri = "https://outlook.office365.com/PowerShell-LiveID/";
        
        var secpassword = new SecureString();
        
        var password = "[email protected]";
        
        var userName = "[email protected]";
        
        foreach (var character in password)
        {
           secpassword.AppendChar(character);
        }
        
        var credentials = new PSCredential(userName, secpassword);
        
        
        PSObject sessionHolder = null;
        
        using var powershell = System.Management.Automation.PowerShell.Create();
        
        
        var connectionInfo = new WSManConnectionInfo(new Uri("https://ps.outlook.com/PowerShell-LiveID?PSVersion=2.0"),
           "http://schemas.microsoft.com/powershell/Microsoft.Exchange", credentials)
        {
            AuthenticationMechanism = AuthenticationMechanism.Basic,
            SkipCACheck = true,
            SkipCNCheck = true,
            MaximumConnectionRedirectionCount = 4
        };
        
        using var runspace = RunspaceFactory.CreateRunspace(connectionInfo);
        runspace.Open();
        
        powershell.Runspace = runspace;
        
        
        var getUserCmd = new PSCommand();
        getUserCmd.AddCommand("Get-User");
        getUserCmd.AddParameter("Identity", "[email protected]");
        
        powershell.Commands = getUserCmd;
        Collection<PSObject> users = powershell.Invoke();
        
        foreach (PSObject outputItem in users)
        {
          Console.WriteLine(outputItem);
        }
    }
    catch (Exception ex)
    {
    
    Console.WriteLine(ex);
    
    }

}

Second iteration, I did have some success by modifying my Docker file to the following. Is there a better cleaner approach?

FROM mcr.microsoft.com/dotnet/sdk:5.0 AS base
WORKDIR /app
COPY . .

#avoid the System.Management.Automation.CmdletInvocationException exception
RUN wget http://archive.ubuntu.com/ubuntu/pool/main/o/openssl1.0/libssl1.0.0_1.0.2n-1ubuntu5.4_amd64.deb
RUN dpkg -i libssl1.0.0_1.0.2n-1ubuntu5.4_amd64.deb


FROM mcr.microsoft.com/dotnet/sdk:5.0 AS build
WORKDIR /src
COPY *.sln ./
COPY nuget.config ./
COPY PowerShell.Office365.TestClient/*.csproj ./PowerShell.Office365.TestClient/


RUN dotnet restore
COPY . .
WORKDIR /src/PowerShell.Office365.TestClient
RUN dotnet build -c Release -o /app --no-restore

FROM build AS publish
RUN dotnet publish -c Release -o /app


FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "PowerShell.Office365.TestClient.dll"]

Source: Docker Questions