Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2320,13 +2320,8 @@ public Version PSVersion

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);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -512,10 +512,12 @@ public virtual Version PSVersion

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))
Comment thread
PaulHigin marked this conversation as resolved.
Resolved
{
throw new ArgumentException(
StringUtil.Format(RemotingErrorIdStrings.PSVersionParameterOutOfRange, value, "PSVersion"));
}

_psVersion = value;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,60 +165,5 @@ internal static void CheckHostRemotingPrerequisites()
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
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -837,17 +837,16 @@ 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>
</data>
<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>

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we have PowerShell Committee for the decision?
Previously we support side-by-side scenarios, ex. 7.0 and 7.1 versions are installed simultaneously.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@PowerShell/powershell-committee reviewed this and we have an existing side-by-side story with PS7 which is to use the full path to pwsh to use a specific version of PS7. We don't believe there is a real world scenario where supporting -PSVersion to specify a PS7 version that would be unsupported by 3 years (at most). If there is a real world scenario that we are not aware of, we recommend opening a Discussion to help us understand. This PR doesn't prevent future support of such a feature if it makes sense.

</data>
<data name="JobManagerRegistrationConstructorError" xml:space="preserve">
<value>The following type cannot be instantiated because its constructor is not public: {0}.</value>
Expand Down
13 changes: 13 additions & 0 deletions test/powershell/Modules/Microsoft.PowerShell.Core/Job.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -567,13 +563,9 @@ namespace PowershellTestConfigNamespace
$Result.Session.UseSharedProcess | Should -Be $UseSharedProcess
}

It "Validate Set-PSSessionConfiguration -PSVersion" {

Set-PSSessionConfiguration -Name $TestSessionConfigName -PSVersion 5.1
$Session = (Get-PSSessionConfiguration -Name $TestSessionConfigName)
It "Verifies that Set-PSSessionConfiguration -PSVersion parameter is not supported" {

$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 {
Expand Down