mirror of
https://github.com/PowerShell/PowerShell
synced 2026-06-08 12:12:50 +00:00
Fix PSVersion parameter version checks and error messages for PowerShell 7 remoting (#16228)
This commit is contained in:
@@ -2320,13 +2320,8 @@ else
|
||||
|
||||
set
|
||||
{
|
||||
RemotingCommandUtil.CheckPSVersion(value);
|
||||
|
||||
// Check if specified version of PowerShell is installed
|
||||
RemotingCommandUtil.CheckIfPowerShellVersionIsInstalled(value);
|
||||
|
||||
psVersion = value;
|
||||
isPSVersionSpecified = true;
|
||||
// PowerShell 7 remoting endpoints do not support PSVersion.
|
||||
throw new PSNotSupportedException(RemotingErrorIdStrings.PowerShellVersionNotSupported);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -512,10 +512,12 @@ namespace Microsoft.PowerShell.Commands
|
||||
|
||||
set
|
||||
{
|
||||
RemotingCommandUtil.CheckPSVersion(value);
|
||||
|
||||
// Check if specified version of PowerShell is installed
|
||||
RemotingCommandUtil.CheckIfPowerShellVersionIsInstalled(value);
|
||||
// PSVersion value can only be 5.1 for Start-Job.
|
||||
if (!(value.Major == 5 && value.Minor == 1))
|
||||
{
|
||||
throw new ArgumentException(
|
||||
StringUtil.Format(RemotingErrorIdStrings.PSVersionParameterOutOfRange, value, "PSVersion"));
|
||||
}
|
||||
|
||||
_psVersion = value;
|
||||
}
|
||||
|
||||
@@ -165,60 +165,5 @@ namespace Microsoft.PowerShell.Commands
|
||||
throw new InvalidOperationException(errorRecord.ToString());
|
||||
}
|
||||
}
|
||||
|
||||
internal static void CheckPSVersion(Version version)
|
||||
{
|
||||
// PSVersion value can only be 2.0, 3.0, 4.0, 5.0, or 5.1
|
||||
if (version != null)
|
||||
{
|
||||
// PSVersion value can only be 2.0, 3.0, 4.0, 5.0, or 5.1
|
||||
if (!(version.Major >= 2 && version.Major <= 4 && version.Minor == 0) &&
|
||||
!(version.Major == 5 && version.Minor <= 1))
|
||||
{
|
||||
throw new ArgumentException(
|
||||
StringUtil.Format(RemotingErrorIdStrings.PSVersionParameterOutOfRange, version, "PSVersion"));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Checks if the specified version of PowerShell is installed.
|
||||
/// </summary>
|
||||
/// <param name="version"></param>
|
||||
internal static void CheckIfPowerShellVersionIsInstalled(Version version)
|
||||
{
|
||||
// Check if PowerShell 2.0 is installed
|
||||
if (version != null && version.Major == 2)
|
||||
{
|
||||
#if CORECLR
|
||||
// PowerShell 2.0 is not available for CoreCLR
|
||||
throw new ArgumentException(
|
||||
PSRemotingErrorInvariants.FormatResourceString(
|
||||
RemotingErrorIdStrings.PowerShellNotInstalled,
|
||||
version, "PSVersion"));
|
||||
#else
|
||||
// Because of app-compat issues, in Win8, we will have PS 2.0 installed by default but not .NET 2.0
|
||||
// In such a case, it is not enough if we check just PowerShell registry keys. We also need to check if .NET 2.0 is installed.
|
||||
try
|
||||
{
|
||||
RegistryKey engineKey = PSSnapInReader.GetPSEngineKey(PSVersionInfo.RegistryVersion1Key);
|
||||
// Also check for .NET 2.0 installation
|
||||
if (!PsUtils.FrameworkRegistryInstallation.IsFrameworkInstalled(2, 0, 0))
|
||||
{
|
||||
throw new ArgumentException(
|
||||
PSRemotingErrorInvariants.FormatResourceString(
|
||||
RemotingErrorIdStrings.NetFrameWorkV2NotInstalled));
|
||||
}
|
||||
}
|
||||
catch (PSArgumentException)
|
||||
{
|
||||
throw new ArgumentException(
|
||||
PSRemotingErrorInvariants.FormatResourceString(
|
||||
RemotingErrorIdStrings.PowerShellNotInstalled,
|
||||
version, "PSVersion"));
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -837,8 +837,7 @@ Note that 'Start-Job' is not supported by design in scenarios where PowerShell i
|
||||
<value>A {1} job source adapter threw an exception with the following message: {0}</value>
|
||||
</data>
|
||||
<data name="PSVersionParameterOutOfRange" xml:space="preserve">
|
||||
<value>The value {0} is not valid for the {1} parameter. The available values are 2.0, 3.0, 4.0, 5.0, 5.1.</value>
|
||||
<comment>{StrContains="2.0"} {StrContains="3.0"}</comment>
|
||||
<value>The value {0} is not valid for the {1} parameter. The only allowed value is 5.1.</value>
|
||||
</data>
|
||||
<data name="BlockCannotBeUsedWithKeep" xml:space="preserve">
|
||||
<value>The Wait and Keep parameters cannot be used together in the same command.</value>
|
||||
@@ -846,8 +845,8 @@ Note that 'Start-Job' is not supported by design in scenarios where PowerShell i
|
||||
<data name="WriteEventsCannotBeUsedWithoutWait" xml:space="preserve">
|
||||
<value>The WriteEvents parameter cannot be used without the Wait parameter.</value>
|
||||
</data>
|
||||
<data name="PowerShellNotInstalled" xml:space="preserve">
|
||||
<value>PowerShell {0} is not installed. Install PowerShell {0}, and then try again.</value>
|
||||
<data name="PowerShellVersionNotSupported" xml:space="preserve">
|
||||
<value>PowerShell remoting endpoint versioning is not supported on PowerShell Core.</value>
|
||||
</data>
|
||||
<data name="JobManagerRegistrationConstructorError" xml:space="preserve">
|
||||
<value>The following type cannot be instantiated because its constructor is not public: {0}.</value>
|
||||
|
||||
@@ -243,3 +243,16 @@ Describe "Ampersand background test" -Tag "CI","Slow" {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Describe "Start-Job with -PSVersion parameter" -Tag "CI" {
|
||||
|
||||
It "Verifies that -PSVersion is not supported except for version 5.1" {
|
||||
{ Start-Job -PSVersion 2.0 } | Should -Throw -ErrorId 'ParameterBindingFailed,Microsoft.PowerShell.Commands.StartJobCommand'
|
||||
}
|
||||
|
||||
It "Verifies that -PSVersion 5.1 runs the job in a version 5.1 PowerShell session" -Skip:(-not $IsWindows) {
|
||||
$version = Start-Job -PSVersion 5.1 -ScriptBlock { $PSVersionTable } | Receive-Job -Wait -AutoRemoveJob
|
||||
$version.PSVersion.Major | Should -Be 5
|
||||
$version.PSVersion.Minor | Should -Be 1
|
||||
}
|
||||
}
|
||||
|
||||
@@ -491,13 +491,9 @@ namespace PowershellTestConfigNamespace
|
||||
$Result.Session.UseSharedProcess | Should -Be $UseSharedProcess
|
||||
}
|
||||
|
||||
It "Validate Register-PSSessionConfiguration -PSVersion" {
|
||||
It "Verifies that Register-PSSessionConfiguration -PSVersion parameter is not supported" {
|
||||
|
||||
Register-PSSessionConfiguration -Name $TestSessionConfigName -PSVersion 5.1
|
||||
$Session = Get-PSSessionConfiguration -Name $TestSessionConfigName
|
||||
|
||||
$Session.Name | Should -Be $TestSessionConfigName
|
||||
$Session.PSVersion | Should -BeExactly 5.1
|
||||
{ Register-PSSessionConfiguration -Name $TestSessionConfigName -PSVersion 5.1 } | Should -Throw -ErrorId 'ParameterBindingFailed,Microsoft.PowerShell.Commands.RegisterPSSessionConfigurationCommand'
|
||||
}
|
||||
|
||||
It "Validate Register-PSSessionConfiguration -startupscript parameter" -Pending {
|
||||
@@ -567,13 +563,9 @@ namespace PowershellTestConfigNamespace
|
||||
$Result.Session.UseSharedProcess | Should -Be $UseSharedProcess
|
||||
}
|
||||
|
||||
It "Validate Set-PSSessionConfiguration -PSVersion" {
|
||||
It "Verifies that Set-PSSessionConfiguration -PSVersion parameter is not supported" {
|
||||
|
||||
Set-PSSessionConfiguration -Name $TestSessionConfigName -PSVersion 5.1
|
||||
$Session = (Get-PSSessionConfiguration -Name $TestSessionConfigName)
|
||||
|
||||
$Session.Name | Should -Be $TestSessionConfigName
|
||||
$Session.PSVersion | Should -BeExactly 5.1
|
||||
{ Set-PSSessionConfiguration -Name $TestSessionConfigName -PSVersion 5.1 } | Should -Throw -ErrorId 'ParameterBindingFailed,Microsoft.PowerShell.Commands.SetPSSessionConfigurationCommand'
|
||||
}
|
||||
|
||||
It "Validate Set-PSSessionConfiguration -startupscript parameter" -Pending {
|
||||
|
||||
Reference in New Issue
Block a user