-
Notifications
You must be signed in to change notification settings - Fork 21
Add GPO support for WindowsAdvancedSettings #14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
dkbennett
wants to merge
2
commits into
main
Choose a base branch
from
user/dkbennett/gpo
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| using Microsoft.Win32; | ||
| using Serilog; | ||
|
|
||
| namespace WindowsAdvancedSettings.Common.Helpers; | ||
|
|
||
| public class GPOHelper | ||
| { | ||
| private static readonly ILogger _log = Log.ForContext("SourceContext", nameof(GPOHelper)); | ||
|
|
||
| private enum GpoRuleConfigured | ||
| { | ||
| WrongValue = -3, // The policy is set to an unrecognized value | ||
| Unavailable = -2, // Couldn't access registry | ||
| NotConfigured = -1, // Policy is not configured | ||
| Disabled = 0, // Policy is disabled | ||
| Enabled = 1, // Policy is enabled | ||
| } | ||
|
|
||
| // Registry path where gpo policy values are stored | ||
| private const string PoliciesScopeMachine = "HKEY_LOCAL_MACHINE"; | ||
| private const string PoliciesPath = @"\SOFTWARE\Policies\Microsoft\Windows\WindowsAdvancedSettings"; | ||
|
|
||
| // Registry value names | ||
| private const string PolicyConfigureEnabledWindowsAdvancedSettings = "ConfigureEnabledWindowsAdvancedSettings"; | ||
|
|
||
| private static GpoRuleConfigured GetConfiguredValue(string registryValueName) | ||
| { | ||
| try | ||
| { | ||
| var rawValue = Registry.GetValue( | ||
| keyName: PoliciesScopeMachine + PoliciesPath, | ||
| valueName: registryValueName, | ||
| defaultValue: GpoRuleConfigured.NotConfigured); | ||
|
|
||
| _log.Debug($"Registry value {registryValueName} set to {rawValue}"); | ||
|
|
||
| // Value will be null if the subkey specified by keyName does not exist. | ||
| if (rawValue == null) | ||
| { | ||
| return GpoRuleConfigured.NotConfigured; | ||
| } | ||
| else if (rawValue is not int && rawValue is not GpoRuleConfigured) | ||
| { | ||
| return GpoRuleConfigured.WrongValue; | ||
| } | ||
| else | ||
| { | ||
| return (GpoRuleConfigured)rawValue; | ||
| } | ||
| } | ||
| catch (System.Security.SecurityException) | ||
| { | ||
| // The user does not have the permissions required to read from the registry key. | ||
| return GpoRuleConfigured.Unavailable; | ||
| } | ||
| catch (System.IO.IOException) | ||
| { | ||
| // The RegistryKey that contains the specified value has been marked for deletion. | ||
| return GpoRuleConfigured.Unavailable; | ||
| } | ||
| catch (System.ArgumentException) | ||
| { | ||
| // keyName does not begin with a valid registry root. | ||
| return GpoRuleConfigured.NotConfigured; | ||
| } | ||
| } | ||
|
|
||
| private static bool EvaluateConfiguredValue(string registryValueName, GpoRuleConfigured defaultValue) | ||
| { | ||
| var configuredValue = GetConfiguredValue(registryValueName); | ||
| if (configuredValue < 0) | ||
| { | ||
| if (configuredValue != GpoRuleConfigured.NotConfigured) | ||
| { | ||
| // Only log an error if a configuration was attempted but was incorrect. NotConfigured is expected state. | ||
| _log.Error($"Registry value {registryValueName} set to {configuredValue}, using default {defaultValue} instead."); | ||
| } | ||
|
|
||
| configuredValue = defaultValue; | ||
| } | ||
|
|
||
| return configuredValue == GpoRuleConfigured.Enabled; | ||
| } | ||
|
|
||
| public static bool GetConfiguredEnabledWindowsAdvancedSettingsValue() | ||
| { | ||
| var defaultValue = GpoRuleConfigured.Enabled; | ||
| return EvaluateConfiguredValue(PolicyConfigureEnabledWindowsAdvancedSettings, defaultValue); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| <?xml version="1.0" encoding="utf-8"?> | ||
| <!-- Copyright (c) Microsoft Corporation. | ||
| Licensed under the MIT License. --> | ||
| <policyDefinitions xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" revision="0.1900" schemaVersion="1.0" xmlns="http://schemas.microsoft.com/GroupPolicy/2006/07/PolicyDefinitions"> | ||
| <policyNamespaces> | ||
| <target prefix="devhome" namespace="Microsoft.Policies.DevHome" /> | ||
| </policyNamespaces> | ||
| <resources minRequiredRevision="0.1900"/> | ||
| <supportedOn> | ||
| <definitions> | ||
| <definition name="SUPPORTED_DEVHOME_0_1900" displayName="$(string.SUPPORTED_DEVHOME_0_1900)"/> | ||
| </definitions> | ||
| </supportedOn> | ||
| <categories> | ||
| <category name="DevHome" displayName="$(string.DevHome)" /> | ||
| </categories> | ||
|
|
||
| <policies> | ||
|
|
||
| <!--The name (id) of the policy is different from the valueName to sort it as first policy in edit dialog. The order is sorted alphabetically based on the "name" property.--> | ||
| <policy name="ConfigureAllDevHomeEnabledState" class="Both" displayName="$(string.ConfigureAllDevHomeEnabledState)" explainText="$(string.ConfigureAllDevHomeEnabledStateDescription)" key="Software\Policies\DevHome" valueName="ConfigureEnabledDevHome"> | ||
| <parentCategory ref="DevHome" /> | ||
| <supportedOn ref="SUPPORTED_DEVHOME_0_1901" /> | ||
| <enabledValue> | ||
| <decimal value="1" /> | ||
| </enabledValue> | ||
| <disabledValue> | ||
| <decimal value="0" /> | ||
| </disabledValue> | ||
| </policy> | ||
| </policies> | ||
| </policyDefinitions> | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why are there two copies of this file? |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| <?xml version="1.0" encoding="utf-8"?> | ||
| <!-- Copyright (c) Microsoft Corporation. | ||
| Licensed under the MIT License. --> | ||
| <policyDefinitions xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" revision="0.1900" schemaVersion="1.0" xmlns="http://schemas.microsoft.com/GroupPolicy/2006/07/PolicyDefinitions"> | ||
| <policyNamespaces> | ||
| <target prefix="windowsadvancedsettings" namespace="Microsoft.Policies.WindowsAdvancedSettings" /> | ||
| </policyNamespaces> | ||
| <resources minRequiredRevision="0.1900"/> | ||
| <supportedOn> | ||
| <definitions> | ||
| <definition name="SUPPORTED_WINDOWSADVANCEDSETTINGS_0_1900" displayName="$(string.SUPPORTED_WINDOWSADVANCEDSETTINGS_0_1900)"/> | ||
| </definitions> | ||
| </supportedOn> | ||
| <categories> | ||
| <category name="WindowsAdvancedSettings" displayName="$(string.WindowsAdvancedSettings)" /> | ||
| </categories> | ||
|
|
||
| <policies> | ||
|
|
||
| <!--The name (id) of the policy is different from the valueName to sort it as first policy in edit dialog. The order is sorted alphabetically based on the "name" property.--> | ||
| <policy name="ConfigureAllWindowsAdvancedSettingsEnabledState" class="Both" displayName="$(string.ConfigureAllWindowsAdvancedSettingsEnabledState)" explainText="$(string.ConfigureAllWindowsAdvancedSettingsEnabledStateDescription)" key="Software\Policies\Microsoft\Windows\WindowsAdvancedSettings" valueName="ConfigureEnabledWindowsAdvancedSettings"> | ||
| <parentCategory ref="DevHome" /> | ||
| <supportedOn ref="SUPPORTED_WINDOWSADVANCEDSETTINGS_0_1900" /> | ||
| <enabledValue> | ||
| <decimal value="1" /> | ||
| </enabledValue> | ||
| <disabledValue> | ||
| <decimal value="0" /> | ||
| </disabledValue> | ||
| </policy> | ||
| </policies> | ||
| </policyDefinitions> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| <?xml version="1.0" encoding="utf-8"?> | ||
| <!-- Copyright (c) Microsoft Corporation. | ||
| Licensed under the MIT License. --> | ||
| <policyDefinitionResources xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" revision="0.1900" schemaVersion="1.0" xmlns="http://schemas.microsoft.com/GroupPolicy/2006/07/PolicyDefinitions"> | ||
| <displayName>Windows Advanced Settings</displayName> | ||
| <description>Windows Advanced Settings</description> | ||
| <resources> | ||
| <stringTable> | ||
| <string id="WindowsAdvancedSettings">Microsoft Windows Advanced Settings</string> | ||
|
|
||
| <string id="SUPPORTED_WINDOWSADVANCEDSETTINGS_0_1900">Windows Advanced Settings version 0.1900.* or later</string> | ||
|
|
||
| <string id="ConfigureAllWindowsAdvancedSettingsEnabledStateDescription"> | ||
| This policy configures the enabled state for Windows Advanced Settings. | ||
|
|
||
| If you enable this setting, Windows Advanced Settings will be always enabled and the user won't be able to disable it. | ||
|
|
||
| If you disable this setting, Windows Advanced Settings will be always disabled and the user won't be able to enable it. | ||
|
|
||
| If you don't configure this setting, users are able to enable or disable Windows Advanced Settings. | ||
|
|
||
| This policy will override any enabled state policies for individual Windows Advanced Settings features. | ||
| </string> | ||
|
|
||
| <string id="ConfigureAllWindowsAdvancedSettingsEnabledState">Configure Windows Advanced Settings enabled state</string> | ||
| </stringTable> | ||
|
|
||
| </resources> | ||
| </policyDefinitionResources> | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We still using the DevHome name here?