Ejemplo n.º 1
0
        public static void Main(string[] args)
        {
            Boolean syncParam   = false;
            Boolean deleteParam = false;

            for (int i = 0; i < args.Length; i++)
            {
                if (args[i].ToLower().Equals("-sync"))
                {
                    syncParam = true;
                }
                else if (args[i].ToLower().Equals("-delete"))
                {
                    deleteParam = true;
                }
            }

            Configuration         config    = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            AppSettingsSection    amSection = (AppSettingsSection)config.Sections["AMSettings"];
            ActiveDirectoryClient client    = new ActiveDirectoryClient(config);

            client.getUserDetailsFromActiveDirectory(
                amSection.Settings["UserExcludeFilter"].Value,
                amSection.Settings["ExcludeDisableAccount"].Value
                );
            string[] csvStr = client.createImportFile();
            Console.WriteLine("################### Sync Users - Start ###################");
            Console.WriteLine("");
            StringBuilder importDataStr = new StringBuilder();

            for (int i = 0; i < csvStr.Length; i++)
            {
                importDataStr.AppendLine(csvStr[i]);
                Console.WriteLine(csvStr[i]);
            }
            string response = "";

            if (syncParam)
            {
                response = client.uploadToAlertMedia(importDataStr.ToString());
                Console.WriteLine("*********************************************************");
                Console.WriteLine("");
                Console.WriteLine(response);
                Console.WriteLine("");
                Console.WriteLine("*********************************************************");
            }
            else
            {
                Console.WriteLine("*********************************************************");
                Console.WriteLine("");
                Console.WriteLine(" Above listed users have not been synced to AlertMedia. This was a dry run. To actually sync them, you need to send an args parameter (-sync)");
                Console.WriteLine("");
                Console.WriteLine("*********************************************************");
            }
            Console.WriteLine("");
            Console.WriteLine("################### Sync Users - End ###################");
            if (syncParam)
            {
                ArrayList                   importedUserList = new ArrayList();
                JavaScriptSerializer        j = new JavaScriptSerializer();
                Dictionary <string, object> a = (Dictionary <string, object>)j.Deserialize(response, typeof(Dictionary <string, object>));
                StringBuilder               textBoxResponse = new StringBuilder();
                if (a.ContainsKey("stats"))
                {
                    importedUserList = (ArrayList)a["successes"];
                    ArrayList failuresList = (ArrayList)a["failures"];
                    for (int i = 0; i < failuresList.Count; i++)
                    {
                        importedUserList.Add(failuresList[i]);
                    }
                }
                Console.WriteLine("################### Delete Users - Start ###################");
                Console.WriteLine("");
                string[]  delCsvStr         = client.previewDeleteUserList(importedUserList);
                ArrayList deletedUserIdList = new ArrayList();
                for (int i = 1; i < delCsvStr.Length; i++)
                {
                    string strLine = delCsvStr[i];
                    Console.WriteLine(strLine);
                    List <string> fields = SplitCSV(strLine);
                    deletedUserIdList.Add(fields[0].Replace("\"", ""));
                }
                if (deleteParam)
                {
                    client.deleteUsersFromAlertMedia(deletedUserIdList);
                    Console.WriteLine("*********************************************************");
                    Console.WriteLine("");
                    Console.WriteLine(" All the " + deletedUserIdList.Count + " users have been deleted from AlertMedia ");
                    Console.WriteLine("");
                    Console.WriteLine("*********************************************************");
                }
                else
                {
                    Console.WriteLine("*********************************************************");
                    Console.WriteLine("");
                    Console.WriteLine(deletedUserIdList.Count + " of the above listed users have not been deleted from AlertMedia. This was a dry run. To actually delete them, you need to send two args parameter (-sync -delete)");
                    Console.WriteLine("");
                    Console.WriteLine("*********************************************************");
                }
                Console.WriteLine("");
                Console.WriteLine("################### Sync Users - End ###################");
                Console.ReadLine();
            }
        }
 private void syncUsersButton_Click(object sender, EventArgs e)
 {
     try
     {
         Cursor.Current = Cursors.WaitCursor;
         string response = client.uploadToAlertMedia(importCSVData);
         JavaScriptSerializer        j = new JavaScriptSerializer();
         Dictionary <string, object> a = (Dictionary <string, object>)j.Deserialize(response, typeof(Dictionary <string, object>));
         StringBuilder textBoxResponse = new StringBuilder();
         if (a.ContainsKey("stats"))
         {
             this.importedUserList = (ArrayList)a["successes"];
             ArrayList failuresList = (ArrayList)a["failures"];
             for (int i = 0; i < failuresList.Count; i++)
             {
                 this.importedUserList.Add(failuresList[i]);
             }
             Dictionary <string, object> a2 = (Dictionary <string, object>)a["stats"];
             string total       = a2["total"].ToString();
             string created     = a2["created"].ToString();
             string updated     = a2["updated"].ToString();
             string failure     = a2["failed"].ToString();
             var    responseStr = string.Format("Total number of records {0}. Number of records created {1}. Number of records updated {2}. Number of failures {3}", total, created, updated, failure);
             textBoxResponse.AppendLine(" Total number of users - " + total);
             textBoxResponse.AppendLine(" Number of users created - " + created);
             textBoxResponse.AppendLine(" Number of users updated - " + updated);
             textBoxResponse.AppendLine(" Number of failures - " + failure);
             textBoxResponse.AppendLine("");
             if (a.ContainsKey("failureMessages"))
             {
                 textBoxResponse.AppendLine("Reasons for failure:");
                 textBoxResponse.AppendLine("----------------------------");
                 ArrayList failureList = (ArrayList)a["failureMessages"];
                 for (int count = 0; count < failureList.Count; count++)
                 {
                     textBoxResponse.AppendLine(failureList[count].ToString());
                 }
             }
         }
         else
         {
             if (a["status"].ToString().Equals("error"))
             {
                 textBoxResponse.AppendLine("Error - " + a["message"].ToString());
             }
             else
             {
                 textBoxResponse.AppendLine(response);
             }
         }
         resultsPanel.Text = textBoxResponse.ToString();
         MessageBox.Show("Syncing of users from your ActiveDirectory instance to AlertMedia server has been completed. Please look at the response box for more details. Click on Next button to delete users from AlertMedia server");
         Cursor.Current     = Cursors.Default;
         nextButton.Enabled = true;
     }
     catch (Exception ex)
     {
         using (StreamWriter w = File.AppendText("adsynclog.txt"))
         {
             Utils.Log(ex.ToString(), w);
         }
         MessageBox.Show("Not able to connect to AlertMedia servers. Please check you internet connection and try again. If problem persists, please get in touch with support.");
         Cursor.Current = Cursors.Default;
         return;
     }
 }