question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

PowerShell 7.2.6 doesn't ignore a module that requires 7.3

See original GitHub issue

Prerequisites

Steps to reproduce

  1. Install Microsoft.PowerShell.Archive 2.0.1-preview2 in user scope

  2. Modify the incorrect (https://twitter.com/Steve_MSFT/status/1561009864784384002?s=20&t=gvVFcVRDXx9-OzGIBPGGPA) manifest so that it specifies that 7.3 is required image

  3. Launch PowerShell 7.2.6

  4. Attempt to run Compress-Archive

Expected behavior

Compress-Archive from the built-in version 1 of Microsoft.PowerShell.Archive module should run

Actual behavior

PowerShell attempts to load version `2.0.1-preview2` of `Microsoft.PowerShell.Archive` and fails:

PS C:\Program Files\PowerShell\7> compress-archive
compress-archive: The 'compress-archive' command was found in the module 'Microsoft.PowerShell.Archive', but the module could not be loaded. For more information, run 'Import-Module Microsoft.PowerShell.Archive'.
PS C:\Program Files\PowerShell\7> import-module microsoft.powershell.archive
Import-Module: The version of PowerShell on this computer is '7.2.6'. The module 'C:\Users\steph\OneDrive\Documents\PowerShell\Modules\microsoft.powershell.archive\2.0.1\microsoft.powershell.archive.psd1' requires a minimum PowerShell version of '7.3' to run. Verify that you have the minimum required version of PowerShell installed, and then try again.

Error details

PS C:\Program Files\PowerShell\7> get-error

Exception             :
    Type        : System.Management.Automation.CommandNotFoundException
    ErrorRecord :
        Exception             :
            Type    : System.Management.Automation.ParentContainsErrorRecordException
            Message : The 'compress-archive' command was found in the module 'Microsoft.PowerShell.Archive', but the module could not be
loaded. For more information, run 'Import-Module Microsoft.PowerShell.Archive'.
            HResult : -2146233087
        TargetObject          : compress-archive
        CategoryInfo          : ObjectNotFound: (compress-archive:String) [], ParentContainsErrorRecordException
        FullyQualifiedErrorId : CouldNotAutoloadMatchingModule
        InvocationInfo        :
            ScriptLineNumber : 1
            OffsetInLine     : 1
            HistoryId        : 14
            Line             : compress-archive
            PositionMessage  : At line:1 char:1
                               + compress-archive
                               + ~~~~~~~~~~~~~~~~
            InvocationName   : compress-archive
            CommandOrigin    : Internal
        ScriptStackTrace      : at <ScriptBlock>, <No file>: line 1
    CommandName : compress-archive
    TargetSite  :
        Name          : TryModuleAutoDiscovery
        DeclaringType : System.Management.Automation.CommandDiscovery, System.Management.Automation, Version=7.2.6.500, Culture=neutral,
PublicKeyToken=31bf3856ad364e35
        MemberType    : Method
        Module        : System.Management.Automation.dll
    Message     : The 'compress-archive' command was found in the module 'Microsoft.PowerShell.Archive', but the module could not be
loaded. For more information, run 'Import-Module Microsoft.PowerShell.Archive'.
    Data        : System.Collections.ListDictionaryInternal
    Source      : System.Management.Automation
    HResult     : -2146233087
    StackTrace  :
   at System.Management.Automation.CommandDiscovery.TryModuleAutoDiscovery(String commandName, ExecutionContext context, String
originalCommandName, CommandOrigin commandOrigin, SearchResolutionOptions searchResolutionOptions, CommandTypes commandTypes, Exception&
lastError)
   at System.Management.Automation.CommandDiscovery.LookupCommandInfo(String commandName, CommandTypes commandTypes,
SearchResolutionOptions searchResolutionOptions, CommandOrigin commandOrigin, ExecutionContext context)
   at System.Management.Automation.ExecutionContext.CreateCommand(String command, Boolean dotSource)
   at System.Management.Automation.PipelineOps.AddCommand(PipelineProcessor pipe, CommandParameterInternal[] commandElements,
CommandBaseAst commandBaseAst, CommandRedirection[] redirections, ExecutionContext context)
   at System.Management.Automation.PipelineOps.InvokePipeline(Object input, Boolean ignoreInput, CommandParameterInternal[][]
pipeElements, CommandBaseAst[] pipeElementAsts, CommandRedirection[][] commandRedirections, FunctionContext funcContext)
   at System.Management.Automation.Interpreter.ActionCallInstruction`6.Run(InterpretedFrame frame)
   at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
TargetObject          : compress-archive
CategoryInfo          : ObjectNotFound: (compress-archive:String) [], CommandNotFoundException
FullyQualifiedErrorId : CouldNotAutoloadMatchingModule
InvocationInfo        :
    ScriptLineNumber : 1
    OffsetInLine     : 1
    HistoryId        : 14
    Line             : compress-archive
    PositionMessage  : At line:1 char:1
                       + compress-archive
                       + ~~~~~~~~~~~~~~~~
    InvocationName   : compress-archive
    CommandOrigin    : Internal
ScriptStackTrace      : at <ScriptBlock>, <No file>: line 1

Environment data

PS C:\Program Files\PowerShell\7> $PSVersionTable

Name                           Value
----                           -----
PSVersion                      7.2.6
PSEdition                      Core
GitCommitId                    7.2.6
OS                             Microsoft Windows 10.0.19044
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

PS C:\Program Files\PowerShell\7> $env:PSModulePath -split ';' | % { $mp = join-path -path $_ -childpath 'Microsoft.PowerShell.Archive'; if (Test-Path -LiteralPath $mp) { gci -rec $mp -filter 'm*.psd1' } }

    Directory: C:\Users\<REDACTED>\OneDrive\Documents\PowerShell\Modules\Microsoft.PowerShell.Archive\2.0.1

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
la---          2022-08-20    19:33          16210 Microsoft.PowerShell.Archive.psd1

    Directory: C:\Program Files\PowerShell\7\Modules\Microsoft.PowerShell.Archive

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
-a---          2020-02-11    19:01          13503 Microsoft.PowerShell.Archive.psd1

    Directory: C:\Windows\System32\WindowsPowerShell\v1.0\Modules\Microsoft.PowerShell.Archive

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
-a---          2019-12-07    10:10            457 Microsoft.PowerShell.Archive.psd1

PS C:\Program Files\PowerShell\7> gcm *archive

CommandType     Name                                               Version    Source
-----------     ----                                               -------    ------
Function        Compress-Archive                                   1.2.5      Microsoft.PowerShell.Archive
Function        Compress-Archive                                   1.0.1.0    Microsoft.PowerShell.Archive
Function        Expand-Archive                                     1.2.5      Microsoft.PowerShell.Archive
Function        Expand-Archive                                     1.0.1.0    Microsoft.PowerShell.Archive
Cmdlet          Compress-Archive                                   2.0.1      Microsoft.PowerShell.Archive
Cmdlet          Expand-Archive                                     2.0.1      Microsoft.PowerShell.Archive

Visuals

image

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
SteveL-MSFTcommented, Sep 3, 2022

@kilasuit I’m not proposing that we do anything in the case the module indicates it is compatible, but doesn’t load for other reason (it’s incorrectly marked, dependencies missing, etc…). As we do have many folks running 5.1, PS7 and PS7-preview simultaneously, it would make sense that if the module manifest declares requiring a newer version, then an older version of PS should skip that module and continue its normal search logic.

0reactions
kilasuitcommented, Sep 3, 2022

I agree @SteveL-MSFT and captured the above as test cases as currently we don’t load the next working version

  1. We can auto load the next working version, which should be a lower version number, regardless of which path in PSModulePaths it is located,
  2. We don’t break current expectation for those that may not automatically want that to happen - example being in testing/use of preview versions of modules requiring preview versions of PowerShell

Whilst 1 is a definite need 2 is a nice to have IMO

Read more comments on GitHub >

github_iconTop Results From Across the Web

What's New in PowerShell 7.3
After this change, a user needs to explicitly import Microsoft.PowerShell.Security module in order to use those code properties and code methods ...
Read more >
Installing PowerShell on Windows
PowerShell 7.3 installs to a new directory and runs side-by-side with Windows PowerShell 5.1. PowerShell 7.3 is an in-place upgrade that ...
Read more >
Differences between Windows PowerShell 5.1 and ...
This article summarizes the differences and breaking changes from Windows PowerShell 5.1 and the current version of PowerShell that is based ...
Read more >
about Requires - PowerShell
The #Requires statement prevents a script from running unless the PowerShell version, modules (and version), or snap-ins (and version), ...
Read more >
Import-Module (Microsoft.PowerShell.Core)
Import-Module imports a module only into the current session. To import the module into every new session, add an Import-Module command to your...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found