Example #1
0
 // <Snippet1>
 private static void PrintColumn(DataTableReader reader)
 {
     // Loop through all the rows in the DataTableReader
     while (reader.Read())
     {
         if (reader.IsDBNull(2))
         {
             Console.Write("<NULL>");
         }
         else
         {
             try
             {
                 Console.Write(reader.GetGuid(2));
             }
             catch (InvalidCastException)
             {
                 Console.Write("Invalid data type.");
             }
         }
         Console.WriteLine();
     }
 }
 public Guid GetGuid(int i)
 {
     return(_reader.GetGuid(i));
 }
Example #3
0
        private GuidCollection GetSoftwareBulletins()
        {
            GuidCollection bulletin_collection = new GuidCollection();

            DataTable bulletins = GetExistingBulletins();

            Console.WriteLine("# {0} bulletins returned by the stored procedure execution.", bulletins.Rows.Count);
            DataTable excluded_bulletins = GetExcludedBulletins();

            Console.WriteLine("# {0} bulletin names found in the exclusion table.", excluded_bulletins.Rows.Count);

            if (bulletins.Rows.Count == 0)
            {
                return(bulletin_collection);
            }

            try {
                using (DataTableReader sqlRdr = bulletins.CreateDataReader()) {
                    #region Get position of the used field
                    int pos_released = -1;
                    int pos_res_guid = -1;
                    int pos_bulletin = -1;
                    int pos_severity = -1;
                    int pos_vendor   = -1;

                    for (int i = 0; i < sqlRdr.FieldCount; i++)
                    {
                        string field_name = sqlRdr.GetName(i).ToLower();
                        if (field_name == "released")
                        {
                            pos_released = i;
                        }
                        if (field_name == "_resourceguid")
                        {
                            pos_res_guid = i;
                        }
                        if (field_name == "bulletin")
                        {
                            pos_bulletin = i;
                        }
                        if (field_name == "severity")
                        {
                            pos_severity = i;
                        }
                        if (field_name == "vendor")
                        {
                            pos_vendor = i;
                        }
                    }

                    bool field_init = false;
                    if (pos_severity != -1 && pos_res_guid != -1 && pos_released != -1 && pos_bulletin != -1)
                    {
                        field_init = true;
                    }
                    #endregion

                    if (config.Debug)
                    {
                        Console.WriteLine("# Field positions are:\n\tBulletin={0}\n\tReleased={1}\n\tResourceGuid={2}\n\tSeverity={3}\n\tVendor={4}", pos_bulletin, pos_released, pos_res_guid, pos_severity, pos_vendor);
                    }

                    if (field_init)
                    {
                        while (sqlRdr.Read())
                        {
                            Guid     bguid       = sqlRdr.GetGuid(pos_res_guid);
                            String   bull_name   = sqlRdr.GetString(pos_bulletin);
                            String   sev         = sqlRdr.GetString(pos_severity);
                            DateTime dt          = sqlRdr.GetDateTime(pos_released);
                            String   bull_vendor = string.Empty;
                            if (pos_vendor != -1)
                            {
                                bull_vendor = sqlRdr.GetString(pos_vendor).ToLower();
                            }

                            bool row_excluded = false;

                            #region // Break if the current bulletin is excluded
                            foreach (DataRow r in excluded_bulletins.Rows)
                            {
                                if (r[0].ToString() == bull_name)
                                {
                                    row_excluded = true;
                                    break;
                                }
                            }

                            if (row_excluded)
                            {
                                continue;
                            }
                            #endregion

                            if ((sev.ToLower() == config.Severity.ToLower() || config.Severity == "*") && dt >= config.Released_After && dt <= config.Released_Before)
                            {
                                if (pos_vendor == -1 || config.Vendor_Name == bull_vendor || config.Vendor_Name == "*")
                                {
                                    if (config.Debug)
                                    {
                                        Console.WriteLine("\tWe have a match: {0} from {1}", bull_name, bull_vendor);
                                    }
                                    bulletin_collection.Add(bguid);
                                }
                            }
                        }
                    }
                    else
                    {
                        Console.WriteLine("Failed to find the required fields in the provided data table. Not doing anything.");
                    }
                }
            } catch (Exception e) {
                Console.WriteLine("Error: {0}\nException message = {1}\nStack trace = {2}.", e.Message, e.InnerException, e.StackTrace);
            }
            Console.WriteLine("{0} bulletins match the {1} severity and will be checked for policies.", bulletin_collection.Count, config.Severity);
            return(bulletin_collection);
        }
        private GuidCollection GetSoftwareBulletins()
        {
            GuidCollection bulletin_collection = new GuidCollection();

            DataTable bulletins = GetExistingBulletins();

            EventLog.ReportInfo("GetExistingBulletins row count = " + bulletins.Rows.Count);
            DataTable excluded_bulletins = GetExcludedBulletins();

            EventLog.ReportInfo("GetExcludedBulletins row count = " + excluded_bulletins.Rows.Count);

            if (bulletins.Rows.Count == 0)
            {
                EventLog.ReportInfo("There are no bulletins to manage. Returning now.");
                return(bulletin_collection);
            }

            try {
                using (DataTableReader sqlRdr = bulletins.CreateDataReader()) {
                    // Field position shorthands
                    int _released     = -1;
                    int _resourceguid = -1;
                    int _bulletin     = -1;
                    int _severity     = -1;
                    int _vendor       = -1;

                    while (sqlRdr.Read())
                    {
                        #region Get position of the used field

                        for (int i = 0; i < sqlRdr.FieldCount; i++)
                        {
                            string field_name = sqlRdr.GetName(i).ToLower();
                            if (field_name == "released")
                            {
                                _released = i;
                            }
                            if (field_name == "_resourceguid")
                            {
                                _resourceguid = i;
                            }
                            if (field_name == "bulletin")
                            {
                                _bulletin = i;
                            }
                            if (field_name == "severity")
                            {
                                _severity = i;
                            }
                            if (field_name == "vendor")
                            {
                                _vendor = i;
                            }
                        }

                        bool field_init = false;
                        if (_severity != -1 && _resourceguid != -1 &&
                            _released != -1 && _bulletin != -1)
                        {
                            field_init = true;
                        }
                        #endregion

                        if (field_init)
                        {
                            while (sqlRdr.Read())
                            {
                                DateTime dt            = sqlRdr.GetDateTime(_released);
                                Guid     bulletin_guid = sqlRdr.GetGuid(_resourceguid);
                                String   bull_name     = sqlRdr.GetString(_bulletin);
                                String   sev           = sqlRdr.GetString(_severity);
                                String   bull_vendor   = string.Empty;
                                if (_vendor != -1)
                                {
                                    bull_vendor = sqlRdr.GetString(_vendor).ToLower();
                                }

                                #region // Break if the current bulletin is excluded
                                bool row_excluded = false;

                                foreach (DataRow r in excluded_bulletins.Rows)
                                {
                                    if (r[0].ToString() == bull_name)
                                    {
                                        EventLog.ReportInfo("Excluded bulletin " + bull_name);
                                        row_excluded = true;
                                        break;
                                    }
                                }

                                if (row_excluded)
                                {
                                    continue;
                                }
                                #endregion

                                if ((sev.ToUpper() == config.Severity.ToUpper() || config.Severity == "*") &&
                                    dt > config.Released_After && dt < config.Released_Before)
                                {
                                    if (_vendor == -1 || (config.Vendor_Name == bull_vendor || config.Vendor_Name == "*"))
                                    {
                                        bulletin_collection.Add(bulletin_guid);
                                        EventLog.ReportVerbose("Added bulletin " + bulletin_guid.ToString() + " to bulletin collection.");
                                    }
                                    else
                                    {
                                        if (_debug)
                                        {
                                            EventLog.ReportVerbose("Failed on " + config.Vendor_Name + " == " + bull_vendor);
                                        }
                                    }
                                }
                                else
                                {
                                    if (_debug)
                                    {
                                        EventLog.ReportVerbose("Failed on " + sev + "==" + config.Severity
                                                               + " or on " + dt.ToString() + " > " + config.Released_After.ToString()
                                                               + " or on " + dt.ToString() + " < " + config.Released_Before.ToString());
                                    }
                                    if (_debug)
                                    {
                                        EventLog.ReportVerbose("Bulletin " + bulletin_guid.ToString() + " _not_ added to bulletin collection.");
                                    }
                                }
                            }
                        }
                        else
                        {
                            EventLog.ReportInfo("Failed to find the required fields in the provided data table. Not doing anything.");
                        }
                    }
                }
            } catch (Exception e) {
                throw (e);
            }
            EventLog.ReportInfo(string.Format("{0} bulletins match the {1} severity and will be checked for policies.", bulletin_collection.Count, config.Severity));
            return(bulletin_collection);
        }