Skip to content

Export-PSSession generated module does not load in PowerShell 5.1 even though ScriptGenerator version is the same #17132

@Tadas

Description

@Tadas

Prerequisites

It works if done other way around (exported on PS5, imported on PS7).

Seems like at some point Export-PSSession moved to a different assembly. Considering that VersionOfScriptGenerator has stayed on 1.0, that seems to imply that generated modules should be backwards compatible. Changing:

string versionOfScriptGenerator = "[" + typeof(ExportPSSessionCommand).AssemblyQualifiedName + "]" + "::VersionOfScriptGenerator";

to something like the following fixes the issue

string versionOfScriptGenerator = "$executionContext.InvokeCommand.GetCommand('Export-PSSession', [System.Management.Automation.CommandTypes]::Cmdlet).ImplementingType::VersionOfScriptGenerator";

Steps to reproduce

On PS7 export remote session to a module:

$s =  New-PSSession -ComputerName TestMachine

Export-PSSession -Session $s -CommandName Get-Date -OutputModule $PWD\Remote_GetDate -AllowClobber

On PS 5.1 try to load the module

Import-Module .\Remote_GetDate

Expected behavior

# Remote_GetDate module loads, can run the implicit remoting command
(Get-Date).PSComputerName

Actual behavior

# Module fails to load
Unable to find type [Microsoft.PowerShell.Commands.ExportPSSessionCommand,Microsoft.PowerShell.Commands.Utility, Version=7.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]

Error details

Unable to find type [Microsoft.PowerShell.Commands.ExportPSSessionCommand,Microsoft.PowerShell.Commands.Utility, Version=7.0.0.0, Culture=neutral, PublicKeyToken=31bf38
56ad364e35].
At D:\Dev\Remote_GetDate\Remote_GetDate.psm1:15 char:57
+ ... Generator = [Microsoft.PowerShell.Commands.ExportPSSessionCommand, Mi ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (Microsoft.Power...1bf3856ad364e35:TypeName) [], RuntimeException
    + FullyQualifiedErrorId : TypeNotFound

Import-Module : The module cannot be loaded because it has been generated with an incompatible version of the Export-PSSession cmdlet. Generate the module with the Expo
rt-PSSession cmdlet from the current session, and try loading the module again.
At line:1 char:1
+ Import-Module .\Remote_GetDate
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : OperationStopped: (The module cann...e module again.:String) [Import-Module], RuntimeException
    + FullyQualifiedErrorId : The module cannot be loaded because it has been generated with an incompatible version of the Export-PSSession cmdlet. Generate the modul
   e with the Export-PSSession cmdlet from the current session, and try loading the module again.,Microsoft.PowerShell.Commands.ImportModuleCommand

Environment data

# Module exported on
Name                           Value
----                           -----
PSVersion                      7.3.0-preview.3
PSEdition                      Core
GitCommitId                    7.3.0-preview.3-28-g7cc9c874730e87b06dbe54d50e77c1c472996c54
OS                             Microsoft Windows 10.0.22000
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Visuals

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Resolution-FixedThe issue is fixed.WG-RemotingPSRP issues with any transport layer

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions