1

I am trying to execute a remote command in .NET Core using Powershell. This is how I am trying to connect to 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();

However, this line...

var runspace = RunspaceFactory.CreateRunspace(connection);

...throws this exception...

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 DYLD_PRINT_LIBRARIES environment variable: dlopen(liblibpsrpclient, 1): image not found
    at System.Management.Automation.Remoting.Client.WSManNativeApi.WSManInitialize(Int32 flags, IntPtr& wsManAPIHandle)
    at System.Management.Automation.Remoting.Client.WSManClientSessionTransportManager.WSManAPIDataCommon..ctor()
    --- End of inner exception stack trace ---
    at System.Management.Automation.Remoting.Client.WSManClientSessionTransportManager.WSManAPIDataCommon..ctor()
    at System.Management.Automation.Remoting.Client.WSManClientSessionTransportManager..ctor(Guid runspacePoolInstanceId, WSManConnectionInfo connectionInfo, PSRemotingCryptoHelper cryptoHelper, String sessionName)
    at System.Management.Automation.Runspaces.WSManConnectionInfo.CreateClientSessionTransportManager(Guid instanceId, String sessionName, PSRemotingCryptoHelper cryptoHelper)
    at System.Management.Automation.Remoting.ClientRemoteSessionDSHandlerImpl..ctor(ClientRemoteSession session, PSRemotingCryptoHelper cryptoHelper, RunspaceConnectionInfo connectionInfo, URIDirectionReported uriRedirectionHandler)
    at System.Management.Automation.Remoting.ClientRemoteSessionImpl..ctor(RemoteRunspacePoolInternal rsPool, URIDirectionReported uriRedirectionHandler)
    at System.Management.Automation.Internal.ClientRunspacePoolDataStructureHandler.CreateClientRemoteSession(RemoteRunspacePoolInternal rsPoolInternal)
    at System.Management.Automation.Internal.ClientRunspacePoolDataStructureHandler..ctor(RemoteRunspacePoolInternal clientRunspacePool, TypeTable typeTable)
    at System.Management.Automation.Runspaces.Internal.RemoteRunspacePoolInternal.CreateDSHandler(TypeTable typeTable)
    at System.Management.Automation.Runspaces.Internal.RemoteRunspacePoolInternal..ctor(Int32 minRunspaces, Int32 maxRunspaces, TypeTable typeTable, PSHost host, PSPrimitiveDictionary applicationArguments, RunspaceConnectionInfo connectionInfo, String name)
    at System.Management.Automation.Runspaces.RunspacePool..ctor(Int32 minRunspaces, Int32 maxRunspaces, TypeTable typeTable, PSHost host, PSPrimitiveDictionary applicationArguments, RunspaceConnectionInfo connectionInfo, String name)
    at System.Management.Automation.RemoteRunspace..ctor(TypeTable typeTable, RunspaceConnectionInfo connectionInfo, PSHost host, PSPrimitiveDictionary applicationArguments, String name, Int32 id)
    at System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace(RunspaceConnectionInfo connectionInfo, PSHost host, TypeTable typeTable, PSPrimitiveDictionary applicationArguments, String name)
    at System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace(RunspaceConnectionInfo connectionInfo, PSHost host, TypeTable typeTable)
    at System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace(PSHost host, RunspaceConnectionInfo connectionInfo)
    at System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace(RunspaceConnectionInfo connectionInfo)
    at Services.Services.CalculationService.ProcessCalculations(Int32 clientId, Int32 calculationId) in /Users/Services/CalculationService.cs:line 90

Here is my .csproj file:

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

    <PropertyGroup>
        <TargetFramework>netcoreapp2.2</TargetFramework>
    </PropertyGroup>

    <ItemGroup>
        <PackageReference Include="Microsoft.PowerShell.Commands.Diagnostics" Version="6.1.1" />
        <PackageReference Include="Microsoft.PowerShell.SDK" Version="6.1.1" />
        <PackageReference Include="Microsoft.WSMan.Management" Version="6.1.1" />
        <PackageReference Include="System.Management.Automation" Version="6.1.1" />
        <PackageReference Include="WindowsAzure.Storage" Version="8.1.4" />
    </ItemGroup>

    <ItemGroup>
    </ItemGroup>

</Project>

I am running on macOS but the app will run on Azure.

Is the problem with the version of nuget packages? I can't find what I am missing. Or is the problem on the remote computer?

2
  • Found something here github.com/PowerShell/PowerShell/issues/8548 Commented May 27, 2020 at 8:36
  • Though the code acknowledges /* Not Secure! */, it's worth pointing out that pwd.ToCharArray() — which is unnecessary because pwd is already an IEnumerable<char> on which ToList() can be called — creates another copy of the password characters in memory and .ToList() creates yet another, all just to allow using List<>.ForEach() instead of foreach. Commented Jul 12, 2022 at 19:23

1 Answer 1

3

This was occurring for me with .NET Core 3.1 on Fedora 33. Not sure if there is a package that provides this, and I'm not sure if it applies to macOS either, but I found that the exception is correct in my case. It cannot find some shared libraries related to the PowerShell Remoting Protocol (PSRP) client.

I had already installed PowerShell 7.0.3, and made sure that I could actually remote using WS Management. At first, it seemed that I needed to install the PSRP server and the Open Management Infrastructure (OMI) library. However, that didn't resolve the issue.

Poking around some old GitHub issues on the PowerShell repo, I got some clues that the runtime either couldn't find the libpsrpclient shared library object or one of its dependencies. I discovered that these shared libraries existed in the PowerShell installation location, which would explain why PowerShell itself had no problem remoting:

  • libpsrpclient.so - presumably the client library itself
  • libmi.so - a dependency library for omi
  • libssl.so.1.0.0 - a symlink to /lib64/libssl.so.10 (which is itself a symlink to /lib64/libssl.so.1.0.2o on Fedora 33)
  • libcrypto.so.1.0.0 - a symlink to /lib64/libcrypto.so.10 (which again is itself a symlink to /lib64/libcrypto.so.1.0.2o)

Anyway, here's the workaround on Linux:

  1. Navigate to your PowerShell directory
    cd /opt/microsoft/powershell/7
  2. Copy (or symlink) the shared libraries to a location in your shared library path (like /usr/lib):
    sudo cp libpsrpclient.so libmi.so libssl.so.1.0.0 libcrypto.so.1.0.0 /usr/lib
  3. Relink/cache shared library:
    sudo ldconfig -n -v /usr/lib

Edit: On Fedora Linux 36 with PowerShell 7.2.5, I had to use a forked version of Microsoft's OMI. For some reason, the libraries that came with it don't work anymore, perhaps because they depend upon OpenSSL 1.0, which doesn't ship with Fedora Linux anymore?

  1. Initiate an elevated PowerShell session:
    sudo pwsh
  2. Install the PSWSMan module (this is a forked version of Microsoft OMI):
    Install-Module PSWSMan
  3. Let the module copy the correct libraries into your PowerShell directory:
    Install-WSMan
  4. Proceed with previous steps (except you won't copy the libssl and libcrypto libraries, because this forked version is compiled against OpenSSL 1.1 instead)
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot, you just saved me hours of work. It is enough to copy libpsrpclient.so and libmi.so for newer versions of PS (7.2.*).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.