mirror of
https://github.com/aphrody-labs/PowerShell.git
synced 2026-10-17 04:36:39 +00:00
Merge pull request #2036 from mirichmo/mirichmo/fixing-user-config-path
Fixing the path to the user's JSON configuration file
This commit is contained in:
commit
f7487942c4
4 files changed
+108
-20
No files matched your search
@@ -146,7 +146,7 @@ namespace System.Management.Automation
|
|||||||
//
|
//
|
||||||
// Sets the per-user configuration directory
|
// Sets the per-user configuration directory
|
||||||
//
|
//
|
||||||
appDataConfigDirectory = Utils.GetUserSettingsDirectory();
|
appDataConfigDirectory = Utils.GetUserConfigurationDirectory();
|
||||||
if (!Directory.Exists(appDataConfigDirectory))
|
if (!Directory.Exists(appDataConfigDirectory))
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
@@ -166,11 +166,11 @@ namespace System.Management.Automation
|
|||||||
/// not interfere with PowerShell initialization
|
/// not interfere with PowerShell initialization
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <returns>Returns the directory if present or creatable. Throws otherwise.</returns>
|
/// <returns>Returns the directory if present or creatable. Throws otherwise.</returns>
|
||||||
private string GetAppDataConfigDirectory()
|
private string GetCurrentUserConfigDirectory()
|
||||||
{
|
{
|
||||||
if (null == appDataConfigDirectory)
|
if (null == appDataConfigDirectory)
|
||||||
{
|
{
|
||||||
string tempAppDataConfigDir = Utils.GetUserSettingsDirectory();
|
string tempAppDataConfigDir = Utils.GetUserConfigurationDirectory();
|
||||||
if (!Directory.Exists(tempAppDataConfigDir))
|
if (!Directory.Exists(tempAppDataConfigDir))
|
||||||
{
|
{
|
||||||
Directory.CreateDirectory(tempAppDataConfigDir);
|
Directory.CreateDirectory(tempAppDataConfigDir);
|
||||||
@@ -194,7 +194,7 @@ namespace System.Management.Automation
|
|||||||
// Defaults to system wide.
|
// Defaults to system wide.
|
||||||
if (PropertyScope.CurrentUser == scope)
|
if (PropertyScope.CurrentUser == scope)
|
||||||
{
|
{
|
||||||
scopeDirectory = GetAppDataConfigDirectory();
|
scopeDirectory = GetCurrentUserConfigDirectory();
|
||||||
}
|
}
|
||||||
|
|
||||||
string fileName = Path.Combine(scopeDirectory, configFileName);
|
string fileName = Path.Combine(scopeDirectory, configFileName);
|
||||||
@@ -229,7 +229,7 @@ namespace System.Management.Automation
|
|||||||
// Defaults to system wide.
|
// Defaults to system wide.
|
||||||
if(PropertyScope.CurrentUser == scope)
|
if(PropertyScope.CurrentUser == scope)
|
||||||
{
|
{
|
||||||
scopeDirectory = GetAppDataConfigDirectory();
|
scopeDirectory = GetCurrentUserConfigDirectory();
|
||||||
}
|
}
|
||||||
|
|
||||||
string fileName = Path.Combine(scopeDirectory, configFileName);
|
string fileName = Path.Combine(scopeDirectory, configFileName);
|
||||||
@@ -250,7 +250,7 @@ namespace System.Management.Automation
|
|||||||
// Defaults to system wide.
|
// Defaults to system wide.
|
||||||
if (PropertyScope.CurrentUser == scope)
|
if (PropertyScope.CurrentUser == scope)
|
||||||
{
|
{
|
||||||
scopeDirectory = GetAppDataConfigDirectory();
|
scopeDirectory = GetCurrentUserConfigDirectory();
|
||||||
}
|
}
|
||||||
|
|
||||||
string fileName = Path.Combine(scopeDirectory, configFileName);
|
string fileName = Path.Combine(scopeDirectory, configFileName);
|
||||||
@@ -265,7 +265,7 @@ namespace System.Management.Automation
|
|||||||
// Defaults to system wide.
|
// Defaults to system wide.
|
||||||
if (PropertyScope.CurrentUser == scope)
|
if (PropertyScope.CurrentUser == scope)
|
||||||
{
|
{
|
||||||
scopeDirectory = GetAppDataConfigDirectory();
|
scopeDirectory = GetCurrentUserConfigDirectory();
|
||||||
}
|
}
|
||||||
|
|
||||||
string fileName = Path.Combine(scopeDirectory, configFileName);
|
string fileName = Path.Combine(scopeDirectory, configFileName);
|
||||||
|
|||||||
@@ -282,16 +282,16 @@ namespace System.Management.Automation
|
|||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Specifies the per-user configuration settings directory in a platform agnostic manner.
|
/// Specifies the per-user configuration settings directory in a platform agnostic manner.
|
||||||
/// Windows Ex:
|
|
||||||
/// %LOCALAPPDATA%\PowerShell
|
|
||||||
/// Non-Windows Ex:
|
|
||||||
/// ~/.config/PowerShell
|
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <returns>The current user's configuration settings directory</returns>
|
/// <returns>The current user's configuration settings directory</returns>
|
||||||
internal static string GetUserSettingsDirectory()
|
internal static string GetUserConfigurationDirectory()
|
||||||
{
|
{
|
||||||
string appDataPath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
|
#if UNIX
|
||||||
return Path.Combine(appDataPath, "PowerShell");
|
return Platform.SelectProductNameForDirectory(Platform.XDG_Type.CONFIG);
|
||||||
|
#else
|
||||||
|
string basePath = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
|
||||||
|
return IO.Path.Combine(basePath, Utils.ProductNameForDirectory);
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
private static string[] GetProductFolderDirectories()
|
private static string[] GetProductFolderDirectories()
|
||||||
|
|||||||
@@ -182,12 +182,7 @@ namespace System.Management.Automation
|
|||||||
|
|
||||||
if (forCurrentUser)
|
if (forCurrentUser)
|
||||||
{
|
{
|
||||||
#if UNIX
|
basePath = Utils.GetUserConfigurationDirectory();
|
||||||
basePath = Platform.SelectProductNameForDirectory(Platform.XDG_Type.CONFIG);
|
|
||||||
#else
|
|
||||||
basePath = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
|
|
||||||
basePath = IO.Path.Combine(basePath, Utils.ProductNameForDirectory);
|
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -851,6 +851,99 @@ ZoneId=$FileType
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Describe "Validate Set-ExecutionPolicy -Scope" -Tags "CI" {
|
||||||
|
|
||||||
|
BeforeAll {
|
||||||
|
$originalPolicies = Get-ExecutionPolicy -list
|
||||||
|
|
||||||
|
# Calls Set-ExecutionPolicy with a known-bad Scope and expects failure.
|
||||||
|
# It is defined here so that it will be available at It scope.
|
||||||
|
function VerfiyBlockedSetExecutionPolicy
|
||||||
|
{
|
||||||
|
param(
|
||||||
|
[string]
|
||||||
|
$policyScope
|
||||||
|
)
|
||||||
|
$fqeid = ""
|
||||||
|
try {
|
||||||
|
Set-ExecutionPolicy -Scope $policyScope -ExecutionPolicy Restricted
|
||||||
|
}
|
||||||
|
catch {
|
||||||
|
$fqeid = $_.FullyQualifiedErrorId
|
||||||
|
}
|
||||||
|
|
||||||
|
$fqeid | Should Be "CantSetGroupPolicy,Microsoft.PowerShell.Commands.SetExecutionPolicyCommand"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
AfterAll {
|
||||||
|
foreach ($scopedPolicy in $originalPolicies)
|
||||||
|
{
|
||||||
|
if (($scopedPolicy.Scope -eq "Process") -or
|
||||||
|
($scopedPolicy.Scope -eq "CurrentUser"))
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
Set-ExecutionPolicy -Scope $scopedPolicy.Scope -ExecutionPolicy $scopedPolicy.ExecutionPolicy -Force
|
||||||
|
}
|
||||||
|
catch {
|
||||||
|
if ($_.FullyQualifiedErrorId -ne "ExecutionPolicyOverride,Microsoft.PowerShell.Commands.SetExecutionPolicyCommand")
|
||||||
|
{
|
||||||
|
# Re-throw unrecognized exceptions. Otherwise, swallow
|
||||||
|
# the exception that warns about overridden policies
|
||||||
|
throw $_
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
elseif($scopedPolicy.Scope -eq "LocalMachine")
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
Set-ExecutionPolicy -Scope $scopedPolicy.Scope -ExecutionPolicy $scopedPolicy.ExecutionPolicy -Force
|
||||||
|
}
|
||||||
|
catch {
|
||||||
|
if ($_.FullyQualifiedErrorId -eq "System.UnauthorizedAccessException,Microsoft.PowerShell.Commands.SetExecutionPolicyCommand")
|
||||||
|
{
|
||||||
|
# Do nothing. Depending on the ownership of the file,
|
||||||
|
# regular users may or may not be able to set its
|
||||||
|
# value.
|
||||||
|
#
|
||||||
|
# When targetting the Registry, regular users cannot
|
||||||
|
# modify this value.
|
||||||
|
}
|
||||||
|
elseif ($_.FullyQualifiedErrorId -ne "ExecutionPolicyOverride,Microsoft.PowerShell.Commands.SetExecutionPolicyCommand")
|
||||||
|
{
|
||||||
|
# Re-throw unrecognized exceptions. Otherwise, swallow
|
||||||
|
# the exception that warns about overridden policies
|
||||||
|
throw $_
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
It "-Scope MachinePolicy is not Modifiable" {
|
||||||
|
VerfiyBlockedSetExecutionPolicy "MachinePolicy"
|
||||||
|
}
|
||||||
|
|
||||||
|
It "-Scope UserPolicy is not Modifiable" {
|
||||||
|
VerfiyBlockedSetExecutionPolicy "UserPolicy"
|
||||||
|
}
|
||||||
|
|
||||||
|
It "-Scope Process is Settable" {
|
||||||
|
Set-ExecutionPolicy -Scope Process -ExecutionPolicy ByPass
|
||||||
|
Get-ExecutionPolicy -Scope Process | Should Be "ByPass"
|
||||||
|
}
|
||||||
|
|
||||||
|
It "-Scope CurrentUser is Settable" {
|
||||||
|
Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy ByPass
|
||||||
|
Get-ExecutionPolicy -Scope CurrentUser | Should Be "ByPass"
|
||||||
|
}
|
||||||
|
|
||||||
|
# This test requires Administrator privileges on Windows.
|
||||||
|
It "-Scope LocalMachine is Settable" {
|
||||||
|
Set-ExecutionPolicy -Scope LocalMachine -ExecutionPolicy ByPass
|
||||||
|
Get-ExecutionPolicy -Scope LocalMachine | Should Be "ByPass"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Describe "Validate that 'ConvertTo-SecureString -Key' and 'ConvertFrom-SecureString -Key' work on NanoServer and IoT" -Tags "CI" {
|
Describe "Validate that 'ConvertTo-SecureString -Key' and 'ConvertFrom-SecureString -Key' work on NanoServer and IoT" -Tags "CI" {
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user