public void Delete() { var result = Instance.Delete(); if (result != 0) { throw new Exception("Error deleting the GPO"); } Instance = null; }
/// <summary> /// Constructor. /// </summary> /// <param name="remoteMachineName">Target machine name to operate group policy</param> /// <exception cref="System.Runtime.InteropServices.COMException">Throw when com execution throws exceptions</exception> public GroupPolicyObjectHandler(String remoteMachineName) { groupPolicyObject = new GroupPolicyObject(); iGroupPolicyObject = (IGroupPolicyObject)groupPolicyObject; try { if (String.IsNullOrEmpty(remoteMachineName)) { iGroupPolicyObject.OpenLocalMachineGPO(GPO_OPEN_LOAD_REGISTRY); } else { iGroupPolicyObject.OpenRemoteMachineGPO(remoteMachineName, GPO_OPEN_LOAD_REGISTRY); } } catch (COMException e) { throw e; } }
private void DeleteUserDelegateSettings() { IGroupPolicyObject gPClass = (IGroupPolicyObject)(new GPClass()); gPClass.OpenLocalMachineGPO(1); gPClass.GetRegistryKey(2); RegistryKey currentUser = Registry.CurrentUser; string str = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Group Policy Objects"; RegistryKey registryKey = currentUser.OpenSubKey(str, true); string[] subKeyNames = registryKey.GetSubKeyNames(); for (int i = 0; i < (int)subKeyNames.Length; i++) { string str1 = subKeyNames[i]; if (str1.EndsWith("Machine", StringComparison.OrdinalIgnoreCase)) { string str2 = string.Concat(str, "\\", str1, "\\Software\\Policies\\Microsoft\\Windows"); this.DeleteDelegateSettings("wsman", Registry.CurrentUser, str2, gPClass); } } }
private void DeleteUserDelegateSettings() { System.IntPtr KeyHandle = System.IntPtr.Zero; IGroupPolicyObject GPO = (IGroupPolicyObject) new GPClass(); GPO.OpenLocalMachineGPO(1); KeyHandle = GPO.GetRegistryKey(2); RegistryKey rootKey = Registry.CurrentUser; string GPOpath = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Group Policy Objects"; RegistryKey GPOKey = rootKey.OpenSubKey(GPOpath, true); foreach (string keyname in GPOKey.GetSubKeyNames()) { if (keyname.EndsWith("Machine", StringComparison.OrdinalIgnoreCase)) { string key = GPOpath + "\\" + keyname + "\\" + @"Software\Policies\Microsoft\Windows"; DeleteDelegateSettings(applicationname, Registry.CurrentUser, key, GPO); } } KeyHandle = System.IntPtr.Zero; }
/// <summary> /// </summary> private void UpdateCurrentUserRegistrySettings() { System.IntPtr KeyHandle = System.IntPtr.Zero; IGroupPolicyObject GPO = (IGroupPolicyObject) new GPClass(); GPO.OpenLocalMachineGPO(1); KeyHandle = GPO.GetRegistryKey(2); RegistryKey rootKey = Registry.CurrentUser; const string GPOpath = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Group Policy Objects"; RegistryKey GPOKey = rootKey.OpenSubKey(GPOpath, true); foreach (string keyname in GPOKey.GetSubKeyNames()) { if (keyname.EndsWith("Machine", StringComparison.OrdinalIgnoreCase)) { string key = GPOpath + "\\" + keyname + "\\" + @"Software\Policies\Microsoft\Windows"; UpdateGPORegistrySettings(applicationname, this.delegatecomputer, Registry.CurrentUser, key); } } // saving gpo settings GPO.Save(true, true, new Guid("35378EAC-683F-11D2-A89A-00C04FBBCFA2"), new Guid("7A9206BD-33AF-47af-B832-D4128730E990")); }
private void UpdateCurrentUserRegistrySettings() { IGroupPolicyObject gPClass = (IGroupPolicyObject)(new GPClass()); gPClass.OpenLocalMachineGPO(1); gPClass.GetRegistryKey(2); RegistryKey currentUser = Registry.CurrentUser; string str = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Group Policy Objects"; RegistryKey registryKey = currentUser.OpenSubKey(str, true); string[] subKeyNames = registryKey.GetSubKeyNames(); for (int i = 0; i < (int)subKeyNames.Length; i++) { string str1 = subKeyNames[i]; if (str1.EndsWith("Machine", StringComparison.OrdinalIgnoreCase)) { string str2 = string.Concat(str, "\\", str1, "\\Software\\Policies\\Microsoft\\Windows"); this.UpdateGPORegistrySettings("wsman", this.delegatecomputer, Registry.CurrentUser, str2); } } gPClass.Save(true, true, new Guid("35378EAC-683F-11D2-A89A-00C04FBBCFA2"), new Guid("7A9206BD-33AF-47af-B832-D4128730E990")); }
private void DeleteDelegateSettings(string applicationname, RegistryKey rootKey, string Registry_Path, IGroupPolicyObject GPO) { WSManHelper helper = new WSManHelper(this); RegistryKey rKey; int i = 0; bool otherkeys = false; try { string Registry_Path_Credentials_Delegation = Registry_Path + @"\CredentialsDelegation"; RegistryKey Allow_Fresh_Credential_Key = rootKey.OpenSubKey(Registry_Path_Credentials_Delegation + @"\" + helper.Key_Allow_Fresh_Credentials, true); if (Allow_Fresh_Credential_Key != null) { string[] valuenames = Allow_Fresh_Credential_Key.GetValueNames(); if (valuenames.Length > 0) { Collection <string> KeyCollection = new Collection <string>(); foreach (string value in valuenames) { object keyvalue = Allow_Fresh_Credential_Key.GetValue(value); if (keyvalue != null) { if (!keyvalue.ToString().StartsWith(applicationname, StringComparison.OrdinalIgnoreCase)) { KeyCollection.Add(keyvalue.ToString()); otherkeys = true; } } Allow_Fresh_Credential_Key.DeleteValue(value); } foreach (string keyvalue in KeyCollection) { Allow_Fresh_Credential_Key.SetValue(Convert.ToString(i + 1, CultureInfo.InvariantCulture), keyvalue, RegistryValueKind.String); i++; } } } if (!otherkeys) { rKey = rootKey.OpenSubKey(Registry_Path_Credentials_Delegation, true); if (rKey != null) { object regval1 = rKey.GetValue(helper.Key_Allow_Fresh_Credentials); if (regval1 != null) { rKey.DeleteValue(helper.Key_Allow_Fresh_Credentials, false); } object regval2 = rKey.GetValue(helper.Key_Concatenate_Defaults_AllowFresh); if (regval2 != null) { rKey.DeleteValue(helper.Key_Concatenate_Defaults_AllowFresh, false); } if (rKey.OpenSubKey(helper.Key_Allow_Fresh_Credentials) != null) { rKey.DeleteSubKeyTree(helper.Key_Allow_Fresh_Credentials); } } } GPO.Save(true, true, new Guid("35378EAC-683F-11D2-A89A-00C04FBBCFA2"), new Guid("6AD20875-336C-4e22-968F-C709ACB15814")); } catch (InvalidOperationException ex) { ErrorRecord er = new ErrorRecord(ex, "InvalidOperation", ErrorCategory.InvalidOperation, null); WriteError(er); } catch (ArgumentException ex) { ErrorRecord er = new ErrorRecord(ex, "InvalidArgument", ErrorCategory.InvalidArgument, null); WriteError(er); } catch (SecurityException ex) { ErrorRecord er = new ErrorRecord(ex, "SecurityException", ErrorCategory.SecurityError, null); WriteError(er); } catch (UnauthorizedAccessException ex) { ErrorRecord er = new ErrorRecord(ex, "UnauthorizedAccess", ErrorCategory.SecurityError, null); WriteError(er); } }
internal GroupPolicyObject() { Instance = GetInstance(); }
private void DeleteDelegateSettings(string applicationname, RegistryKey rootKey, string Registry_Path, IGroupPolicyObject GPO) { WSManHelper helper = new WSManHelper(this); RegistryKey rKey; int i = 0; bool otherkeys = false; try { string Registry_Path_Credentials_Delegation = Registry_Path + @"\CredentialsDelegation"; RegistryKey Allow_Fresh_Credential_Key = rootKey.OpenSubKey(Registry_Path_Credentials_Delegation + @"\" + helper.Key_Allow_Fresh_Credentials, true); if (Allow_Fresh_Credential_Key != null) { string[] valuenames = Allow_Fresh_Credential_Key.GetValueNames(); if (valuenames.Length > 0) { Collection<string> KeyCollection = new Collection<string>(); foreach (string value in valuenames) { object keyvalue = Allow_Fresh_Credential_Key.GetValue(value); if (keyvalue != null) { if (!keyvalue.ToString().StartsWith(applicationname, StringComparison.OrdinalIgnoreCase)) { KeyCollection.Add(keyvalue.ToString()); otherkeys = true; } } Allow_Fresh_Credential_Key.DeleteValue(value); } foreach (string keyvalue in KeyCollection) { Allow_Fresh_Credential_Key.SetValue(Convert.ToString(i + 1, CultureInfo.InvariantCulture), keyvalue, RegistryValueKind.String); i++; } } } if (!otherkeys) { rKey = rootKey.OpenSubKey(Registry_Path_Credentials_Delegation, true); if (rKey != null) { object regval1 = rKey.GetValue(helper.Key_Allow_Fresh_Credentials); if (regval1 != null) { rKey.DeleteValue(helper.Key_Allow_Fresh_Credentials, false); } object regval2 = rKey.GetValue(helper.Key_Concatenate_Defaults_AllowFresh); if (regval2 != null) { rKey.DeleteValue(helper.Key_Concatenate_Defaults_AllowFresh, false); } if (rKey.OpenSubKey(helper.Key_Allow_Fresh_Credentials) != null) { rKey.DeleteSubKeyTree(helper.Key_Allow_Fresh_Credentials); } } } GPO.Save(true, true, new Guid("35378EAC-683F-11D2-A89A-00C04FBBCFA2"), new Guid("6AD20875-336C-4e22-968F-C709ACB15814")); } catch (InvalidOperationException ex) { ErrorRecord er = new ErrorRecord(ex, "InvalidOperation", ErrorCategory.InvalidOperation, null); WriteError(er); } catch (ArgumentException ex) { ErrorRecord er = new ErrorRecord(ex, "InvalidArgument", ErrorCategory.InvalidArgument, null); WriteError(er); } catch (SecurityException ex) { ErrorRecord er = new ErrorRecord(ex, "SecurityException", ErrorCategory.SecurityError, null); WriteError(er); } catch (UnauthorizedAccessException ex) { ErrorRecord er = new ErrorRecord(ex, "UnauthorizedAccess", ErrorCategory.SecurityError, null); WriteError(er); } }
private void DeleteDelegateSettings(string applicationname, RegistryKey rootKey, string Registry_Path, IGroupPolicyObject GPO) { WSManHelper wSManHelper = new WSManHelper(this); int num = 0; bool flag = false; try { string str = string.Concat(Registry_Path, "\\CredentialsDelegation"); RegistryKey registryKey = rootKey.OpenSubKey(string.Concat(str, "\\", wSManHelper.Key_Allow_Fresh_Credentials), true); if (registryKey != null) { string[] valueNames = registryKey.GetValueNames(); if ((int)valueNames.Length > 0) { Collection <string> strs = new Collection <string>(); string[] strArrays = valueNames; for (int i = 0; i < (int)strArrays.Length; i++) { string str1 = strArrays[i]; object value = registryKey.GetValue(str1); if (value != null && !value.ToString().StartsWith(applicationname, StringComparison.OrdinalIgnoreCase)) { strs.Add(value.ToString()); flag = true; } registryKey.DeleteValue(str1); } foreach (string str2 in strs) { registryKey.SetValue(Convert.ToString(num + 1, CultureInfo.InvariantCulture), str2, RegistryValueKind.String); num++; } } } if (!flag) { RegistryKey registryKey1 = rootKey.OpenSubKey(str, true); if (registryKey1 != null) { object obj = registryKey1.GetValue(wSManHelper.Key_Allow_Fresh_Credentials); if (obj != null) { registryKey1.DeleteValue(wSManHelper.Key_Allow_Fresh_Credentials, false); } object value1 = registryKey1.GetValue(wSManHelper.Key_Concatenate_Defaults_AllowFresh); if (value1 != null) { registryKey1.DeleteValue(wSManHelper.Key_Concatenate_Defaults_AllowFresh, false); } if (registryKey1.OpenSubKey(wSManHelper.Key_Allow_Fresh_Credentials) != null) { registryKey1.DeleteSubKeyTree(wSManHelper.Key_Allow_Fresh_Credentials); } } } GPO.Save(true, true, new Guid("35378EAC-683F-11D2-A89A-00C04FBBCFA2"), new Guid("6AD20875-336C-4e22-968F-C709ACB15814")); } catch (InvalidOperationException invalidOperationException1) { InvalidOperationException invalidOperationException = invalidOperationException1; ErrorRecord errorRecord = new ErrorRecord(invalidOperationException, "InvalidOperation", ErrorCategory.InvalidOperation, null); base.WriteError(errorRecord); } catch (ArgumentException argumentException1) { ArgumentException argumentException = argumentException1; ErrorRecord errorRecord1 = new ErrorRecord(argumentException, "InvalidArgument", ErrorCategory.InvalidArgument, null); base.WriteError(errorRecord1); } catch (SecurityException securityException1) { SecurityException securityException = securityException1; ErrorRecord errorRecord2 = new ErrorRecord(securityException, "SecurityException", ErrorCategory.SecurityError, null); base.WriteError(errorRecord2); } catch (UnauthorizedAccessException unauthorizedAccessException1) { UnauthorizedAccessException unauthorizedAccessException = unauthorizedAccessException1; ErrorRecord errorRecord3 = new ErrorRecord(unauthorizedAccessException, "UnauthorizedAccess", ErrorCategory.SecurityError, null); base.WriteError(errorRecord3); } }
private void DeleteDelegateSettings(string applicationname, RegistryKey rootKey, string Registry_Path, IGroupPolicyObject GPO) { WSManHelper wSManHelper = new WSManHelper(this); int num = 0; bool flag = false; try { string str = string.Concat(Registry_Path, "\\CredentialsDelegation"); RegistryKey registryKey = rootKey.OpenSubKey(string.Concat(str, "\\", wSManHelper.Key_Allow_Fresh_Credentials), true); if (registryKey != null) { string[] valueNames = registryKey.GetValueNames(); if ((int)valueNames.Length > 0) { Collection<string> strs = new Collection<string>(); string[] strArrays = valueNames; for (int i = 0; i < (int)strArrays.Length; i++) { string str1 = strArrays[i]; object value = registryKey.GetValue(str1); if (value != null && !value.ToString().StartsWith(applicationname, StringComparison.OrdinalIgnoreCase)) { strs.Add(value.ToString()); flag = true; } registryKey.DeleteValue(str1); } foreach (string str2 in strs) { registryKey.SetValue(Convert.ToString(num + 1, CultureInfo.InvariantCulture), str2, RegistryValueKind.String); num++; } } } if (!flag) { RegistryKey registryKey1 = rootKey.OpenSubKey(str, true); if (registryKey1 != null) { object obj = registryKey1.GetValue(wSManHelper.Key_Allow_Fresh_Credentials); if (obj != null) { registryKey1.DeleteValue(wSManHelper.Key_Allow_Fresh_Credentials, false); } object value1 = registryKey1.GetValue(wSManHelper.Key_Concatenate_Defaults_AllowFresh); if (value1 != null) { registryKey1.DeleteValue(wSManHelper.Key_Concatenate_Defaults_AllowFresh, false); } if (registryKey1.OpenSubKey(wSManHelper.Key_Allow_Fresh_Credentials) != null) { registryKey1.DeleteSubKeyTree(wSManHelper.Key_Allow_Fresh_Credentials); } } } GPO.Save(true, true, new Guid("35378EAC-683F-11D2-A89A-00C04FBBCFA2"), new Guid("6AD20875-336C-4e22-968F-C709ACB15814")); } catch (InvalidOperationException invalidOperationException1) { InvalidOperationException invalidOperationException = invalidOperationException1; ErrorRecord errorRecord = new ErrorRecord(invalidOperationException, "InvalidOperation", ErrorCategory.InvalidOperation, null); base.WriteError(errorRecord); } catch (ArgumentException argumentException1) { ArgumentException argumentException = argumentException1; ErrorRecord errorRecord1 = new ErrorRecord(argumentException, "InvalidArgument", ErrorCategory.InvalidArgument, null); base.WriteError(errorRecord1); } catch (SecurityException securityException1) { SecurityException securityException = securityException1; ErrorRecord errorRecord2 = new ErrorRecord(securityException, "SecurityException", ErrorCategory.SecurityError, null); base.WriteError(errorRecord2); } catch (UnauthorizedAccessException unauthorizedAccessException1) { UnauthorizedAccessException unauthorizedAccessException = unauthorizedAccessException1; ErrorRecord errorRecord3 = new ErrorRecord(unauthorizedAccessException, "UnauthorizedAccess", ErrorCategory.SecurityError, null); base.WriteError(errorRecord3); } }
internal GroupPolicyObject() { // System.Windows.Forms.MessageBox.Show("Instance" + Instance); Instance = GetInstance(); // System.Windows.Forms.MessageBox.Show("Instance1" + Instance); }