diff --git a/src/System.Management.Automation/CoreCLR/CorePsPlatform.cs b/src/System.Management.Automation/CoreCLR/CorePsPlatform.cs index 134291ed6..6edfbaa90 100644 --- a/src/System.Management.Automation/CoreCLR/CorePsPlatform.cs +++ b/src/System.Management.Automation/CoreCLR/CorePsPlatform.cs @@ -217,12 +217,6 @@ namespace System.Management.Automation } - // Linux does not have PowerShell execution policies - internal static bool HasExecutionPolicy() - { - return IsWindows; - } - // Linux has a single rooted file system internal static bool HasSingleRootFilesystem() { diff --git a/src/System.Management.Automation/security/SecuritySupport.cs b/src/System.Management.Automation/security/SecuritySupport.cs index f66d8094c..8d3fc91f2 100644 --- a/src/System.Management.Automation/security/SecuritySupport.cs +++ b/src/System.Management.Automation/security/SecuritySupport.cs @@ -138,11 +138,9 @@ namespace System.Management.Automation internal static void SetExecutionPolicy(ExecutionPolicyScope scope, ExecutionPolicy policy, string shellId) { - if (!Platform.HasExecutionPolicy()) - { - throw new PlatformNotSupportedException(); - } - +#if LINUX + throw new PlatformNotSupportedException(); +#else string executionPolicy = "Restricted"; string preferenceKey = Utils.GetRegistryConfigurationPath(shellId); const string PolicyKeyValueName = "ExecutionPolicy"; @@ -227,6 +225,7 @@ namespace System.Management.Automation break; } } +#endif } // Clean up the parents of a registry key as long as they @@ -283,11 +282,9 @@ namespace System.Management.Automation internal static ExecutionPolicy GetExecutionPolicy(string shellId, ExecutionPolicyScope scope) { - if (!Platform.HasExecutionPolicy()) - { - throw new PlatformNotSupportedException(); - } - +#if LINUX + return ExecutionPolicy.Unrestricted; +#else switch (scope) { case ExecutionPolicyScope.Process: @@ -368,6 +365,7 @@ namespace System.Management.Automation } return ExecutionPolicy.Restricted; +#endif } internal static ExecutionPolicy ParseExecutionPolicy(string policy) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Security/ExecutionPolicy.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Security/ExecutionPolicy.Tests.ps1 index b5631218d..cdc5a239c 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Security/ExecutionPolicy.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Security/ExecutionPolicy.Tests.ps1 @@ -1,24 +1,24 @@ Describe "ExecutionPolicy" { Context "Check Get-ExecutionPolicy behavior" { - It "Should throw PlatformNotSupported when not on Windows" -Skip:$IsWindows { - { Get-ExecutionPolicy } | Should Throw "Operation is not supported on this platform." - } + It "Should unrestricted when not on Windows" -Skip:$IsWindows { + Get-ExecutionPolicy | Should Be Unrestricted + } - It "Should return Microsoft.Powershell.ExecutionPolicy PSObject on Windows" -Skip:($IsLinux -Or $IsOSX) { - (Get-ExecutionPolicy).GetType() | Should Be Microsoft.Powershell.ExecutionPolicy - } + It "Should return Microsoft.Powershell.ExecutionPolicy PSObject on Windows" -Skip:($IsLinux -Or $IsOSX) { + (Get-ExecutionPolicy).GetType() | Should Be Microsoft.Powershell.ExecutionPolicy + } } Context "Check Set-ExecutionPolicy behavior" { - It "Should throw PlatformNotSupported when not on Windows" -Skip:$IsWindows { - { Set-ExecutionPolicy Unrestricted } | Should Throw "Operation is not supported on this platform." - } + It "Should throw PlatformNotSupported when not on Windows" -Skip:$IsWindows { + { Set-ExecutionPolicy Unrestricted } | Should Throw "Operation is not supported on this platform." + } - It "Should succeed on Windows" -Skip:($IsLinux -Or $IsOSX) { - # We use the Process scope to avoid affecting the system - # Unrestricted is assumed "safe", otherwise these tests would not be running - { Set-ExecutionPolicy -Force -Scope Process -ExecutionPolicy Unrestricted } | Should Not Throw - } + It "Should succeed on Windows" -Skip:($IsLinux -Or $IsOSX) { + # We use the Process scope to avoid affecting the system + # Unrestricted is assumed "safe", otherwise these tests would not be running + { Set-ExecutionPolicy -Force -Scope Process -ExecutionPolicy Unrestricted } | Should Not Throw + } } }