static void Main(string[] args)
        {
            SharePoint      spMethods = new SharePoint();
            ActiveDirectory adMethods = new ActiveDirectory();

            spMethods.getComputerAssets();
            foreach (ComputerInfo c in ComputerInfo.computerNames)
            {
                BitlockerData bitlockerData = adMethods.getBitlockerInfo(c);
                if (bitlockerData.recoveryGuid != null)
                {
                    spMethods.updateBitlockerList(c, bitlockerData);
                }
            }
        }
Beispiel #2
0
        /*Connecting to Active Directory, searching for the current object's computerName property, determining if the AD computer object
         * has Bitlocker data associated with it, if Bitlocker data is present, the Bitlocker Recovery Key, Recovery Guid and Date are assigned to properties
         * of a BitlockerData object, that object is then returned to the calling function*/
        public BitlockerData getBitlockerInfo(ComputerInfo c)
        {
            BitlockerData bitlockerInfoObject = new BitlockerData();

            using (DirectoryEntry parent = new DirectoryEntry("LDAP://wcc.local:636"))
            {
                using (DirectorySearcher LdapSearcher = new DirectorySearcher(parent))
                {
                    LdapSearcher.Filter = string.Concat("(&(objectClass=computer)(name=", c.computerName, "))");
                    SearchResult srcComp = LdapSearcher.FindOne();
                    if (srcComp != null)
                    {
                        using (DirectoryEntry compEntry = srcComp.GetDirectoryEntry())
                        {
                            try
                            {
                                Object objValue = Marshal.BindToMoniker(srcComp.GetDirectoryEntry().Path.Replace("GC://", "LDAP://"));
                                Type   tType    = objValue.GetType();
                                tType.InvokeMember("Filter",
                                                   System.Reflection.BindingFlags.SetProperty | System.Reflection.BindingFlags.Public, null,
                                                   objValue, new Object[] { "msFVE-RecoveryInformation" });
                                foreach (Object obj in (IEnumerable)objValue)
                                {
                                    Guid     gRecoveryGUID = new Guid((Byte[])obj.GetType().InvokeMember("msFVE-RecoveryGuid", System.Reflection.BindingFlags.GetProperty | System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance, null, obj, null, null, null, null));
                                    string   name          = obj.GetType().InvokeMember("name", System.Reflection.BindingFlags.GetProperty | System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance, null, obj, null, null, null, null).ToString();
                                    string   dateString    = name.Substring(3, name.IndexOf("T", System.StringComparison.Ordinal) - 3);
                                    DateTime date          = Convert.ToDateTime(dateString);
                                    string   dateOnly      = date.ToString().Substring(0, date.ToString().IndexOf(" "));
                                    string   time          = name.Substring(name.IndexOf("T", System.StringComparison.Ordinal) + 1, name.IndexOf("{", System.StringComparison.Ordinal) - 20);
                                    string   objTime       = DateTime.Parse(time).ToString("h:mm:ss tt");
                                    time = objTime;
                                    DateTime dateTime = Convert.ToDateTime(dateOnly + " " + time);
                                    if (gRecoveryGUID != null)
                                    {
                                        bitlockerInfoObject.recoveryGuid     = gRecoveryGUID.ToString().ToUpper();
                                        bitlockerInfoObject.recoveryPassword = obj.GetType().InvokeMember("msFVE-RecoveryPassword", System.Reflection.BindingFlags.GetProperty | System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance, null, obj, null, null, null, null).ToString();
                                        bitlockerInfoObject.date             = dateTime;
                                    }
                                }
                            }
                            catch { }
                        }
                    }
                }
            }
            return(bitlockerInfoObject);
        }
        //adding Bitlocker information to the Bitlocker list on SharePoint
        public void updateBitlockerList(ComputerInfo c, BitlockerData b)
        {
            /*determing if the Bitlocker list already contains the current iteration's data. This is determined using a CamlQuery
             * that filters out all list items except those share the computer name and owner of the of the current ComputerInfo object.
             * Then, we iterate over the collection of list items gathered with the CamlQuery, comapring the Date Added field the date property of
             * the BitlockerData object, if any of the dates in the list item collection match the date in the BitlockerData object, a exists bool is set to true,
             * and the data will not be added, if no match is found, a new list item will be added to the Bitlocker list*/
            using (ClientContext context = new ClientContext("https://sharepoint.wilsonconst.com/it-site"))
            {
                List      assetsList = context.Web.Lists.GetByTitle("Bitlocker");
                CamlQuery query      = new CamlQuery()
                {
                    ViewXml = "<View><Query><Where><Eq><FieldRef Name='Title' /><Value Type='Text'>" + c.computerName + "</Value></Eq><Eq><FieldRef Name='User2' /><Value Type='Text'>" + c.computerOwner + "</Value></Eq></And></Where></Query></View>"
                };
                ListItemCollection collection = assetsList.GetItems(query);
                context.Load(collection);
                context.ExecuteQuery();
                bool exists = false;
                foreach (ListItem i in collection)
                {
                    DateTime spDateTime = Convert.ToDateTime(i["Date_x0020_Added"].ToString()).ToLocalTime();
                    if (spDateTime == b.date)
                    {
                        exists = true;
                    }
                }

                if (!exists)
                {
                    ListItemCreationInformation creationInfo = new ListItemCreationInformation();
                    ListItem newItem = assetsList.AddItem(creationInfo);
                    newItem["Title"]              = c.computerName;
                    newItem["User2"]              = c.computerOwner;
                    newItem["Identifier"]         = b.recoveryGuid;
                    newItem["Recovery_x0020_Key"] = b.recoveryPassword;
                    newItem["Date_x0020_Added"]   = b.date;
                    newItem.Update();
                    context.ExecuteQuery();
                }
            }
        }