public void Save()
 {
     var result = Instance.Save(true, true, RegistryExtension, LocalGuid);
     if (result != 0)
     {
         throw new Exception("Error saving machine settings");
     }
     result = Instance.Save(false, true, RegistryExtension, LocalGuid);
     if (result != 0)
     {
         throw new Exception("Error saving user settings");
     }
 }
Exemplo n.º 2
0
            public void Save()
            {
                //System.Windows.Forms.MessageBox.Show("Instance" + Instance);
                //System.Windows.Forms.MessageBox.Show("Instance Local Guid" + LocalGuid);
                var result = Instance.Save(true, true, RegistryExtension, LocalGuid);

                if (result != 0)
                {
                    throw new Exception("Error saving machine settings");
                }

                result = Instance.Save(false, true, RegistryExtension, LocalGuid);
                if (result != 0)
                {
                    throw new Exception("Error saving user settings");
                }
            }
Exemplo n.º 3
0
 /// <summary>
 /// Saves the specified registry policy settings to disk and updates the revision number of the GPO.
 /// </summary>
 /// <param name="isMachine">Specifies the registry policy settings to be saved. If this parameter is TRUE, the computer policy settings are saved. Otherwise, the user policy settings are saved.</param>
 /// <param name="isAdd">Specifies whether this is an add or delete operation. If this parameter is FALSE, the last policy setting for the specified extension pGuidExtension is removed. In all other cases, this parameter is TRUE.</param>
 /// <exception cref="System.Runtime.InteropServices.COMException">Throw when com execution throws exceptions</exception>
 public void Save(bool isMachine, bool isAdd)
 {
     try
     {
         iGroupPolicyObject.Save(isMachine, isAdd, REGISTRY_EXTENSION_GUID, CLSID_GPESnapIn);
     }
     catch (COMException e)
     {
         throw e;
     }
 }
Exemplo n.º 4
0
        /// <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"));
        }
Exemplo n.º 6
0
        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);
            }
        }
Exemplo n.º 7
0
 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);
            }
        }
Exemplo n.º 9
0
 /// <summary>
 /// Saves the specified registry policy settings to disk and updates the revision number of the GPO.
 /// </summary>
 /// <param name="isMachine">Specifies the registry policy settings to be saved. If this parameter is TRUE, the computer policy settings are saved. Otherwise, the user policy settings are saved.</param>
 /// <param name="isAdd">Specifies whether this is an add or delete operation. If this parameter is FALSE, the last policy setting for the specified extension pGuidExtension is removed. In all other cases, this parameter is TRUE.</param>
 /// <exception cref="System.Runtime.InteropServices.COMException">Throw when com execution throws exceptions</exception>
 public void Save(bool isMachine, bool isAdd)
 {
     iGroupPolicyObject.Save(isMachine, isAdd, REGISTRY_EXTENSION_GUID, CLSID_GPESnapIn);
 }
Exemplo n.º 10
0
		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);
			}
		}