/// <summary>
 /// MailSlotMessageArrived method implmentation
 /// </summary>
 private static void MailSlotMessageArrived(MailSlotServer maislotserver, MailSlotMessage message)
 {
     if (message.Operation == (byte)NotificationsKind.ConfigurationReload)
     {
         ADFSManager.Config = null;
         ADFSManager.EnsureLocalConfiguration(null); // Force Reload Configuration
     }
     else if (message.Operation == (byte)NotificationsKind.ServiceStatusRunning)
     {
         ADFSManager.ServicesStatus = ServiceOperationStatus.OperationRunning;
         ADFSManager.OnServiceStatusChanged(ADFSManager.ServicesStatus, message.Text);
     }
     else if (message.Operation == (byte)NotificationsKind.ServiceStatusStopped)
     {
         ADFSManager.ServicesStatus = ServiceOperationStatus.OperationStopped;
         ADFSManager.OnServiceStatusChanged(ADFSManager.ServicesStatus, message.Text);
     }
     else if (message.Operation == (byte)NotificationsKind.ServiceStatusPending)
     {
         ADFSManager.ServicesStatus = ServiceOperationStatus.OperationPending;
         ADFSManager.OnServiceStatusChanged(ADFSManager.ServicesStatus, message.Text);
     }
     else if (message.Operation == (byte)NotificationsKind.ServiceStatusInError)
     {
         ADFSManager.ServicesStatus = ServiceOperationStatus.OperationInError;
         ADFSManager.OnServiceStatusChanged(ADFSManager.ServicesStatus, message.Text);
     }
 }
        /// <summary>
        /// Initialize method implementation
        /// </summary>
        internal static void Initialize(PSHost host = null, bool loadconfig = false)
        {
            if (ADFSManager == null)
            {
                ADFSManager = new ADFSServiceManager();
                ADFSManager.Initialize();
            }
            if (MailslotServer == null)
            {
                MailslotServer = new MailSlotServer("MGT");
                MailslotServer.MailSlotMessageArrived += MailSlotMessageArrived;
                MailslotServer.AllowToSelf             = true;
                MailslotServer.Start();
            }

            if (loadconfig)
            {
                try
                {
                    ADFSManager.EnsureLocalConfiguration(host);
                }
                catch (CmdletInvocationException cm)
                {
                    EventLog.WriteEntry(EventLogSource, SErrors.ErrorMFAUnAuthorized + "\r\r" + cm.Message, EventLogEntryType.Error, 30901);
                    throw cm;
                }
                catch (Exception ex)
                {
                    EventLog.WriteEntry(EventLogSource, string.Format(SErrors.ErrorLoadingMFAConfiguration, ex.Message), EventLogEntryType.Error, 30900);
                    throw ex;
                }
            }
        }
 /// <summary>
 /// RegisterADFSComputer method implementation
 /// </summary>
 public static bool RegisterADFSComputer(PSHost host, string servername)
 {
     try
     {
         string         fqdn    = Dns.GetHostEntry(servername).HostName.ToLower();
         ADFSServerHost srvhost = WebAdminManagerClient.GetCompterInformations(fqdn);
         if (srvhost != null)
         {
             int i = ADFSManager.ADFSFarm.Servers.FindIndex(c => c.FQDN.ToLower() == srvhost.FQDN.ToLower());
             if (i < 0)
             {
                 ADFSManager.ADFSFarm.Servers.Add(srvhost);
             }
             else
             {
                 ADFSManager.ADFSFarm.Servers[i] = srvhost;
             }
             ADFSManager.SetDirty(true);
             ADFSManager.WriteConfiguration(host);
         }
         else
         {
             return(false);
         }
     }
     catch (Exception ex)
     {
         throw ex;
     }
     return(true);
 }
 /// <summary>
 /// MailSlotMessageArrived method implmentation
 /// </summary>
 private static void MailSlotMessageArrived(MailSlotServer maislotserver, MailSlotMessage message)
 {
     if (message.Operation == 0xAA)
     {
         ADFSManager.Config = null;
         ADFSManager.EnsureLocalConfiguration(null); // Force Reload Configuration
         MailslotServer.AllowedMachines.Clear();
         foreach (ADFSServerHost svr in Config.Hosts.ADFSFarm.Servers)
         {
             MailslotServer.AllowedMachines.Add(svr.MachineName);
         }
     }
     else if (message.Operation == 0x10)
     {
         ADFSManager.ServicesStatus = ServiceOperationStatus.OperationRunning;
         ADFSManager.OnServiceStatusChanged(ADFSManager.ServicesStatus, message.Text);
     }
     else if (message.Operation == 0x11)
     {
         ADFSManager.ServicesStatus = ServiceOperationStatus.OperationStopped;
         ADFSManager.OnServiceStatusChanged(ADFSManager.ServicesStatus, message.Text);
     }
     else if (message.Operation == 0x12)
     {
         ADFSManager.ServicesStatus = ServiceOperationStatus.OperationPending;
         ADFSManager.OnServiceStatusChanged(ADFSManager.ServicesStatus, message.Text);
     }
     else if (message.Operation == 0x19)
     {
         ADFSManager.ServicesStatus = ServiceOperationStatus.OperationInError;
         ADFSManager.OnServiceStatusChanged(ADFSManager.ServicesStatus, message.Text);
     }
 }
        /// <summary>
        /// UnRegisterADFSComputer method implementation
        /// </summary>
        public static bool UnRegisterADFSComputer(PSHost Host, string servername, out List <ADFSServerHost> servers)
        {
            string fqdn = Dns.GetHostEntry(servername).HostName;

            ADFSManager.ADFSFarm.Servers.RemoveAll(c => c.FQDN.ToLower().Equals(fqdn.ToLower()));
            ADFSManager.SetDirty(true);
            ADFSManager.WriteConfiguration(Host);
            servers = ADFSManager.ADFSFarm.Servers;
            return(true);
        }
 /// <summary>
 /// UnRegisterADFSComputer method implementation
 /// </summary>
 public static bool UnRegisterADFSComputer(PSHost Host, string servername)
 {
     try
     {
         string fqdn = Dns.GetHostEntry(servername).HostName;
         ADFSManager.ADFSFarm.Servers.RemoveAll(c => c.FQDN.ToLower().Equals(fqdn.ToLower()));
         ADFSManager.SetDirty(true);
         ADFSManager.WriteConfiguration(Host);
     }
     catch (Exception ex)
     {
         throw ex;
     }
     return(true);
 }
 /// <summary>
 /// SetADDSAttributesTemplate method implementation
 /// </summary>
 internal static bool SetADDSAttributesTemplate(ADDSTemplateKind kind, bool updatecfg = false)
 {
     try
     {
         Initialize(null, true);
         ADFSManager.EnsureLocalService();
         ADFSManager.Config.Hosts.ActiveDirectoryHost.ApplyAttributesTemplate(kind);
         if (updatecfg)
         {
             ADFSManager.WriteConfiguration(null);
         }
         return(true);
     }
     catch (Exception)
     {
         return(false);
     }
 }
        /// <summary>
        /// RegisterADFSComputer method implementation
        /// </summary>
        public static bool RegisterADFSComputer(PSHost host, string servername, out List <ADFSServerHost> servers)
        {
            bool           bRet    = false;
            string         fqdn    = Dns.GetHostEntry(servername).HostName.ToLower();
            ADFSServerHost srvhost = WebAdminManagerClient.GetComputerInformations(fqdn);

            if (srvhost != null)
            {
                int i = ADFSManager.ADFSFarm.Servers.FindIndex(c => c.FQDN.ToLower().Equals(srvhost.FQDN.ToLower()));
                if (i < 0)
                {
                    ADFSManager.ADFSFarm.Servers.Add(srvhost);
                }
                else
                {
                    ADFSManager.ADFSFarm.Servers[i] = srvhost;
                }
                ADFSManager.SetDirty(true);
                ADFSManager.WriteConfiguration(host);
                bRet = true;
            }
            servers = ADFSManager.ADFSFarm.Servers;
            return(bRet);
        }
 /// <summary>
 /// RestartADFSService method implementation
 /// </summary>
 internal static bool RestartADFSService(PSHost hh, string servername)
 {
     return(ADFSManager.RestartServer(hh, servername));
 }