8000 Move group policy settings and enable policy controlled logging in Po… · PowerShell/PowerShell@658960e · GitHub
[go: up one dir, main page]

Skip to content

Commit 658960e

Browse files
daxian-dbwTravisEz13
authored andcommitted
Move group policy settings and enable policy controlled logging in PowerShell Core (#5791)
Make PowerShell Core reads group policy settings from different registry keys (Windows only) and the configuration files (both Windows and Unix). - On Windows, move to different GPO registry keys. - On both Windows and Unix, read GPO related settings from the configuration file `powershell.config.json`. - On Windows, the policy settings in registry take precedence over the configuration file. - Enable policy controlled logging and transcription on Unix.
1 parent 0f60625 commit 658960e

31 files changed

+1480
-764
lines changed

build.psm1

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1364,7 +1364,8 @@ function Test-PSPesterResults
13641364

13651365
function Start-PSxUnit {
13661366
[CmdletBinding()]param(
1367-
[string] $TestResultsFile = "XUnitResults.xml"
1367+
[string] $SequentialTestResultsFile = "SequentialXUnitResults.xml",
1368+
[string] $ParallelTestResultsFile = "ParallelXUnitResults.xml"
13681369
)
13691370

13701371
# Add .NET CLI tools to PATH
@@ -1375,9 +1376,11 @@ function Start-PSxUnit {
13751376
throw "PowerShell must be built before running tests!"
13761377
}
13771378

1378-
if(Test-Path $TestResultsFile)
1379-
{
1380-
Remove-Item $TestResultsFile -Force -ErrorAction SilentlyContinue
1379+
if (Test-Path $SequentialTestResultsFile) {
1380+
Remove-Item $SequentialTestResultsFile -Force -ErrorAction SilentlyContinue
1381+
}
1382+
if (Test-Path $ParallelTestResultsFile) {
1383+
Remove-Item $ParallelTestResultsFile -Force -ErrorAction SilentlyContinue
13811384
}
13821385

13831386
try {
@@ -1386,12 +1389,7 @@ function Start-PSxUnit {
13861389
# Path manipulation to obtain test project output directory
13871390
dotnet restore
13881391

1389-
# --fx-version workaround required due to https://github.com/dotnet/cli/issues/7901#issuecomment-343323674
1390-
if($Environment.IsWindows)
1391-
{
1392-
dotnet xunit --fx-version 2.0.0 -xml $TestResultsFile
1393-
}
1394-
else
1392+
if(-not $Environment.IsWindows)
13951393
{
13961394
if($Environment.IsMacOS)
13971395
{
@@ -1419,9 +1417,12 @@ function Start-PSxUnit {
14191417
{
14201418
throw "Dependencies $requiredDependencies not met."
14211419
}
1422-
1423-
dotnet xunit --fx-version 2.0.0 -configuration $Options.configuration -xml $TestResultsFile
14241420
}
1421+
1422+
# '-fxversion' workaround required due to https://github.com/dotnet/cli/issues/7901#issuecomment-343323674
1423+
# Run sequential tests first, and then run the tests that can execute in parallel
1424+
dotnet xunit -fxversion 2.0.0 -configuration $Options.configuration -xml $SequentialTestResultsFile -namespace "PSTests.Sequential" -parallel none
1425+
dotnet xunit -fxversion 2.0.0 -configuration $Options.configuration -xml $ParallelTestResultsFile -namespace "PSTests.Parallel" -nobuild
14251426
}
14261427
finally {
14271428
Pop-Location

src/Microsoft.PowerShell.ConsoleHost/host/msh/CommandLineParameterParser.cs

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
using System.IO;
1111
using System.Collections.ObjectModel;
1212
using System.Management.Automation;
13+
using System.Management.Automation.Configuration;
1314
using System.Management.Automation.Runspaces;
1415
using System.Management.Automation.Internal;
1516
using System.Diagnostics;
@@ -459,29 +460,17 @@ internal void Parse(string[] args)
459460
}
460461
}
461462

462-
private static string s_groupPolicyBase = @"Software\Policies\Microsoft\Windows\PowerShell";
463-
private static string s_consoleSessionConfigurationKey = "ConsoleSessionConfiguration";
464-
private static string s_enableConsoleSessionConfiguration = "EnableConsoleSessionConfiguration";
465-
private static string s_consoleSessionConfigurationName = "ConsoleSessionConfigurationName";
466463
private static string GetConfigurationNameFromGroupPolicy()
467464
{
468465
// Current user policy takes precedence.
469-
var groupPolicySettings = Utils.GetGroupPolicySetting(s_groupPolicyBase, s_consoleSessionConfigurationKey, Utils.RegCurrentUserThenLocalMachine);
470-
if (groupPolicySettings != null)
466+
var consoleSessionSetting = Utils.GetPolicySetting<ConsoleSessionConfiguration>(Utils.CurrentUserThenSystemWideConfig);
467+
if (consoleSessionSetting != null)
471468
{
472-
object keyValue;
473-
if (groupPolicySettings.TryGetValue(s_enableConsoleSessionConfiguration, out keyValue))
469+
if (consoleSessionSetting.EnableConsoleSessionConfiguration == true)
474470
{
475-
if (String.Equals(keyValue.ToString(), "1", StringComparison.OrdinalIgnoreCase))
471+
if (!string.IsNullOrEmpty(consoleSessionSetting.ConsoleSessionConfigurationName))
476472
{
477-
if (groupPolicySettings.TryGetValue(s_consoleSessionConfigurationName, out keyValue))
478-
{
479-
string consoleSessionConfigurationName = keyValue.ToString();
480-
if (!string.IsNullOrEmpty(consoleSessionConfigurationName))
481-
{
482-
return consoleSessionConfigurationName;
483-
}
484-
}
473+
return consoleSessionSetting.ConsoleSessionConfigurationName;
485474
}
486475
}
487476
}

src/System.Management.Automation/engine/CommandDiscovery.cs

Lines changed: 0 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1643,59 +1643,6 @@ private int GetCmdletRemovalIndex(List<CmdletInfo> cacheEntry, string PSSnapin)
16431643

16441644
internal ExecutionContext Context { get; }
16451645

1646-
/// <summary>
1647-
/// Reads the path for the appropriate shellID from the registry.
1648-
/// </summary>
1649-
///
1650-
/// <param name="shellID">
1651-
/// The ID of the shell to retrieve the path for.
1652-
/// </param>
1653-
///
1654-
/// <returns>
1655-
/// The path to the shell represented by the shellID.
1656-
/// </returns>
1657-
///
1658-
/// <remarks>
1659-
/// The shellID must be registered in the Windows Registry in either
1660-
/// the HKEY_CURRENT_USER or HKEY_LOCAL_MACHINE hive under
1661-
/// Software/Microsoft/MSH/&lt;ShellID&gt; and are searched in that order.
1662-
/// </remarks>
1663-
///
1664-
internal static string GetShellPathFromRegistry(string shellID)
1665-
{
1666-
string result = null;
1667-
1668-
#if !UNIX
1669-
try
1670-
{
1671-
RegistryKey shellKey = Registry.LocalMachine.OpenSubKey(Utils.GetRegistryConfigurationPath(shellID));
1672-
if (shellKey != null)
1673-
{
1674-
// verify the value kind as a string
1675-
RegistryValueKind kind = shellKey.GetValueKind("path");
1676-
1677-
if (kind == RegistryValueKind.ExpandString ||
1678-
kind == RegistryValueKind.String)
1679-
{
1680-
result = shellKey.GetValue("path") as string;
1681-
}
1682-
}
1683-
}
1684-
// Ignore these exceptions and return an empty or null result
1685-
catch (SecurityException)
1686-
{
1687-
}
1688-
catch (IOException)
1689-
{
1690-
}
1691-
catch (ArgumentException)
1692-
{
1693-
}
1694-
#endif
1695-
1696-
return result;
1697-
}
1698-
16991646
internal static PSModuleAutoLoadingPreference GetCommandDiscoveryPreference(ExecutionContext context, VariablePath variablePath, string environmentVariable)
17001647
{
17011648
Dbg.Assert(context != null, "context cannot be Null");

src/System.Management.Automation/engine/Modules/ModuleCmdletBase.cs

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
using System.IO;
1212
using System.Linq;
1313
using System.Management.Automation;
14+
using System.Management.Automation.Configuration;
1415
using System.Management.Automation.Internal;
1516
using System.Management.Automation.Runspaces;
1617
using System.Management.Automation.Security;
@@ -4749,28 +4750,18 @@ internal static ModuleLoggingGroupPolicyStatus GetModuleLoggingInformation(out I
47494750
{
47504751
moduleNames = null;
47514752
ModuleLoggingGroupPolicyStatus status = ModuleLoggingGroupPolicyStatus.Undefined;
4752-
Dictionary<string, object> groupPolicySettings = Utils.GetGroupPolicySetting("ModuleLogging", Utils.RegLocalMachineThenCurrentUser);
47534753

4754-
if (groupPolicySettings != null)
4754+
var moduleLogging = Utils.GetPolicySetting<ModuleLogging>(Utils.SystemWideThenCurrentUserConfig);
4755+
if (moduleLogging != null)
47554756
{
4756-
object enableModuleLoggingValue = null;
4757-
if (groupPolicySettings.TryGetValue("EnableModuleLogging", out enableModuleLoggingValue))
4757+
if (moduleLogging.EnableModuleLogging == false)
47584758
{
4759-
if (String.Equals(enableModuleLoggingValue.ToString(), "0", StringComparison.OrdinalIgnoreCase))
4760-
{
4761-
return ModuleLoggingGroupPolicyStatus.Disabled;
4762-
}
4763-
4764-
if (String.Equals(enableModuleLoggingValue.ToString(), "1", StringComparison.OrdinalIgnoreCase))
4765-
{
4766-
status = ModuleLoggingGroupPolicyStatus.Enabled;
4767-
4768-
object moduleNamesValue = null;
4769-
if (groupPolicySettings.TryGetValue("ModuleNames", out moduleNamesValue))
4770-
{
4771-
moduleNames = new List<String>((string[])moduleNamesValue);
4772-
}
4773-
}
4759+
status = ModuleLoggingGroupPolicyStatus.Disabled;
4760+
}
4761+
else if (moduleLogging.EnableModuleLogging == true)
4762+
{
4763+
status = ModuleLoggingGroupPolicyStatus.Enabled;
4764+
moduleNames = moduleLogging.ModuleNames;
47744765
}
47754766
}
47764767

src/System.Management.Automation/engine/Modules/ModuleIntrinsics.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
using System.Collections.Generic;
66
using System.IO;
7+
using System.Management.Automation.Configuration;
78
using System.Management.Automation.Internal;
89
using System.Management.Automation.Language;
910
using Microsoft.PowerShell.Commands;
@@ -977,8 +978,8 @@ internal static string GetModulePath()
977978
internal static string SetModulePath()
978979
{
979980
string currentModulePath = GetExpandedEnvironmentVariable(Constants.PSModulePathEnvVar, EnvironmentVariableTarget.Process);
980-
string systemWideModulePath = ConfigPropertyAccessor.Instance.GetModulePath(ConfigPropertyAccessor.PropertyScope.SystemWide);
981-
string personalModulePath = ConfigPropertyAccessor.Instance.GetModulePath(ConfigPropertyAccessor.PropertyScope.CurrentUser);
981+
string systemWideModulePath = PowerShellConfig.Instance.GetModulePath(ConfigScope.SystemWide);
982+
string personalModulePath = PowerShellConfig.Instance.GetModulePath(ConfigScope.CurrentUser);
982983

983984
string newModulePathString = GetModulePath(currentModulePath, systemWideModulePath, personalModulePath);
984985

0 commit comments

Comments
 (0)
0