Matthew MacFarland
Matthew MacFarland

Reputation: 2729

Why does a newer version of the PowerShell module run when I import with -MaximumVersion set to a lower version?

I have a build script that depends on an older version of one of our modules. Version 1.0.1. I added -MaximumVersion 1.0.1 to the Import-Module command. When the build script runs it fails and error shows that it is running code in version 2.1.0 of the module.

Import-Module DrilQuip.Build -MaximumVersion 1.0.1 -Force

Creating next version number... The property 'VersionFilePath' cannot be found on this object. Verify that the property exists. At C:\Users\svcTFSBuildProd\Documents\WindowsPowerShell\Modules\DrilQuip.Build\ 2.1.0\DrilQuip.Build.psm1:253 char:5

I have tried with and without the -Force switch but that makes no difference.

I used Get-Module DrilQuip.Build -ListAvailable to confirm that version 1.0.1 is present on the computer.

How can I insure that the script imports and uses the older version of the modules?

Update 1

Added -Verbose switch to get more details about what is happening. Here are the results:

VERBOSE: Loading module from path 'C:\Program Files\WindowsPowerShell\Modules\DrilQuip.Build\1.0.1\DrilQuip.Build.psd1'. VERBOSE: Populating RepositorySourceLocation property for module DrilQuip.Build.

Creating next version number... The property 'VersionFilePath' cannot be found on this object. Verify that the property exists. At C:\Users\svcTFSBuildProd\Documents\WindowsPowerShell\Modules\DrilQuip.Build\ 2.1.0\DrilQuip.Build.psm1:253 char:5 + $Matches = Select-String -Path $global:BuildConfig.VersionFilePat ...

This shows that the same module has been installed into 2 different locations. The location C:\Users\svcTFSBuildProd... seems to trump the location C:\Program Files\WindowsPowerShell...

I think this has to do with Machine vs User scope on the module installation. I'll go back and remove the User scoped modules and install all versions of the module with Machine scope and see if that helps.

Update 2

I removed all of the versions of the module from user scope folder and then tried the script again. It is still failing but now both versions of the module are coming from the same module folder location.

VERBOSE: Loading module from path 'C:\Program Files\WindowsPowerShell\Modules\DrilQuip.Build\1.0.1\DrilQuip.Build.psd1'. VERBOSE: Populating RepositorySourceLocation property for module DrilQuip.Build. Creating next version number... The property 'VersionFilePath' cannot be found on this object. Verify that the property exists. At C:\Program Files\WindowsPowerShell\Modules\DrilQuip.Build\2.0.4\DrilQuip.Build.psm1:251 char:5

Since the new version is still trumping the max version I requested my theory that user scope trumps machine scope is not the real problem. Something else is going on.

I ran Get-Module -Name DrilQuip.Build -ListAvailable again and I notice that the ModuleType is different. On version 1.0.1 the type is Manifest but on versions 1.1.1 and 2.0.4 the type is Script. Maybe this difference is causing the problem.

ModuleType Version    Name          
---------- -------    ----          
Script     2.0.4      DrilQuip.Build
Script     1.1.1      DrilQuip.Build
Manifest   1.0.1      DrilQuip.Build

I will remove all of the modules and reinstall them from the repository.

Upvotes: 1

Views: 1595

Answers (1)

Matthew MacFarland
Matthew MacFarland

Reputation: 2729

The older version of the module 1.0.1 has the type Manifest and all the versions after that are type Script. The next version of the module 1.0.2 is also compatible with my build script so I changed the -MaximumVersion parameter to 1.0.2.

Before trying this out I also uninstalled all versions of the module on the computer and then installed just versions 1.0.2 and 2.1.0 that are really needed. I ran PowerShell as administrator so both modules installed into the folder C:\Program Files\WindowsPowerShell\Modules

PS C:\Program Files\WindowsPowerShell\Modules\DrilQuip.Build> get-module DrilQuip.Build -li

    Directory: C:\Program Files\WindowsPowerShell\Modules
ModuleType Version    Name                                ExportedCommands
---------- -------    ----                                ----------------
Script     2.1.0      DrilQuip.Build                      {Start-Build, Write-FileCopyResult, Invoke-MSBuild, New-Da...
Script     1.0.2      DrilQuip.Build                      {Get-NextVersion, Set-TfsWorkspaceFileTime}

After these changes the build script works and uses the 1.0.2 version of the code as expected.

VERBOSE: Loading module from path 'C:\Program 
Files\WindowsPowerShell\Modules\DrilQuip.Build\1.0.2\DrilQuip.Build.psd1'.
VERBOSE: Populating RepositorySourceLocation property for module 
DrilQuip.Build.
VERBOSE: Loading module from path 'C:\Program 
Files\WindowsPowerShell\Modules\DrilQuip.Build\1.0.2\DrilQuip.Build.psm1'.
VERBOSE: Importing function 'Get-NextVersion'.
VERBOSE: Importing function 'Set-TfsWorkspaceFileTime'.

Creating next version number...
New version: 10.2.10928.11004

Based on mklement0's comment it seems that whole problem is that version 1.0.1 was not setup correctly and so no functions got imported. The verbose output from Import-Module confirms that. When the script called the function Get-NextVersion PowerShell used module auto loading to find and load a version of the module that did have the function.

Version 1.0.1 was missing a value for RootModule in the manifest. That error was fixed in version 1.0.2. The module uses Export-ModuleMember to export the functions instead of the FunctionsToExport setting in the manifest. Since 1.0.1 did not have root module set to the psm1 file it had no way to export the functions..

Upvotes: 1

Related Questions