C# 无法在.NET core中运行远程powershell命令:未找到受支持的WSMan客户端库

C# 无法在.NET core中运行远程powershell命令:未找到受支持的WSMan客户端库,c#,macos,powershell,.net-core,C#,Macos,Powershell,.net Core,我正在尝试使用powershell在.NET core中执行远程命令。 以下是我尝试连接到powershell的方式: var username = "dwaldkjesfcdw"; SecureString password = new SecureString(); string pwd = "fsdfdsdsfvds"; /* Not Secure! */ pwd.ToCharArray().T

我正在尝试使用powershell在.NET core中执行远程命令。 以下是我尝试连接到powershell的方式:

            var username = "dwaldkjesfcdw";
            SecureString password = new SecureString();
            string pwd = "fsdfdsdsfvds"; /* Not Secure! */
            pwd.ToCharArray().ToList().ForEach(password.AppendChar);
            /* and now : seal the deal */
            password.MakeReadOnly();

            var credentials = new PSCredential(username, password);
            var remoteComputer = new Uri(String.Format("{0}://{1}:3311/wsman", "HTTP", "11.11.111.111"));
            var connection = new WSManConnectionInfo(remoteComputer, null, credentials);

            var runspace = RunspaceFactory.CreateRunspace(connection);
            runspace.Open();

            var powershell = PowerShell.Create();
            powershell.Runspace = runspace;

            powershell.AddScript("$env:ComputerName");

            var result = powershell.Invoke();
但是在
var runspace=RunspaceFactory.CreateRunspace(连接)上

我得到一个例外:

System.Management.Automation.Remoting.PSRemotingTransportException:此参数集需要WSMan,但未找到支持的WSMan客户端库。此系统未安装WSMan或WSMan不可用。-->System.DllNotFoundException:无法加载共享库“libpsrpclient”或其依赖项之一。为了帮助诊断加载问题,请考虑设置DeldyPrimtLoCub环境变量:DLOPEN(LyBubPSRPclipse,1):未找到图像
在System.Management.Automation.Remoting.Client.wsmannavieapi.WSManInitialize(Int32标志、IntPtr和wsManAPIHandle)
在System.Management.Automation.Remoting.Client.WSManClientSessionTransportManager.WSManapidatCommon..ctor()中
---内部异常堆栈跟踪的结束---
在System.Management.Automation.Remoting.Client.WSManClientSessionTransportManager.WSManapidatCommon..ctor()中
位于System.Management.Automation.Remoting.Client.WSManClientSessionTransportManager..ctor(Guid RunspacePoolsInstanceId、WSManConnectionInfo connectionInfo、PSRemotingCryptoHelper cryptoHelper、String sessionName)
位于System.Management.Automation.Runspaces.WSManConnectionInfo.CreateClientSessionTransportManager(Guid实例ID、字符串sessionName、PSRemotingCryptoHelper cryptoHelper)
位于System.Management.Automation.Remoting.ClientRemoteSessionDSHandlerImpl..ctor(ClientRemoteSession会话、PSRemotingCryptoHelper cryptoHelper、RunspaceConnectionInfo connectionInfo、URIDirectionReported uriRedirectionHandler)
位于System.Management.Automation.Remoting.ClientRemoteSessionImpl..ctor(RemoteRunspacePool内部rsPool,URIDirectionReported uriRedirectionHandler)
位于System.Management.Automation.Internal.ClientRunspacePoolDataStructureHandler.CreateClientRemoteSession(RemoteRunspacePoolTernal RSPoolTernal)
位于System.Management.Automation.Internal.ClientRunspacePoolDataStructureHandler..ctor(RemoteRunSpacePoolTernal clientRunspacePool,TypeTable TypeTable)
位于System.Management.Automation.Runspaces.Internal.RemoteRunspacePoolternal.CreateDSHandler(类型表)
位于System.Management.Automation.Runspaces.Internal.RemoteRunspacePoolternal..ctor(Int32 minRunspaces、Int32 maxRunspaces、TypeTable TypeTable、PSHost主机、PSPrimitiveDictionary应用程序参数、RunspaceConnectionInfo connectionInfo、字符串名称)
位于System.Management.Automation.Runspaces.RunspacePool..ctor(Int32 minRunspaces、Int32 maxRunspaces、TypeTable TypeTable、PSHost主机、PSPrimitiveDictionary应用程序参数、RunspaceConnectionInfo connectionInfo、字符串名称)
位于System.Management.Automation.RemoteRunspace..ctor(类型表类型表、RunspaceConnectionInfo连接信息、PSHost主机、PSPrimitiveDictionary应用程序参数、字符串名称、Int32 id)
位于System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace(RunspaceConnectionInfo connectionInfo、PSHost主机、TypeTable TypeTable、PSPrimitiveDictionary应用程序参数、字符串名称)
在System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace(RunspaceConnectionInfo connectionInfo,PSHost host,TypeTable TypeTable)
在System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace(PSHost主机,RunspaceConnectionInfo connectionInfo)
位于System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace(RunspaceConnectionInfo connectionInfo)

在/Users/Services/CalculationService.cs中的Services.Services.CalculationService.ProcessCalculations(Int32 clientId,Int32 calculationId):第90行
中,我在Fedora 33上的.NET Core 3.1中遇到了这种情况。不确定是否有提供此功能的软件包,也不确定它是否适用于macOS,但我发现例外情况在我的情况下是正确的。找不到与PowerShell远程处理协议(PSRP)客户端相关的某些共享库

我已经安装了PowerShell 7.0.3,并确保可以使用WS-Management进行远程操作。起初,我似乎需要安装和。然而,这并没有解决问题

我在PowerShell repo上查找了一些旧的GitHub问题,发现运行时要么找不到
libpsrpclient
共享库对象,要么找不到它的一个依赖项。我发现这些共享库存在于PowerShell安装位置,这可以解释为什么PowerShell本身在远程处理方面没有问题:

  • libpsrpclient.so-可能是客户端库本身
  • libmi.so-omi的依赖项库
  • libssl.so.1.0.0-指向/lib64/libssl.so.10的符号链接(它本身是指向Fedora 33上/lib64/libssl.so.1.0.2o的符号链接)
  • libcrypto.so.1.0.0-指向/lib64/libcrypto.so.10的符号链接(它本身也是指向/lib64/libcrypto.so.1.0.2o的符号链接)
无论如何,这里是Linux上的解决方法:

  • 导航到您的PowerShell目录
    cd/opt/microsoft/powershell/7
  • 将共享库复制到共享库路径中的某个位置(如/usr/lib):
    sudo cp libpsrpclient.so libmi.so libssl.so.1.0.0 libcrypto.so.1.0.0/usr/lib
  • 重新链接/缓存共享库:
    sudo ldconfig-n-v/usr/lib
  • 在这里发现了一些东西