Advertisement
parabola949

SCCM SQL and AD comparison

Oct 14th, 2011
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 31.02 KB | None | 0 0
  1. /* Clinical ID Switcher
  2.  * Justin Mangum
  3.  * 05/19/2011
  4.  *
  5.  * Tasks to be done:
  6.  * Get Data from SCCM, load into array
  7.  *
  8.  *
  9.  * Functions to be written:
  10.  *
  11.  *
  12.  *
  13.  * Connection Strings:
  14.  * Data Source=FTWSCCMSQL01\INSTANCE1;Initial Catalog=SMS_TH1;Integrated Security=True
  15.  *
  16.  *
  17.  * Test Accounts Info:
  18.  * User accounts:
  19.  * JCPSite01 - 04 wired 01 - 03
  20.  * JCPSite01 - 04 wireless 01 - 03
  21.  *
  22.  * Groups:
  23.  * Wired Test Site 1-4
  24.  * Wireless Test Site 1-4
  25.  *
  26.  */
  27. using System;
  28. using System.Collections;
  29. using System.ComponentModel;
  30. using System.Data;
  31. using System.Drawing;
  32. using System.Text;
  33. using System.Windows.Forms;
  34. using System.DirectoryServices;
  35. using System.DirectoryServices.AccountManagement;
  36. using System.Data.SqlClient;
  37. using System.Text.RegularExpressions;
  38. using System.IO;
  39.  
  40.  
  41. namespace Clinical_ID_Switcher
  42. {
  43.     public partial class frmMain : Form
  44.     {
  45.         String[, ,] _SCCMInfo;
  46.         String[, ,] _ADInfo;
  47.         String[, ,] _Final;
  48.         String[, ,] _Final2;
  49.         String[] _sites;
  50.         String[] _groups;
  51.  
  52.         public enum objectClass
  53.         {
  54.             user, group, computer
  55.         }
  56.         public enum returnType
  57.         {
  58.             distinguishedName, ObjectGUID
  59.         }
  60.         public enum logType
  61.         {
  62.             notification, warning, error
  63.         }
  64.  
  65.         public frmMain()
  66.         {
  67.  
  68.             Log("Initializing",logType.notification);
  69.             InitializeComponent();
  70.            
  71.             _SCCMInfo = new String[15, 2, 2000];
  72.             _ADInfo = new String[15, 2, 2000];
  73.             _Final = new String[15, 2, 2000];
  74.             _Final2 = new String[15, 2, 2000];
  75.             _sites = new String[15];
  76.             _sites[0] = "THA";
  77.             _sites[1] = "THAM";
  78.             _sites[2] = "THAZ";
  79.             _sites[3] = "THC";
  80.             _sites[4] = "THD";
  81.             _sites[5] = "THDN";
  82.             _sites[6] = "THFW";
  83.             _sites[7] = "THHEB";
  84.             _sites[8] = "THHV";
  85.             _sites[9] = "THK";
  86.             _sites[10] = "THP";
  87.             _sites[11] = "THPG";
  88.             _sites[12] = "THS";
  89.             _sites[13] = "THSW";
  90.             _sites[14] = "THW";
  91.  
  92.             _groups = new String[2];
  93.             _groups[0] = "Wired";
  94.             _groups[1] = "Wireless";
  95.             lblStatus.Text = "Getting Data from SCCM";
  96.             progressBar1.Value = 33;
  97.             backgroundGetSCCM.RunWorkerAsync();
  98.            
  99.             txtUser.Text = "texas\\e~mangumj";
  100.             foreach (string site in _sites)
  101.             {
  102.                 lstSites.Items.Add(site.ToString());
  103.             }
  104.            
  105.            
  106.         }
  107.  
  108.  
  109.  
  110.         public void AddToGroup(string userDn, string groupDn)
  111.         {
  112.             try
  113.             {
  114.                 //"LDAP://CN"
  115.                
  116.                 userDn = userDn.Substring(7);
  117.                 DirectoryEntry dirEntry = new DirectoryEntry(groupDn, txtUser.Text, txtPass.Text);
  118.                 dirEntry.Properties["member"].Add(userDn);
  119.                 dirEntry.CommitChanges();
  120.                 dirEntry.Close();
  121.                 Log("Add successful", logType.notification);
  122.             }
  123.             catch (System.DirectoryServices.DirectoryServicesCOMException e)
  124.             {
  125.                 MessageBox.Show(e.Message);
  126.                 Log("Adding " + userDn + " failed - " + e.Message, logType.error);
  127.             }
  128.             catch (Exception ex)
  129.             {
  130.                 Log("Adding " + userDn + " failed - " + ex.Message,logType.error);
  131.                 MessageBox.Show(ex.Message);
  132.             }
  133.         }
  134.  
  135.         public void RemoveFromGroup(string userDn, string groupDn)
  136.         {
  137.             try
  138.             {
  139.                 userDn = userDn.Substring(7);
  140.                 DirectoryEntry dirEntry = new DirectoryEntry(groupDn, txtUser.Text, txtPass.Text);
  141.                 dirEntry.Properties["member"].Remove(userDn);
  142.                 dirEntry.CommitChanges();
  143.                 dirEntry.Close();
  144.                 Log("Removal successful", logType.notification);
  145.             }
  146.             catch (System.DirectoryServices.DirectoryServicesCOMException e)
  147.             {
  148.                 Log("Removing " + userDn + " failed - " + e.Message, logType.error);
  149.                 MessageBox.Show(e.Message);
  150.             }
  151.             catch (Exception ex)
  152.             {
  153.                 Log("Removing " + userDn + " failed - " + ex.Message, logType.error);
  154.                 MessageBox.Show(ex.Message);
  155.             }
  156.         }
  157.  
  158.         public ArrayList GetUsersInGroup(string myGroup)
  159.         {
  160.             ArrayList memberList = new ArrayList();
  161.             var myDomain = new PrincipalContext(ContextType.Domain, "txhealth.org", "OU=THR Users,DC=txhealth,DC=org");
  162.             var group = GroupPrincipal.FindByIdentity(myDomain, myGroup);
  163.             if (group != null)
  164.             {
  165.                 Log("Getting users in group: " + group, logType.notification);
  166.                 var members = group.GetMembers();
  167.                 foreach (var member in members)
  168.                 {
  169.                     string tempUser = member.SamAccountName;
  170.                     if (!Regex.IsMatch(tempUser, @"^[\p{L}]+$"))
  171.                         memberList.Add(tempUser);
  172.                     else
  173.                         Log(tempUser + " ignored.", logType.warning);
  174.                    
  175.                 }
  176.             }
  177.             else
  178.             {
  179.                 Log(@"Could not find group " + myGroup + ". Try again.", logType.error);
  180.                 return null;
  181.             }
  182.             return memberList;
  183.         }
  184.  
  185.         private void lstSites_SelectedIndexChanged(object sender, EventArgs e)
  186.         {
  187.             lstGroups.Items.Clear();
  188.             lstUsers.Items.Clear();
  189.             foreach (string group in _groups)
  190.             {
  191.                 lstGroups.Items.Add(group);
  192.             }
  193.         }
  194.  
  195.         private void lstGroups_SelectedIndexChanged(object sender, EventArgs e)
  196.         {
  197.             if (lstSites.FocusedItem == null)
  198.                 return;
  199.             if (lstGroups.FocusedItem == null)
  200.                 return;
  201.             lstUsers.Items.Clear();
  202.  
  203.             int x =0;
  204.  
  205.             while (_Final2[lstSites.FocusedItem.Index, lstGroups.FocusedItem.Index, x] != null)
  206.             {
  207.                 x++;
  208.             }
  209.             for (int i = 0; i < x; i++)
  210.             {
  211.                 string temp = _Final2[lstSites.FocusedItem.Index, lstGroups.FocusedItem.Index, i];
  212.                 if (temp.Contains("+"))
  213.                 {
  214.                     lstUsers.Items.Add(temp.Substring(1));
  215.                     lstUsers.Items[i].BackColor = Color.Green;
  216.                 }
  217.                 else if (temp.Contains("-"))
  218.                 {
  219.                     lstUsers.Items.Add(temp.Substring(1));
  220.                     lstUsers.Items[i].BackColor = Color.Red;
  221.                 }
  222.                 else
  223.                     lstUsers.Items.Add(temp);
  224.             }
  225.         }
  226.  
  227.         private void btnCancel_Click(object sender, EventArgs e)
  228.         {
  229.             Log("Closing", logType.notification);
  230.             this.Close();
  231.         }
  232.  
  233.         private void btnCleanup_Click(object sender, EventArgs e)
  234.         {
  235.             //HERE WE GO!
  236.             int x = 0, y = 0, z = 0;
  237.             string tempUser = "", tempGroup = "";
  238.             foreach (string site in _sites)
  239.             {
  240.                 y = 0;
  241.                 foreach (string group in _groups)
  242.                 {
  243.                     z = 0;
  244.                     tempGroup = GetObjectDistinguishedName("Clinical IDs " + site + " " + group, objectClass.group);
  245.                     bool end = false;
  246.                     do
  247.                     {
  248.                         if (_Final2[x, y, z] != null)
  249.                         {
  250.                             tempUser = _Final2[x, y, z];
  251.                             if (tempUser.Contains("+"))
  252.                             {
  253.                                 //add user
  254.                                 tempUser = tempUser.Substring(1);
  255.                                 if (tempUser.Contains("texas"))
  256.                                     tempUser = tempUser.Substring(6);
  257.                                 Log("Adding " + tempUser + " to " + tempGroup, logType.notification);
  258.                                 try
  259.                                 {
  260.                                     tempUser = GetObjectDistinguishedName(tempUser, objectClass.user);
  261.                                     AddToGroup(tempUser, tempGroup);
  262.                                 }
  263.                                 catch (Exception ex)
  264.                                 {
  265.                                     Log(ex.Message, logType.error);
  266.                                 }
  267.                                
  268.                                
  269.                             }
  270.                             else if (tempUser.Contains("-"))
  271.                             {
  272.                                 //remove user
  273.                                 tempUser = tempUser.Substring(1);
  274.                                 if (tempUser.Contains("texas"))
  275.                                     tempUser = tempUser.Substring(6);
  276.                                 Log("Removing " + tempUser + " from " + tempGroup, logType.notification);
  277.                                 try
  278.                                 {
  279.                                     tempUser = GetObjectDistinguishedName(tempUser, objectClass.user);
  280.                                     RemoveFromGroup(tempUser, tempGroup);
  281.                                 }
  282.                                 catch (Exception ex)
  283.                                 {
  284.                                     Log(ex.Message, logType.error);
  285.                                 }
  286.                             }
  287.                             else
  288.                             {
  289.                                 //do nothing
  290.                             }
  291.                         }
  292.                         else
  293.                             end = true;
  294.  
  295.                         z++;
  296.                     } while (!end);
  297.                     y++;
  298.                 }
  299.                 x++;
  300.             }
  301.             Log("Completed Cleanup.  Closing", logType.notification);
  302.             this.Close();
  303.         }
  304.  
  305.         public string GetObjectDistinguishedName(string objectName, objectClass objectCls)
  306.         {
  307.            
  308.             returnType returnValue = returnType.distinguishedName;
  309.             string LdapDomain = "txhealth.org";
  310.             string distinguishedName = string.Empty;
  311.             string connectionPrefix = "LDAP://" + LdapDomain;
  312.             DirectoryEntry entry = new DirectoryEntry(connectionPrefix);
  313.             DirectorySearcher mySearcher = new DirectorySearcher(entry);
  314.  
  315.             switch (objectCls)
  316.             {
  317.                 case objectClass.user:
  318.                     mySearcher.Filter = "(&(objectClass=user)(|(cn=" + objectName + ")(sAMAccountName=" + objectName + ")))";
  319.                     break;
  320.                 case objectClass.group:
  321.                     mySearcher.Filter = "(&(objectClass=group)(|(cn=" + objectName + ")(dn=" + objectName + ")))";
  322.                     break;
  323.                 case objectClass.computer:
  324.                     mySearcher.Filter = "(&(objectClass=computer)(|(cn=" + objectName + ")(dn=" + objectName + ")))";
  325.                     break;
  326.             }
  327.             SearchResult result = mySearcher.FindOne();
  328.  
  329.             if (result == null)
  330.             {
  331.                 throw new NullReferenceException
  332.                 ("unable to locate the distinguishedName for the object " +
  333.                 objectName + " in the " + LdapDomain + " domain");
  334.             }
  335.             DirectoryEntry directoryObject = result.GetDirectoryEntry();
  336.             if (returnValue.Equals(returnType.distinguishedName))
  337.             {
  338.                 distinguishedName = "LDAP://" + directoryObject.Properties
  339.                     ["distinguishedName"].Value;
  340.             }
  341.             if (returnValue.Equals(returnType.ObjectGUID))
  342.             {
  343.                 distinguishedName = directoryObject.Guid.ToString();
  344.             }
  345.             entry.Close();
  346.             entry.Dispose();
  347.             mySearcher.Dispose();
  348.             return distinguishedName;
  349.         }
  350.  
  351.         static void Log(string note, logType lType)
  352.         {
  353.             using (var logFile = new StreamWriter("c:\\logs\\IDClean.log", true))
  354.             {
  355.                 string type = "";
  356.                 switch (lType)
  357.                 {
  358.                     case logType.notification:
  359.                         type = "1";
  360.                         break;
  361.                     case logType.warning:
  362.                         type = "2";
  363.                         break;
  364.                     case logType.error:
  365.                         type = "3";
  366.                         break;
  367.                 }
  368.                
  369.                 //logFile.WriteLine("[" + DateTime.Now.ToString() + "] " + note);
  370.                 string Time = DateTime.Now.ToShortTimeString().Contains("PM") ? DateTime.Now.ToLongTimeString().Replace(" PM", ".679+300") : DateTime.Now.ToLongTimeString().Replace(" AM", ".679+300");
  371.                 logFile.WriteLine("<![LOG[" + note + "]LOG]!><time=\"" + Time + "\" date=\"" + DateTime.Now.ToShortDateString().Replace("/", "-") + "\" component=\"IDSwitcher\" context=\"\" type=\""+type+"\" file=\"execreqmgr.cpp:6524\">");
  372.                 logFile.Close();
  373.             }
  374.  
  375.         }
  376.  
  377.         private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
  378.         {
  379.             //bool end = false;
  380.             //Compare each list in _Final to all other lists, find common users, load to _Final2
  381.             for (int x = 0; x < 15; x++)
  382.             {
  383.                 Log("Site " + x, logType.notification);
  384.                 for (int y = 0; y < 2; y++)
  385.                 {
  386.                     int o = 0;
  387.                     int i = 0;
  388.                     while (_Final[x, y, i] != null)
  389.                     {
  390.                         i++;
  391.                     }
  392.  
  393.                     for (int z = 0; z < i; z++)
  394.                     {
  395.                         string temp = _Final[x, y, z];
  396.                         string temp2 = "";
  397.                        
  398.  
  399.                         if (!temp.Contains("-"))
  400.                         {
  401.                            
  402.                             _Final2[x, y, o] = temp;
  403.                             o++;
  404.                         }
  405.                        
  406.                         if (temp.Contains("-"))
  407.                         {
  408.                             temp2 = "+" + temp.Substring(1);
  409.                             for (int x2 = 0; x2 < 15; x2++)
  410.                             {
  411.                                 for (int y2 = 0; y2 < 2; y2++)
  412.                                 {
  413.                                     int i2 = 0;
  414.                                     while (_Final[x2, y2, i2] != null)
  415.                                     {
  416.                                         i2++;
  417.                                     }
  418.                                     for (int z2 = 0; z2 < i2; z2++)
  419.                                     {
  420.                                         if (_Final[x2, y2, z2] != null)
  421.                                         {
  422.                                             if (temp2 == _Final[x2, y2, z2])
  423.                                             {
  424.                                                 //found
  425.                                                 _Final2[x, y, o] = temp;
  426.                                                 o++;
  427.                                                 Log(temp.Substring(1) + " found in SCCM, will be removed from its group", logType.notification);
  428.                                             }
  429.                                            
  430.                                         }
  431.                                         else
  432.                                         {
  433.                                             Log(temp.Substring(1) + " not found in SCCM, will not be removed from its group", logType.warning);
  434.                                             //not found, do not remove
  435.                                         }
  436.                                     }
  437.                                 }
  438.                             }
  439.                             //if (_Final[x, y, z] != null)
  440.                             //{
  441.                             //    string curUser = _Final[x, y, z];
  442.                             //    if (temp2 == curUser)
  443.                             //    {
  444.                             //        //found
  445.                                    
  446.                             //        _Final2[x, y, o] = temp;
  447.                             //        o++;
  448.                             //    }
  449.  
  450.                             //}
  451.                             //else
  452.                             //{
  453.                                
  454.                             //    Log(temp.Substring(1) + "not found in SCCM, will not be removed from its group", logType.warning);
  455.  
  456.                             //    //not found, do not remove
  457.                             //    //_Final[x, y, z] = temp;
  458.                             //}
  459.                             //backgroundCompare1.ReportProgress(0, temp);
  460.  
  461.  
  462.  
  463.                         }
  464.                            
  465.                        
  466.                     }
  467.  
  468.                 }
  469.             }
  470.         }
  471.  
  472.         private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
  473.         {
  474.  
  475.         }
  476.  
  477.         private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
  478.         {
  479.             lblStatus.Visible = false;
  480.             progressBar1.Visible = false;
  481.             lblStatus2.Visible = false;
  482.             progressBar2.Visible = false;
  483.             lblGroup.Visible = true;
  484.             lblPass.Visible = true;
  485.             lblSite.Visible = true;
  486.             lblUserID.Visible = true;
  487.             lblUsers.Visible = true;
  488.             txtPass.Visible = true;
  489.             txtUser.Visible = true;
  490.             lstGroups.Visible = true;
  491.             lstSites.Visible = true;
  492.             lstUsers.Visible = true;
  493.             btnCancel.Visible = true;
  494.             btnCleanup.Visible = true;
  495.         }
  496.  
  497.         private void backgroundGetSCCM_DoWork(object sender, DoWorkEventArgs e)
  498.         {
  499.             Log("Getting data from SCCM", logType.notification);
  500.             int x = 0, y = 0, z = 0;
  501.             SqlConnection myConnection = new SqlConnection("Data Source=FTWSCCMSQL01\\INSTANCE1;Initial Catalog=SMS_TH1;Integrated Security=True");
  502.             try
  503.             {
  504.                 myConnection.Open();
  505.                 Log("SQL Connection opened", logType.notification);
  506.                 SqlDataReader myReader = null;
  507.                 SqlCommand myCommand = new SqlCommand("SELECT     v_R_System.User_Name0, v_RA_System_SystemOUName.System_OU_Name0 FROM v_R_System INNER JOIN v_RA_System_SystemOUName ON v_R_System.ResourceID = v_RA_System_SystemOUName.ResourceID WHERE     (v_RA_System_SystemOUName.System_OU_Name0 LIKE '%Clinical%') AND (v_R_System.User_Name0 IS NOT NULL) ORDER BY v_RA_System_SystemOUName.System_OU_Name0, v_R_System.User_Name0", myConnection);
  508.                 myReader = myCommand.ExecuteReader();
  509.                 Log("SQL Reader started", logType.notification);
  510.                 while (myReader.Read())
  511.                 {
  512.                     x = 0;
  513.                     y = 0;
  514.                     z = 0;
  515.                     string user = myReader[0].ToString();
  516.                     string OU = myReader[1].ToString();
  517.                     OU = OU.Substring(OU.IndexOf("/") + 1);
  518.                     OU = OU.Substring(OU.IndexOf("/") + 1);
  519.                     OU = OU.Substring(OU.IndexOf("/") + 1);
  520.                     //MessageBox.Show(OU);
  521.                     y = OU.Contains("WIRED") ? 0 : 1;
  522.  
  523.                     string site = OU.Substring(OU.IndexOf(" ") + 1);
  524.                     site = site.Substring(site.IndexOf(" ") + 1);
  525.                     //MessageBox.Show(OU);
  526.  
  527.                     switch (site)
  528.                     {
  529.                         case ("THHV"):
  530.                             //_sites[8] = "THHV";
  531.                             x = 8;
  532.                             break;
  533.                         case ("THPG"):
  534.                             //_sites[11] = "THPG";
  535.                             x = 11;
  536.                             break;
  537.                         case ("AMH"):
  538.                             //_sites[1] = "THAM";
  539.                             x = 1;
  540.                             break;
  541.                         case ("HEB"):
  542.                             //_sites[7] = "THHEB";
  543.                             x = 7;
  544.                             break;
  545.                         case ("HFW"):
  546.                             //_sites[6] = "THFW";
  547.                             x = 6;
  548.                             break;
  549.                         case ("HMEC"):
  550.                             //_sites[12] = "THS";
  551.                             x = 12;
  552.                             break;
  553.                         case ("HNW"):
  554.                             //_sites[2] = "THAZ";
  555.                             x = 2;
  556.                             break;
  557.                         case ("HSW"):
  558.                             //_sites[13] = "THSW";
  559.                             x = 13;
  560.                             break;
  561.                         case ("PHA"):
  562.                             //_sites[0] = "THA";
  563.                             x = 0;
  564.                             break;
  565.                         case ("PHD"):
  566.                             //_sites[4] = "THD";
  567.                             x = 4;
  568.                             break;
  569.                         case ("PHK"):
  570.                             //_sites[9] = "THK";
  571.                             x = 9;
  572.                             break;
  573.                         case ("PHP"):
  574.                             //_sites[10] = "THP";
  575.                             x = 10;
  576.                             break;
  577.                         case ("PHW"):
  578.                             //_sites[14] = "THW";
  579.                             x = 14;
  580.                             break;
  581.                         case ("THDN"):
  582.                             //_sites[5] = "THDN";
  583.                             x = 5;
  584.                             break;
  585.                         case ("WAL"):
  586.                             //_sites[3] = "THC";
  587.                             x = 3;
  588.                             break;
  589.                         default:
  590.                             MessageBox.Show(site);
  591.                             break;
  592.                     }
  593.                     //we now have site and group, now add to list of users for that
  594.                     while (_SCCMInfo[x, y, z] != null)
  595.                     {
  596.                         z++;
  597.                     }
  598.                     if (!Regex.IsMatch(user, @"^[\p{L}]+$"))
  599.                         _SCCMInfo[x, y, z] = user;
  600.                     else
  601.                         Log(user + " ignored from SCCM data.", logType.warning);
  602.  
  603.                     backgroundGetSCCM.ReportProgress(50, user);
  604.                     z = 0;
  605.                 }
  606.                 Log("SQL Reader finished, closing connection", logType.notification);
  607.                 myConnection.Close();
  608.                 myConnection.Dispose();
  609.             }
  610.             catch (Exception ex)
  611.             {
  612.                 MessageBox.Show(ex.Message, "SQL Error");
  613.                 Log(ex.Message, logType.error);
  614.                 return;
  615.             }
  616.         }
  617.  
  618.         private void backgroundGetSCCM_ProgressChanged(object sender, ProgressChangedEventArgs e)
  619.         {
  620.             progressBar2.Value = e.ProgressPercentage;
  621.             lblStatus2.Text = e.UserState.ToString();
  622.      
  623.         }
  624.  
  625.         private void backgroundGetSCCM_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
  626.         {
  627.             lblStatus.Text = "Getting Data from Active Directory";
  628.             progressBar1.Value = 66;
  629.             progressBar2.Value = 0;
  630.             progressBar2.Maximum = 15;
  631.             backgroundGetAD.RunWorkerAsync();
  632.         }
  633.  
  634.         private void backgroundGetAD_DoWork(object sender, DoWorkEventArgs e)
  635.         {
  636.             Log("Getting data from Active Directory", logType.notification);
  637.             //_ADInfo = null;
  638.             int x = 0, y = 0, z = 0, i=0;
  639.             foreach (string site in _sites)
  640.             {
  641.                 i++;
  642.                 backgroundGetAD.ReportProgress(i, site);
  643.                 y = 0;
  644.                 foreach (string group in _groups)
  645.                 {
  646.                     z = 0;
  647.                     ArrayList members = GetUsersInGroup("Clinical IDs " + site + " " + group);
  648.                     if (members != null)
  649.                     {
  650.                         foreach (string member in members)
  651.                         {
  652.                             _ADInfo[x, y, z] = member;
  653.                             z++;
  654.                         }
  655.                     }
  656.                     y++;
  657.                 }
  658.                 x++;
  659.             }
  660.         }
  661.  
  662.         private void backgroundGetAD_ProgressChanged(object sender, ProgressChangedEventArgs e)
  663.         {
  664.             progressBar2.Value = e.ProgressPercentage;
  665.             lblStatus2.Text = e.UserState.ToString();
  666.         }
  667.  
  668.         private void backgroundGetAD_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
  669.         {
  670.             lblStatus.Text = "Comparing Data";
  671.             progressBar1.Value = 80;
  672.             progressBar2.Value = 0;
  673.             progressBar2.Maximum = _SCCMInfo.Length + _ADInfo.Length;
  674.             backgroundCompare1.RunWorkerAsync();
  675.            
  676.         }
  677.  
  678.         private void backgroundCompare1_DoWork(object sender, DoWorkEventArgs e)
  679.         {
  680.             Log("Begin Comparing", logType.notification);
  681.             int x = 0, y = 0, z = 0, i = 0;
  682.             string temp = "";
  683.             bool end = false;
  684.             Log("Start with comparing SCCM to AD", logType.notification);
  685.             foreach (string site in _sites)
  686.             {
  687.                 y = 0;
  688.                 foreach (string group in _groups)
  689.                 {
  690.                     z = 0;
  691.                     i = 0;
  692.                     end = false;
  693.                     do
  694.                     {
  695.                         if (_SCCMInfo[x, y, z] != null)
  696.                         {
  697.                             temp = _SCCMInfo[x, y, z];
  698.                             for (i = 0; i < 10000; i++)
  699.                             {
  700.                                 if (_ADInfo[x, y, i] != null)
  701.                                 {
  702.                                     if (temp == _ADInfo[x, y, i])
  703.                                     {
  704.                                         //found
  705.                                         i = 10000;
  706.                                         _Final[x, y, z] = temp;
  707.                                     }
  708.  
  709.                                 }
  710.                                 else
  711.                                 {
  712.                                     i = 10000;
  713.                                     //not found
  714.                                     _Final[x, y, z] = "+" + temp;
  715.                                 }
  716.                                 //backgroundCompare1.ReportProgress(0, temp);
  717.                             }
  718.  
  719.                         }
  720.                         else
  721.                             end = true;
  722.  
  723.                         z++;
  724.                     } while (!end);
  725.                     y++;
  726.                 }
  727.                 x++;
  728.             }
  729.  
  730.             Log("Now reverse the comparison, to find what is being removed", logType.notification);
  731.             //Log("This has been disabled for now",logType.warning);
  732.  
  733.             x = 0;
  734.             y = 0;
  735.             z = 0;
  736.             i = 0;
  737.             temp = "";
  738.             end = false;
  739.  
  740.             foreach (string site in _sites)
  741.             {
  742.                 y = 0;
  743.                 foreach (string group in _groups)
  744.                 {
  745.                     z = 0;
  746.                     i = 0;
  747.                     int z2 = 0;
  748.                     while (_Final[x, y, z2] != null)
  749.                     {
  750.                         z2++;
  751.                     }
  752.  
  753.                     end = false;
  754.                     do
  755.                     {
  756.                         if (_ADInfo[x, y, z] != null)
  757.                         {
  758.                             temp = _ADInfo[x, y, z];
  759.                             for (i = 0; i < 2000; i++)
  760.                             {
  761.                                 if (_SCCMInfo[x, y, i] != null)
  762.                                 {
  763.                                     if (temp == _SCCMInfo[x, y, i])
  764.                                     {
  765.                                         //found
  766.                                         i = 2000;
  767.                                         for (int f = 0; f < 2000; f++)
  768.                                         {
  769.                                             if (_Final[x, y, f] == temp)
  770.                                             {
  771.  
  772.                                                 //already been found, don't add
  773.                                                 f = 1999;
  774.                                             }
  775.                                             if (_Final[x, y, f] == null && f != 1999)
  776.                                             {
  777.                                                 //not added previously, add now
  778.                                                 _Final[x, y, z2] = temp;
  779.                                                 z2++;
  780.                                                 f = 2000;
  781.                                             }
  782.                                         }
  783.                                     }
  784.  
  785.                                 }
  786.                                 else
  787.                                 {
  788.                                     i = 2000;
  789.                                     //not found
  790.                                     _Final[x, y, z2] = "-" + temp;
  791.                                     z2++;
  792.                                 }
  793.                                 //backgroundCompare1.ReportProgress(0, temp);
  794.  
  795.                             }
  796.  
  797.                         }
  798.                         else
  799.                             end = true;
  800.  
  801.                         z++;
  802.  
  803.                     } while (!end);
  804.  
  805.                     y++;
  806.                 }
  807.                 x++;
  808.  
  809.             }
  810.         }
  811.  
  812.         private void backgroundCompare1_ProgressChanged(object sender, ProgressChangedEventArgs e)
  813.         {
  814.             //progressBar2.Value += 1;
  815.             //lblStatus2.Text = e.UserState.ToString();
  816.         }
  817.  
  818.         private void backgroundCompare1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
  819.         {
  820.             backgroundWorker1.RunWorkerAsync();
  821.             lblStatus.Text = "Doing Final Comparison";
  822.             progressBar1.Value = 95;
  823.             progressBar2.Value = 0;
  824.         }
  825.     }
  826. }
  827.  
  828.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement