C# 使用c运行PowerShell“在Docker中,获取异常”;无法加载共享库';libpsrpclient'&引用;

C# 使用c运行PowerShell“在Docker中,获取异常”;无法加载共享库';libpsrpclient'&引用;,c#,docker,powershell,C#,Docker,Powershell,我是Docker的新手,正在将我的一些应用程序从旧版本的Dotnet Core迁移到5.0 在Docker中构建和运行将返回以下错误,在Windows环境中运行时不会出错 System.Management.Automation.CmdletInvocationException:此参数集需要WSMan,但未找到支持的WSMan客户端库。此系统未安装WSMan或WSMan不可用。 --->System.Management.Automation.Remoting.PSRemotingTransp

我是Docker的新手,正在将我的一些应用程序从旧版本的Dotnet Core迁移到5.0

在Docker中构建和运行将返回以下错误,在Windows环境中运行时不会出错

System.Management.Automation.CmdletInvocationException:此参数集需要WSMan,但未找到支持的WSMan客户端库。此系统未安装WSMan或WSMan不可用。 --->System.Management.Automation.Remoting.PSRemotingTransportException:此参数集需要WSMan,但未找到支持的WSMan客户端库。此系统未安装WSMan或WSMan不可用。 --->System.DllNotFoundException:无法加载共享库“libpsrpclient”或其依赖项之一。为了帮助诊断加载问题,请考虑设置LDYDebug环境变量:LyBubPSRPcli客:无法打开共享对象文件:没有这样的文件或目录 在System.Management.Automation.Remoting.Client.wsmannavieapi.WSManInitialize(Int32标志、IntPtr和wsManAPIHandle) 在System.Management.Automation.Remoting.Client.WSManClientSessionTransportManager.WSManapidatCommon..ctor()中

代码如下

我的测试控制台应用程序项目(保持非常简单)

验证正常运行的C代码

public void TestGetUser()
{

    try
    {

        var connectionUri = "https://outlook.office365.com/PowerShell-LiveID/";
        
        var secpassword = new SecureString();
        
        var password = "P@ssWordGoesHere";
        
        var userName = "userName@tennant.onmicrosoft.com";
        
        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", "someUser@mydomain.com");
        
        powershell.Commands = getUserCmd;
        Collection<PSObject> users = powershell.Invoke();
        
        foreach (PSObject outputItem in users)
        {
          Console.WriteLine(outputItem);
        }
    }
    catch (Exception ex)
    {
    
    Console.WriteLine(ex);
    
    }

}
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"]
public void TestGetUser()
{

    try
    {

        var connectionUri = "https://outlook.office365.com/PowerShell-LiveID/";
        
        var secpassword = new SecureString();
        
        var password = "P@ssWordGoesHere";
        
        var userName = "userName@tennant.onmicrosoft.com";
        
        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", "someUser@mydomain.com");
        
        powershell.Commands = getUserCmd;
        Collection<PSObject> users = powershell.Invoke();
        
        foreach (PSObject outputItem in users)
        {
          Console.WriteLine(outputItem);
        }
    }
    catch (Exception ex)
    {
    
    Console.WriteLine(ex);
    
    }

}
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"]