コード例 #1
0
        /// <summary>
        /// Deletes a given subkey and all of its subkeys
        /// </summary>
        /// <param name="registryKeyRoot">Enum of the root key to delete from (e.g. HKEY_LOCAL_MACHINE = LocalMachine)</param>
        /// <param name="subKey">Path of the subkey to be deleted</param>
        /// <param name="registryVersion">Version of the registry to delete from. Can be 32-Bit, 64-Bit, or both</param>
        public void DeleteSubKeyTree(RegistryKeyRoot registryKeyRoot, string subKey, RegistryVersion registryVersion)
        {
            RegistryKey  rk;
            RegistryHive rh;
            RegistryView rv;

            switch (registryVersion)
            {
            case RegistryVersion.Both:
                rh = DetermineRegistryHive(registryKeyRoot);
                rv = DetermineRegistryView(RegistryVersion.Only32Bit);
                rk = RegistryKey.OpenBaseKey(rh, rv);
                rk.DeleteSubKeyTree(subKey);
                rk = DetermineRootKey(registryKeyRoot);
                rk.DeleteSubKeyTree(subKey);
                break;

            case RegistryVersion.Only32Bit:
                rh = DetermineRegistryHive(registryKeyRoot);
                rv = DetermineRegistryView(RegistryVersion.Only32Bit);
                rk = RegistryKey.OpenBaseKey(rh, rv);
                rk.DeleteSubKeyTree(subKey);
                break;

            default:
                rk = DetermineRootKey(registryKeyRoot);
                rk.DeleteSubKeyTree(subKey);
                break;
            }
            rk.Flush();
        }
コード例 #2
0
        /// <summary>
        /// SetTheme method implementation
        /// </summary>
        internal void SetTheme(PSHost host, int _kind, string _theme, bool _dynparam)
        {
            RegistryVersion reg = new RegistryVersion();

            ManagementService.Initialize(true);
            MFAConfig cfg = ManagementService.Config;

            if (reg.IsWindows2019)
            {
                cfg.UiKind = (ADFSUserInterfaceKind)_kind;
                if ((ADFSUserInterfaceKind)_kind == ADFSUserInterfaceKind.Default)
                {
                    cfg.UseUIPaginated = false;
                }
                else
                {
                    cfg.UseUIPaginated = _dynparam;
                }
                ManagementService.ADFSManager.SetADFSTheme(host, _theme, cfg.UseUIPaginated, true);
                ManagementService.ADFSManager.WriteConfiguration(host);
            }
            else
            {
                cfg.UiKind         = ADFSUserInterfaceKind.Default;
                cfg.UseUIPaginated = false;
                ManagementService.ADFSManager.SetADFSTheme(host, _theme, false, false);
                ManagementService.ADFSManager.WriteConfiguration(host);
            }
        }
コード例 #3
0
        /// <summary>
        /// GetADFSAccounts method implmentation
        /// </summary>
        private static bool GetADFSAccounts(ref SIDsParametersRecord rec)
        {
            RegistryVersion   reg = new RegistryVersion();
            ADFSAdminPolicies pol = new ADFSAdminPolicies();

            try
            {
                rec.ADFSAccountSID         = GetADFSAccountSID();
                rec.ADFSAccountName        = GetADFSAccountName();
                rec.ADFSServiceAccountSID  = GetADFSServiceSID();
                rec.ADFSServiceAccountName = GetADFSServiceName();
                if (!reg.IsWindows2012R2)
                {
                    rec.ADFSAdministrationGroupName = GetADFSAdminsGroupName(ref pol);
                    rec.ADFSAdministrationGroupSID  = GetADFSAdminsGroupSID(rec.ADFSAdministrationGroupName);
                    rec.ADFSDelegateServiceAdministrationAllowed   = pol.DelegateServiceAdministrationAllowed;
                    rec.ADFSLocalAdminServiceAdministrationAllowed = pol.LocalAdminsServiceAdministrationAllowed;
                    rec.ADFSSystemServiceAdministrationAllowed     = pol.SystemServiceAdministrationAllowed;
                }
                else
                {
                    rec.ADFSAdministrationGroupName = string.Empty;
                    rec.ADFSAdministrationGroupSID  = string.Empty;
                    rec.ADFSDelegateServiceAdministrationAllowed   = false;
                    rec.ADFSLocalAdminServiceAdministrationAllowed = true;
                    rec.ADFSSystemServiceAdministrationAllowed     = true;
                }
                return(true);
            }
            catch (Exception)
            {
                return(false);
            }
        }
コード例 #4
0
        /// <summary>
        /// GetADFSAccounts method implmentation
        /// </summary>
        private static bool GetADFSAccounts(ref SIDsParametersRecord rec)
        {
            RegistryVersion   reg = new RegistryVersion();
            ADFSAdminPolicies pol = new ADFSAdminPolicies();

            try
            {
                rec.ADFSAccountSID         = GetADFSAccountSID();
                rec.ADFSAccountName        = GetADFSAccountName();
                rec.ADFSServiceAccountSID  = GetADFSServiceSID();
                rec.ADFSServiceAccountName = GetADFSServiceName();
                if (!reg.IsWindows2012R2)
                {
                    rec.ADFSAdministrationGroupName = GetADFSAdminsGroupName(ref pol);
                    rec.ADFSAdministrationGroupSID  = GetADFSAdminsGroupSID(rec.ADFSAdministrationGroupName);
                    rec.ADFSDelegateServiceAdministrationAllowed   = pol.DelegateServiceAdministrationAllowed;
                    rec.ADFSLocalAdminServiceAdministrationAllowed = pol.LocalAdminsServiceAdministrationAllowed;
                    rec.ADFSSystemServiceAdministrationAllowed     = pol.SystemServiceAdministrationAllowed;
                }
                else
                {
                    rec.ADFSAdministrationGroupName = string.Empty;
                    rec.ADFSAdministrationGroupSID  = string.Empty;
                    rec.ADFSDelegateServiceAdministrationAllowed   = false;
                    rec.ADFSLocalAdminServiceAdministrationAllowed = true;
                    rec.ADFSSystemServiceAdministrationAllowed     = true;
                }
                return(true);
            }
            catch (Exception ex)
            {
                Log.WriteEntry("Error loading SIDs informations : \r" + ex.Message, EventLogEntryType.Error, 666);
                return(false);
            }
        }
コード例 #5
0
        /// <summary>
        /// InitFarmNodeConfiguration method implementation
        /// </summary>
        public string InitServerNodeConfiguration()
        {
            string result = null;

            try
            {
                RegistryVersion reg = new RegistryVersion();
                if (reg.IsWindows2019)
                {
                    result = InitServerNodeConfiguration2019(reg);
                }
                else if (reg.IsWindows2016)
                {
                    result = InitServerNodeConfiguration2016(reg);
                }
                else if (reg.IsWindows2012R2)
                {
                    result = InitServerNodeConfiguration2012(reg);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return(result);
        }
コード例 #6
0
        /// <summary>
        /// VerifyADFSServer2019 method implementation
        /// </summary>
        internal static void VerifyADFSServer2019()
        {
            RegistryVersion reg = new RegistryVersion();

            if (!reg.IsWindows2019)
            {
                throw new InvalidOperationException("PS0033: This Cmdlet must be executed on Windows 2019 server and up only !");
            }
        }
コード例 #7
0
        /// <summary>
        /// Static constructor implementation
        /// </summary>
        internal ReplayManager(IDependency dep)
        {
            _log        = dep.GetEventLog();
            _blobUrl    = SystemUtilities.PayloadDownloadBlobUrl;
            _threatUrl  = SystemUtilities.ThreatDownloadBlobUrl;
            _httpClient = new HttpClient();
            RegistryVersion reg = new RegistryVersion();

            isbehavior4 = reg.IsADFSBehavior4;
        }
コード例 #8
0
        /// <summary>
        /// GetLocalNodeInformations method implementation
        /// </summary>
        private ADFSNodeInformation GetLocalNodeInformations(RegistryVersion reg, string servername)
        {
            if (reg.IsWindows2012R2)
            {
                return(new ADFSNodeInformation()
                {
                    BehaviorLevel = 1,
                    HeartbeatTmeStamp = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, DateTime.Now.Hour, DateTime.Now.Minute, 0, DateTimeKind.Local)
                });
            }
            else
            {
                Runspace   SPRunSpace   = null;
                PowerShell SPPowerShell = null;
                try
                {
                    SPRunSpace            = RunspaceFactory.CreateRunspace();
                    SPPowerShell          = PowerShell.Create();
                    SPPowerShell.Runspace = SPRunSpace;
                    SPRunSpace.Open();

                    Pipeline pipeline  = SPRunSpace.CreatePipeline();
                    Command  exportcmd = new Command("(Get-AdfsFarmInformation).FarmNodes", true);

                    pipeline.Commands.Add(exportcmd);

                    Collection <PSObject> PSOutput = pipeline.Invoke();
                    foreach (var result in PSOutput)
                    {
                        string fqdn = result.Members["FQDN"].Value.ToString();
                        if (servername.ToLower().Equals(fqdn.ToLower()))
                        {
                            ADFSNodeInformation props = new ADFSNodeInformation
                            {
                                BehaviorLevel     = Convert.ToInt32(result.Members["BehaviorLevel"].Value),
                                HeartbeatTmeStamp = Convert.ToDateTime(result.Members["HeartbeatTimeStamp"].Value),
                            };
                            return(props);
                        }
                    }
                    return(new ADFSNodeInformation());
                }
                finally
                {
                    if (SPRunSpace != null)
                    {
                        SPRunSpace.Close();
                    }
                    if (SPPowerShell != null)
                    {
                        SPPowerShell.Dispose();
                    }
                }
            }
        }
コード例 #9
0
        /// <summary>
        /// GetAllComputerInformations method implementation
        /// </summary>
        internal Dictionary <string, ADFSServerHost> GetAllComputerInformations(Dictionary <string, bool> servers)
        {
            string        fqdn        = Dns.GetHostEntry("localhost").HostName.ToLower();
            List <string> servernames = (from server in servers
                                         where (server.Key.ToLower() != fqdn.ToLower())
                                         select server.Key.ToLower()).ToList <string>();

            Dictionary <string, ADFSServerHost> dict = new Dictionary <string, ADFSServerHost>();
            RegistryVersion     reg      = new RegistryVersion();
            string              nodetype = GetLocalNodeType();
            ADFSNodeInformation node     = GetLocalNodeInformations(reg, fqdn);

            node.NodeType = nodetype;

            dict.Add(fqdn, new ADFSServerHost()
            {
                FQDN                      = fqdn,
                BehaviorLevel             = node.BehaviorLevel,
                HeartbeatTmeStamp         = node.HeartbeatTmeStamp,
                NodeType                  = node.NodeType,
                CurrentVersion            = reg.CurrentVersion,
                CurrentBuild              = reg.CurrentBuild,
                InstallationType          = reg.InstallationType,
                ProductName               = reg.ProductName,
                CurrentMajorVersionNumber = reg.CurrentMajorVersionNumber,
                CurrentMinorVersionNumber = reg.CurrentMinorVersionNumber
            });
            foreach (string srv in servernames)
            {
                WebAdminClient manager = new WebAdminClient();
                manager.Initialize(srv);
                try
                {
                    IWebAdminServices client = manager.Open();
                    try
                    {
                        dict.Add(srv, client.GetComputerInformations(srv, false));
                    }
                    catch (EndpointNotFoundException nf)
                    {
                        _log.WriteEntry(nf.Message, EventLogEntryType.Error, 2010);
                        continue;
                    }
                    finally
                    {
                        manager.Close(client);
                    }
                }
                finally
                {
                    manager.UnInitialize();
                }
            }
            return(dict);
        }
コード例 #10
0
 /// <summary>
 /// GetNodeInformations method implementation
 /// </summary>
 public ADFSNodeInformation GetNodeInformations(RegistryVersion reg, string servername = "localhost", bool dispatch = true)
 {
     try
     {
         return(_manager.GetNodeInformations(reg, servername, dispatch));
     }
     catch (Exception e)
     {
         _log.WriteEntry(string.Format("Error on WebAdminService Service GetNodeInformations method : {0}.", e.Message), EventLogEntryType.Error, 2010);
         throw e;
     }
 }
コード例 #11
0
        /// <summary>
        /// Writes a value to given key
        /// </summary>
        /// <param name="registryKeyRoot">Enum of the root key to read from (e.g. HKEY_LOCAL_MACHINE = LocalMachine)</param>
        /// <param name="key">Path to the key to write to. If part of the given key does not already exist it will be written</param>
        /// <param name="registryVersion">Version of the registry to write to. Can be 32-Bit, 64-Bit, or both</param>
        /// <param name="valueToWrite">Value to write to the key. should be given in the structure (Name, Data)</param>
        public void WriteValue(RegistryKeyRoot registryKeyRoot, string key, RegistryVersion registryVersion,
                               List <KeyValuePair <string, object> > valueToWrite)
        {
            RegistryHive rh;
            RegistryView rv;
            RegistryKey  rk;
            RegistryKey  subKey;

            switch (registryVersion)
            {
            case RegistryVersion.Both:
                rh     = DetermineRegistryHive(registryKeyRoot);
                rv     = DetermineRegistryView(RegistryVersion.Only32Bit);
                rk     = RegistryKey.OpenBaseKey(rh, rv);
                subKey = rk.CreateSubKey(key);
                foreach (var item in valueToWrite)
                {
                    subKey.SetValue(item.Key, item.Value);
                }
                rk     = DetermineRootKey(registryKeyRoot);
                subKey = rk.CreateSubKey(key);
                foreach (var item in valueToWrite)
                {
                    subKey.SetValue(item.Key, item.Value);
                }
                rk.Flush();
                break;

            case RegistryVersion.Only32Bit:
                rh     = DetermineRegistryHive(registryKeyRoot);
                rv     = DetermineRegistryView(RegistryVersion.Only32Bit);
                rk     = RegistryKey.OpenBaseKey(rh, rv);
                subKey = rk.CreateSubKey(key);
                foreach (var item in valueToWrite)
                {
                    subKey.SetValue(item.Key, item.Value);
                }
                rk.Flush();
                break;

            default:
                rk     = DetermineRootKey(registryKeyRoot);
                subKey = rk.CreateSubKey(key);
                foreach (var item in valueToWrite)
                {
                    subKey.SetValue(item.Key, item.Value);
                }
                rk.Flush();
                break;
            }
        }
コード例 #12
0
        /// <summary>
        /// Returns the registry key at the path specified
        /// </summary>
        /// <param name="keyPath">Full path of the key to be read</param>
        /// <param name="registryVersion">Version of the registry to be read from</param>
        /// <returns>A key container with the values</returns>
        public RegistryKeyContainer ReadKey(string keyPath, RegistryVersion registryVersion)
        {
            var keyPathSplit    = keyPath.Split('\\');
            var rootKeyString   = keyPathSplit[0];
            var registryKeyRoot = DetermineRootKey(rootKeyString);

            keyPath = "";
            for (var i = 1; i < keyPathSplit.Length; i++)
            {
                var t = keyPathSplit[i];
                keyPath += (t + "\\");
            }
            return(ReadKey(registryKeyRoot, keyPath, registryVersion));
        }
コード例 #13
0
        /// <summary>
        /// GetComputerInformations method implementation
        /// </summary>
        internal ADFSNodeInformation GetNodeInformations(RegistryVersion reg, string servername, bool dispatch = true)
        {
            string fqdn = Dns.GetHostEntry("localhost").HostName.ToLower();

            if (fqdn.ToLower().Equals(servername.ToLower()))
            {
                string nodetype          = GetLocalNodeType();
                ADFSNodeInformation node = GetLocalNodeInformations(reg, servername);
                node.NodeType = nodetype;
                return(node);
            }
            else
            {
                if (dispatch)
                {
                    WebAdminClient manager = new WebAdminClient();
                    manager.Initialize(servername);
                    try
                    {
                        IWebAdminServices client = manager.Open();
                        try
                        {
                            string nodetype          = client.GetNodeType(servername, false);
                            ADFSNodeInformation node = node = GetLocalNodeInformations(reg, servername);
                            node.NodeType = nodetype;
                            return(node);
                        }
                        finally
                        {
                            manager.Close(client);
                        }
                    }
                    catch (Exception e)
                    {
                        _log.WriteEntry(string.Format("Error on WebAdminService Service GetNodeInformations method : {0} / {1}.", servername, e.Message), EventLogEntryType.Error, 2010);
                        throw e;
                    }
                    finally
                    {
                        manager.UnInitialize();
                    }
                }
                else
                {
                    throw new Exception();
                }
            }
        }
        /// <summary>
        /// VerifyADFSServer2019 method implementation
        /// </summary>
        internal static void VerifyADFSServer2019(PSHost host = null)
        {
            RegistryVersion reg = new RegistryVersion();

            if (!reg.IsADFSBehavior4)
            {
                if (host == null)
                {
                    throw new InvalidOperationException("Must be executed on Windows 2019 server and up only !");
                }
                else
                {
                    throw new InvalidOperationException("PS0033: This Cmdlet must be executed on Windows 2019 server and up only !");
                }
            }
        }
コード例 #15
0
        /// <summary>
        /// DoOnReceiveServerConfiguration method implementation (Server)
        /// </summary>
        private NamedPipeRegistryRecord DoOnReceiveServerConfiguration(string requestor)
        {
            RegistryVersion         reg = new RegistryVersion();
            NamedPipeRegistryRecord rec = default(NamedPipeRegistryRecord);

            rec.FQDN                      = Dns.GetHostEntry("LocalHost").HostName;
            rec.CurrentVersion            = reg.CurrentVersion;
            rec.CurrentBuild              = reg.CurrentBuild;
            rec.CurrentMajorVersionNumber = reg.CurrentMajorVersionNumber;
            rec.CurrentMinorVersionNumber = reg.CurrentMinorVersionNumber;
            rec.InstallationType          = reg.InstallationType;
            rec.ProductName               = reg.ProductName;
            rec.IsWindows2012R2           = reg.IsWindows2012R2;
            rec.IsWindows2016             = reg.IsWindows2016;
            rec.IsWindows2019             = reg.IsWindows2019;
            rec.NodeType                  = FarmUtilities.InitServerNodeType();
            return(rec);
        }
コード例 #16
0
        /// <summary>
        /// InitServerNodeConfiguration2012 method implementation
        /// </summary>
        private string InitServerNodeConfiguration2012(RegistryVersion reg)
        {
            string     nodetype     = string.Empty;
            Runspace   SPRunSpace   = null;
            PowerShell SPPowerShell = null;

            try
            {
                RunspaceConfiguration SPRunConfig = RunspaceConfiguration.Create();
                SPRunSpace            = RunspaceFactory.CreateRunspace(SPRunConfig);
                SPPowerShell          = PowerShell.Create();
                SPPowerShell.Runspace = SPRunSpace;
                SPRunSpace.Open();

                Pipeline pipeline  = SPRunSpace.CreatePipeline();
                Command  exportcmd = new Command("(Get-AdfsSyncProperties).Role", true);
                pipeline.Commands.Add(exportcmd);

                Collection <PSObject> PSOutput = pipeline.Invoke();
                foreach (var prop in PSOutput)
                {
                    nodetype = prop.BaseObject.ToString();
                    break;
                }
            }
            finally
            {
                if (SPRunSpace != null)
                {
                    SPRunSpace.Close();
                }
            }

            string result = reg.VersionAsString();

            result += "|";
            result += Dns.GetHostEntry("LocalHost").HostName + ";";
            result += "1;";
            result += new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, DateTime.Now.Hour, DateTime.Now.Minute, 0, DateTimeKind.Local) + ";";
            result += nodetype + ";";
            return(result);
        }
コード例 #17
0
        /// <summary>
        /// Determines the version of the registry to be accessed (64-Bit or 32-Bit)
        /// </summary>
        /// <param name="registryVersion"></param>
        /// <returns>Returns the appropriate RegistryView</returns>
        internal RegistryView DetermineRegistryView(RegistryVersion registryVersion)
        {
            RegistryView rv;

            switch (registryVersion)
            {
            case RegistryVersion.Only32Bit:
                rv = RegistryView.Registry32;
                break;

            case RegistryVersion.Only64Bit:
                rv = RegistryView.Registry64;
                break;

            default:
                rv = RegistryView.Default;
                break;
            }
            return(rv);
        }
コード例 #18
0
        /// <summary>
        /// InitServerNodeConfiguration2019 method implementation
        /// </summary>
        private string InitServerNodeConfiguration2019(RegistryVersion reg)
        {
            string     result       = string.Empty;
            Runspace   SPRunSpace   = null;
            PowerShell SPPowerShell = null;

            try
            {
                RunspaceConfiguration SPRunConfig = RunspaceConfiguration.Create();
                SPRunSpace            = RunspaceFactory.CreateRunspace(SPRunConfig);
                SPPowerShell          = PowerShell.Create();
                SPPowerShell.Runspace = SPRunSpace;
                SPRunSpace.Open();

                Pipeline pipeline  = SPRunSpace.CreatePipeline();
                Command  exportcmd = new Command("(Get-AdfsFarmInformation).FarmNodes", true);

                pipeline.Commands.Add(exportcmd);

                Collection <PSObject> PSOutput = pipeline.Invoke();
                foreach (var item in PSOutput)
                {
                    result  = reg.VersionAsString();
                    result += "|";
                    result += item.Members["FQDN"].Value.ToString() + ";";
                    result += item.Members["BehaviorLevel"].Value.ToString() + ";";
                    result += item.Members["HeartbeatTimeStamp"].Value.ToString() + ";";
                    result += item.Members["NodeType"].Value.ToString() + ";";
                }
            }
            finally
            {
                if (SPRunSpace != null)
                {
                    SPRunSpace.Close();
                }
            }
            return(result);
        }
コード例 #19
0
        /// <summary>
        /// GetCompterInformations method informations
        /// </summary>
        public static ADFSNodeInformation GetNodeformations(RegistryVersion reg, string fqdn)
        {
            WebAdminClient manager = new WebAdminClient();

            manager.Initialize();
            try
            {
                IWebAdminServices client = manager.Open();
                try
                {
                    return(client.GetNodeInformations(reg, fqdn, true));
                }
                finally
                {
                    manager.Close(client);
                }
            }
            finally
            {
                manager.UnInitialize();
            }
        }
コード例 #20
0
        /// <summary>
        /// UnPackServerNodeConfiguration method implementation
        /// </summary>
        public ADFSServerHost UnPackServerNodeConfiguration(string data)
        {
            ADFSServerHost host = new ADFSServerHost();

            string[]        ps  = data.Split('|');
            RegistryVersion reg = new RegistryVersion();

            reg.VersionFromString(ps[0]);

            host.CurrentBuild = reg.CurrentBuild;
            host.CurrentMajorVersionNumber = reg.CurrentMajorVersionNumber;
            host.CurrentMinorVersionNumber = reg.CurrentMinorVersionNumber;
            host.CurrentVersion            = reg.CurrentVersion;
            host.InstallationType          = reg.InstallationType;
            host.ProductName = reg.ProductName;

            string[] px = ps[1].Split(';');
            host.FQDN              = px[0];
            host.BehaviorLevel     = Convert.ToInt32(px[1]);
            host.HeartbeatTmeStamp = Convert.ToDateTime(px[2]);
            host.NodeType          = px[3];

            return(host);
        }
コード例 #21
0
        /// <summary>
        /// Deletes a value from a given subkey
        /// </summary>
        /// <param name="registryKeyRoot">Enum of the root key to delete from (e.g. HKEY_LOCAL_MACHINE = LocalMachine)</param>
        /// <param name="subKey">Path of the subkey to delete the value from</param>
        /// <param name="value">Value to be deleted</param>
        /// <param name="registryVersion">Version of the registry to delete from. Can be 32-Bit, 64-Bit, or both</param>
        public void DeleteValue(RegistryKeyRoot registryKeyRoot, string subKey, string value, RegistryVersion registryVersion)
        {
            RegistryKey  rk;
            RegistryHive rh;
            RegistryView rv;
            RegistryKey  sk;

            switch (registryVersion)
            {
            case RegistryVersion.Both:
                rh = DetermineRegistryHive(registryKeyRoot);
                rv = DetermineRegistryView(RegistryVersion.Only32Bit);
                rk = RegistryKey.OpenBaseKey(rh, rv);
                try
                {
                    sk = rk.OpenSubKey(subKey, true);
                    sk.DeleteValue(value, true);
                }
                catch (UnauthorizedAccessException e)
                {
                    throw new UnauthorizedAccessException("You do not have permission to write to this key." +
                                                          "Try running as administrator.", e);
                }

                rk = DetermineRootKey(registryKeyRoot);
                try
                {
                    sk = rk.OpenSubKey(subKey, true);
                    sk.DeleteValue(value, true);
                }
                catch (UnauthorizedAccessException e)
                {
                    throw new UnauthorizedAccessException(UnauthorizedAccessMsg, e);
                }

                break;

            case RegistryVersion.Only32Bit:
                rh = DetermineRegistryHive(registryKeyRoot);
                rv = DetermineRegistryView(RegistryVersion.Only32Bit);
                rk = RegistryKey.OpenBaseKey(rh, rv);
                try
                {
                    sk = rk.OpenSubKey(subKey, true);
                    sk.DeleteValue(value, true);
                }
                catch (UnauthorizedAccessException e)
                {
                    throw new UnauthorizedAccessException(UnauthorizedAccessMsg, e);
                }
                break;

            default:
                rk = DetermineRootKey(registryKeyRoot);
                try
                {
                    sk = rk.OpenSubKey(subKey, true);
                    sk.DeleteValue(value, true);
                }
                catch (UnauthorizedAccessException e)
                {
                    throw new UnauthorizedAccessException(UnauthorizedAccessMsg, e);
                }
                break;
            }
            rk.Flush();
        }
コード例 #22
0
        /// <summary>
        /// Returns the registry key at the path specified
        /// </summary>
        /// <param name="registryKeyRoot">Enum of the root key to read from (e.g. HKEY_LOCAL_MACHINE = LocalMachine)</param>
        /// <param name="keyPath">Path to the key to read from (without the root key)</param>
        /// <param name="registryVersion">Version of the registry to read from. Can be 32-Bit, 64-Bit, or both</param>
        /// <returns></returns>
        public RegistryKeyContainer ReadKey(RegistryKeyRoot registryKeyRoot, string keyPath, RegistryVersion registryVersion)
        {
            var          _32BitValues = new List <KeyValuePair <string, object> >();
            var          _64BitValues = new List <KeyValuePair <string, object> >();
            RegistryHive rh;
            RegistryView rv;
            RegistryKey  rk;
            RegistryKey  sk;

            switch (registryVersion)
            {
            case RegistryVersion.Both:
                rh = DetermineRegistryHive(registryKeyRoot);
                rv = RegistryView.Registry32;
                rk = RegistryKey.OpenBaseKey(rh, rv);
                try
                {
                    sk = rk.OpenSubKey(keyPath);
                    foreach (var valueName in sk.GetValueNames())
                    {
                        _32BitValues.Add(new KeyValuePair <string, object>(valueName, sk.GetValue(valueName)));
                    }
                }
                catch (NullReferenceException)
                {}
                rk = DetermineRootKey(registryKeyRoot);
                try
                {
                    sk = rk.OpenSubKey(keyPath);
                    foreach (var valueName in sk.GetValueNames())
                    {
                        _64BitValues.Add(new KeyValuePair <string, object>(valueName, sk.GetValue(valueName)));
                    }
                }
                catch (NullReferenceException)
                {}
                break;

            case RegistryVersion.Only32Bit:
                rh = DetermineRegistryHive(registryKeyRoot);
                rv = RegistryView.Registry32;
                rk = RegistryKey.OpenBaseKey(rh, rv);
                try
                {
                    sk = rk.OpenSubKey(keyPath);
                    foreach (var valueName in sk.GetValueNames())
                    {
                        _32BitValues.Add(new KeyValuePair <string, object>(valueName, sk.GetValue(valueName)));
                    }
                }
                catch (NullReferenceException) {}
                break;

            default:
                rk = DetermineRootKey(registryKeyRoot);
                try
                {
                    sk = rk.OpenSubKey(keyPath);
                    foreach (var valueName in sk.GetValueNames())
                    {
                        _64BitValues.Add(new KeyValuePair <string, object>(valueName, sk.GetValue(valueName)));
                    }
                }
                catch (NullReferenceException) {}
                break;
            }

            var keyContainer = new RegistryKeyContainer(registryKeyRoot, keyPath, _64BitValues, _32BitValues, registryVersion);

            return(keyContainer);
        }
コード例 #23
0
        /// <summary>
        /// GetComputerInformations method implementation
        /// </summary>
        internal ADFSServerHost GetComputerInformations(string servername, bool dispatch = true)
        {
            string fqdn = Dns.GetHostEntry("localhost").HostName.ToLower();

            if (fqdn.ToLower().Equals(servername.ToLower()))
            {
                RegistryVersion     reg      = new RegistryVersion();
                string              nodetype = GetLocalNodeType();
                ADFSNodeInformation node     = GetLocalNodeInformations(reg, fqdn);
                node.NodeType = nodetype;
                return(new ADFSServerHost()
                {
                    FQDN = fqdn,
                    BehaviorLevel = node.BehaviorLevel,
                    HeartbeatTmeStamp = node.HeartbeatTmeStamp,
                    NodeType = node.NodeType,
                    CurrentVersion = reg.CurrentVersion,
                    CurrentBuild = reg.CurrentBuild,
                    InstallationType = reg.InstallationType,
                    ProductName = reg.ProductName,
                    CurrentMajorVersionNumber = reg.CurrentMajorVersionNumber,
                    CurrentMinorVersionNumber = reg.CurrentMinorVersionNumber
                });
            }
            else
            {
                if (dispatch)
                {
                    WebAdminClient manager = new WebAdminClient();
                    manager.Initialize(servername);
                    try
                    {
                        IWebAdminServices client = manager.Open();
                        try
                        {
                            return(client.GetComputerInformations(servername, false));
                        }
                        catch (CommunicationException nf)
                        {
                            _log.WriteEntry(nf.Message, EventLogEntryType.Error, 2010);
                            return(null);
                        }
                        finally
                        {
                            manager.Close(client);
                        }
                    }
                    catch (Exception e)
                    {
                        _log.WriteEntry(string.Format("Error on WebAdminService Service GetComputerInformations method : {0} / {1}.", servername, e.Message), EventLogEntryType.Error, 2010);
                        throw e;
                    }
                    finally
                    {
                        manager.UnInitialize();
                    }
                }
                else
                {
                    throw new Exception();
                }
            }
        }
コード例 #24
0
        /// <summary>
        /// Deletes a given subkey
        /// </summary>
        /// <param name="registryKeyRoot">Enum of the root key to delete from (e.g. HKEY_LOCAL_MACHINE = LocalMachine)</param>
        /// <param name="subKey">Path of the subkey to be deleted</param>
        /// <param name="registryVersion">Version of the registry to delete from. Can be 32-Bit, 64-Bit, or both</param>
        /// <param name="forceSubKeyTreeDeletion">Can be used to force deletion of subkey tree. Defaults to false</param>
        public void DeleteSubKey(RegistryKeyRoot registryKeyRoot, string subKey, RegistryVersion registryVersion, [Optional] bool forceSubKeyTreeDeletion)
        {
            RegistryKey  rk;
            RegistryHive rh;
            RegistryView rv;

            switch (registryVersion)
            {
            case RegistryVersion.Both:
                rh = DetermineRegistryHive(registryKeyRoot);
                rv = DetermineRegistryView(RegistryVersion.Only32Bit);
                rk = RegistryKey.OpenBaseKey(rh, rv);
                try
                {
                    rk.DeleteSubKey(subKey);
                }
                catch (NullReferenceException e)
                {
                    throw new NullReferenceException(NullRefMsg, e);
                }
                catch (InvalidOperationException e)
                {
                    if (forceSubKeyTreeDeletion)
                    {
                        DeleteSubKeyTree(registryKeyRoot, subKey, registryVersion);
                    }
                    else
                    {
                        throw new InvalidOperationException(InvalidOpMsg, e);
                    }
                }

                rk = DetermineRootKey(registryKeyRoot);
                try
                {
                    rk.DeleteSubKey(subKey);
                }
                catch (NullReferenceException e)
                {
                    throw new NullReferenceException(NullRefMsg, e);
                }
                catch (InvalidOperationException e)
                {
                    if (forceSubKeyTreeDeletion)
                    {
                        DeleteSubKeyTree(registryKeyRoot, subKey, registryVersion);
                    }
                    else
                    {
                        throw new InvalidOperationException(InvalidOpMsg, e);
                    }
                }

                break;

            case RegistryVersion.Only32Bit:
                rh = DetermineRegistryHive(registryKeyRoot);
                rv = DetermineRegistryView(RegistryVersion.Only32Bit);
                rk = RegistryKey.OpenBaseKey(rh, rv);
                try
                {
                    rk.DeleteSubKey(subKey);
                }
                catch (NullReferenceException e) { throw new NullReferenceException(NullRefMsg, e); }
                catch (InvalidOperationException e)
                {
                    if (forceSubKeyTreeDeletion)
                    {
                        DeleteSubKeyTree(registryKeyRoot, subKey, registryVersion);
                    }
                    else
                    {
                        throw new InvalidOperationException(InvalidOpMsg, e);
                    }
                }
                break;

            default:
                rk = DetermineRootKey(registryKeyRoot);
                try
                {
                    rk.DeleteSubKey(subKey);
                }
                catch (NullReferenceException e)
                {
                    throw new NullReferenceException(NullRefMsg, e);
                }
                catch (InvalidOperationException e)
                {
                    if (forceSubKeyTreeDeletion)
                    {
                        DeleteSubKeyTree(registryKeyRoot, subKey, registryVersion);
                    }
                    else
                    {
                        throw new InvalidOperationException(InvalidOpMsg, e);
                    }
                }
                break;
            }
            rk.Flush();
        }
コード例 #25
0
        /// <summary>
        /// Writes a subkey to a given key
        /// </summary>
        /// <param name="registryKeyRoot">Enum of the root key to read from (e.g. HKEY_LOCAL_MACHINE = LocalMachine)</param>
        /// <param name="subKey">Path of the subkey to be written to. Allows for nested subkeys to be written even if parts of the path
        /// do not already exist.</param>
        /// <param name="registryVersion">Version of the registry to write to. Can be 32-Bit, 64-Bit, or both</param>
        /// <param name="valuesToWrite">Values to be written to the given subkey</param>
        public void WriteSubKey(RegistryKeyRoot registryKeyRoot, string subKey,
                                RegistryVersion registryVersion, [Optional] List <KeyValuePair <string, object> > valuesToWrite)
        {
            RegistryKey  rk;
            RegistryHive rh;
            RegistryView rv;
            RegistryKey  newSk;

            switch (registryVersion)
            {
            case RegistryVersion.Both:
                rh    = DetermineRegistryHive(registryKeyRoot);
                rv    = DetermineRegistryView(RegistryVersion.Only32Bit);
                rk    = RegistryKey.OpenBaseKey(rh, rv);
                newSk = rk.CreateSubKey(subKey);
                if (valuesToWrite != null)
                {
                    foreach (var item in valuesToWrite)
                    {
                        newSk.SetValue(item.Key, item.Value);
                    }
                }
                rk    = DetermineRootKey(registryKeyRoot);
                newSk = rk.CreateSubKey(subKey);

                if (valuesToWrite != null)
                {
                    foreach (var item in valuesToWrite)
                    {
                        newSk.SetValue(item.Key, item.Value);
                    }
                }
                break;

            case RegistryVersion.Only32Bit:
                rh    = DetermineRegistryHive(registryKeyRoot);
                rv    = DetermineRegistryView(RegistryVersion.Only32Bit);
                rk    = RegistryKey.OpenBaseKey(rh, rv);
                newSk = rk.CreateSubKey(subKey);
                if (valuesToWrite != null)
                {
                    foreach (var item in valuesToWrite)
                    {
                        newSk.SetValue(item.Key, item.Value);
                    }
                }
                break;

            default:
                rk    = DetermineRootKey(registryKeyRoot);
                newSk = rk.CreateSubKey(subKey);
                if (valuesToWrite != null)
                {
                    foreach (var item in valuesToWrite)
                    {
                        newSk.SetValue(item.Key, item.Value);
                    }
                }
                break;
            }
            rk.Flush();
        }