Stephen
Stephen

Reputation: 31

c# PowerShell Remoting Not Working On Server 2012 R2 With .Net 6

I have a c# application which uses powershell remoting to make connections to remote servers. I recently migrated it from .Net 4.6 to .Net 6.0. Everything works well on my dev system but it doesn't quite work on Server 2012 R2.

The code basically does this:

// using System.Management.Automation.Runspaces;
const string shellUri = "http://schemas.microsoft.com/powershell/Microsoft.PowerShell"
WSManConnectionInfo connectionInfo = new (true, remoteAddress, remotePort, "/wsman", shellUri, remoteCredentials);

// this throws the exception:
var runspace = RunspaceFactory.CreateRunspace(connectionInfo);

If I deploy the application onto a Server 2019 server it runs fine, so it's something specific to Server 2012 R2. Unfortunately this is the OS I'm stuck with so upgrading isn't an option.

Callstack details: .NET Version: 6.0.10 Description: The process was terminated due to an unhandled exception. Exception Info: 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 DLL 'WsmSvc.dll' or one of its dependencies: The specified module could not be found. (0x8007007E) 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) ... (user code here)

Upvotes: 1

Views: 241

Answers (1)

Stephen
Stephen

Reputation: 31

I solved my own issue.

Out of habit I reused my existing deploy script which ran the following:

dotnet publish -c --self-contained=true $file --runtime win10-x64 --configuration Release

See the problem there? Fixed it by running the following:

dotnet publish -c --self-contained=true $file --runtime win81-x64 --configuration Release

Where the face-palm emogi...

Upvotes: 2

Related Questions