Hi @Thakur, Apurva [EMR/SYSS/PSS/PUNE] , Welcome to Microsoft Q&A,
You seem to be overlooking the critical steps of accessing and updating the policy. In your code, you have successfully obtained the GPO object and the computer configuration, but you need to further access the policy object under the computer configuration and update it. Likewise, you need to do the same to access policies under User Configuration. Try the following code:
GPDomain _gpDomain = new GPDomain();
GPO gpoTarget = _gpDomain.GetGPO(gpo.DisplayName);
//Access policies under computer configuration
PolicySettings computerPolicySettings = gpoTarget.Computer.Policy;
//Access policies under user configuration
PolicySettings userPolicySettings = gpoTarget.User.Policy;
// Example of updating local policy
RegistryPolicy computerRegistryPolicy = computerPolicySettings.GetRegistry(true);
RegistryPolicy userRegistryPolicy = userPolicySettings.GetRegistry(true);
// Add or update registry policy under computer configuration
RegistryPolicySetting computerRegistryPolicySetting = new RegistryPolicySetting("PathToRegistryKey", "RegistryValueName", "RegistryValueData");
computerRegistryPolicy.Set(computerRegistryPolicySetting);
// Add or update registry policy under user configuration
RegistryPolicySetting userRegistryPolicySetting = new RegistryPolicySetting("PathToRegistryKey", "RegistryValueName", "RegistryValueData");
userRegistryPolicy.Set(userRegistryPolicySetting);
// save Changes
gpoTarget.Save();
// Generate report
string report = gpoTarget.GenerateReport(ReportType.Xml);
// Get permission information
GPPermissionCollection gPPermissions = gpoTarget.GetSecurityInfo();
Best Regards,
Jiale
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.