diff --git a/src/System.Management.Automation/engine/Modules/ModuleSpecification.cs b/src/System.Management.Automation/engine/Modules/ModuleSpecification.cs
index 775b14a555e..d7e1255cafd 100644
--- a/src/System.Management.Automation/engine/Modules/ModuleSpecification.cs
+++ b/src/System.Management.Automation/engine/Modules/ModuleSpecification.cs
@@ -136,7 +136,7 @@ internal static Exception ModuleSpecificationInitHelper(ModuleSpecification modu
string message;
if (badKeys.Length != 0)
{
- message = StringUtil.Format(Modules.InvalidModuleSpecificationMember, "ModuleName, ModuleVersion, RequiredVersion, GUID", badKeys);
+ message = StringUtil.Format(Modules.InvalidModuleSpecificationMember, "ModuleName, ModuleVersion, MaximumVersion, RequiredVersion, GUID", badKeys);
return new ArgumentException(message);
}
@@ -163,6 +163,13 @@ internal static Exception ModuleSpecificationInitHelper(ModuleSpecification modu
message = StringUtil.Format(SessionStateStrings.GetContent_TailAndHeadCannotCoexist, "MaximumVersion", "RequiredVersion");
return new ArgumentException(message);
}
+
+ if (moduleSpecification.Version != null && moduleSpecification.MaximumVersion != null &&
+ moduleSpecification.Version > ModuleCmdletBase.GetMaximumVersion(moduleSpecification.MaximumVersion))
+ {
+ message = StringUtil.Format(Modules.ModuleSpecificationMemberIsLessThanOther, moduleSpecification.Version, moduleSpecification.MaximumVersion);
+ return new ArgumentException(message);
+ }
return null;
}
diff --git a/src/System.Management.Automation/resources/Modules.resx b/src/System.Management.Automation/resources/Modules.resx
index d351e756e3a..61e5d8f065e 100644
--- a/src/System.Management.Automation/resources/Modules.resx
+++ b/src/System.Management.Automation/resources/Modules.resx
@@ -165,6 +165,9 @@
The hashtable describing a module contains one or more members that are not valid. The valid members are ({0}). Remove the members that are not valid ({1}), then try again.
+
+ The hashtable describing a module cannot have its ModuleVersion ({0}) greater than its MaximumVersion ({1}).
+
Cannot load the module '{0}' because the module nesting limit has been exceeded. Modules can only be nested to {1} levels. Evaluate and change the order in which you are loading modules to prevent exceeding the nesting limit, and then try running your script again.
diff --git a/test/powershell/Modules/Microsoft.PowerShell.Core/Import-Module.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Core/Import-Module.Tests.ps1
index 263f5eacf4d..fad525e1ff1 100644
--- a/test/powershell/Modules/Microsoft.PowerShell.Core/Import-Module.Tests.ps1
+++ b/test/powershell/Modules/Microsoft.PowerShell.Core/Import-Module.Tests.ps1
@@ -56,6 +56,10 @@ Describe "Import-Module" -Tags "CI" {
Import-Module TestModule -RequiredVersion 1.1
(Get-Module TestModule).Version | Should -BeIn "1.1"
}
+
+ It "should throw if 'MaximumVersion' is less than 'ModuleVersion' when using -FullyQualifiedName parameter" {
+ { Import-Module -FullyQualifiedName @{ModuleName = $moduleName; ModuleVersion = '2.0'; MaximumVersion = '1.0'} } | Should -Throw -ExceptionType 'System.Management.Automation.ParameterBindingException'
+ }
}
Describe "Import-Module with ScriptsToProcess" -Tags "CI" {
diff --git a/test/powershell/engine/Module/ModuleSpecification.Tests.ps1 b/test/powershell/engine/Module/ModuleSpecification.Tests.ps1
index b9f81a47463..588f1e83061 100644
--- a/test/powershell/engine/Module/ModuleSpecification.Tests.ps1
+++ b/test/powershell/engine/Module/ModuleSpecification.Tests.ps1
@@ -245,6 +245,11 @@ Describe "ModuleSpecification objects and logic" -Tag "CI" {
TestName = "BadType"
ModuleSpecification = @{ ModuleName = "BadTypeModule"; RequiredVersion = "Hello!" }
ErrorId = 'PSInvalidCastException'
+ },
+ @{
+ TestName = "ModuleVersion is greater than MaximumVersion"
+ ModuleSpecification = @{ ModuleName = "ModuleVersion>MaximumVersion"; ModuleVersion = "3.0"; MaximumVersion = '2.0' }
+ ErrorId = 'ArgumentException'
}
)
}