Verify your SCCM Distribution Point Content is Synced with your Source Content

December 2nd, 2010

This code will compare your package’s file size on your DP with the file size of the package’s source location. Each package is listed on a separate line with out-of-sync ones colored red.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
using System.Threading;

namespace compareFinpkgsrcToDP
{
    class Program
    {
        struct aPackage
        {
            public string packageID;
            public string sourcePath;
            public string name;
            public Int32 sourceSize;
        }

        static string databaseServerName = "KCSQL-01"; //SCCM database server name
        static string databaseUserID = "smithj"; //SCCM database userID
        static string databasePassword = "p@ssw0rd"; //SCCM database password
        static string databaseName="SCCM_01"; //SCCM database name
        static string dpShare = @"\\KCDP-01\smspkgc$\" //Distribution point share (include ending backslash)

        static void Main(string[] args)
        {
            DateTime beginTime = DateTime.Now;
            ArrayList thePackages = new ArrayList(500);

            using (SqlConnection con = new SqlConnection())
            {
                con.ConnectionString = "Data Source=" + databaseServerName + ";User ID=" + databaseUserID + ";Password=" + databasePassword + ";Initial Catalog=" + databaseName + ";Network Library=dbnmpntw;Connect Timeout=5";

                con.Open();

                SqlCommand com = con.CreateCommand();
                com.CommandType = CommandType.Text;
                //com.Parameters.Add("@sent", SqlDbType.Bit).Value = 0;

                com.CommandText = "SELECT PkgID , Source, Name , SourceSize from SMSpackages where source <> '' and source not like '%.wim' order by name ";

                SqlDataReader dr = com.ExecuteReader();

                if (dr.HasRows)
                {
                    while (dr.Read())
                    {
                        aPackage tempPackage;
                        tempPackage.packageID = dr.GetValue(0).ToString();
                        tempPackage.sourcePath = dr.GetValue(1).ToString();
                        tempPackage.name = dr.GetValue(2).ToString();
                        tempPackage.sourceSize = Convert.ToInt32(dr.GetValue(3).ToString());

                        if (tempPackage.sourcePath.Contains(".wim"))
                            continue;

                        thePackages.Add(tempPackage);

                        //int theIndex = Convert.ToInt32(dr.GetValue(0));
                        //DateTime dateToSend = DateTime.Parse(dr.GetValue(1).ToString());
                        //string toAddress = dr.GetValue(2).ToString();

                    }
                }
            }

            foreach (aPackage tempPackage in thePackages)
            {
                Console.ResetColor();
                Console.Write(tempPackage.name + "   ");

                try
                {
                    long totalLength1 = 0;
                    long totalLength2 = 0;

                    var theFiles1 = Directory.GetFiles(tempPackage.sourcePath, "*", SearchOption.AllDirectories);
                    var theFiles2 = Directory.GetFiles(dpShare + tempPackage.packageID, "*", SearchOption.AllDirectories);

                    foreach (string theFile in theFiles1)
                    {
                        FileInfo theInfo = new FileInfo(theFile);
                        //FileAttributes theAttributes = File.GetAttributes(theFile);
                        totalLength1 += theInfo.Length;
                    }

                    foreach (string theFile in theFiles2)
                    {
                        FileInfo theInfo = new FileInfo(theFile);
                        //FileAttributes theAttributes = File.GetAttributes(theFile);
                        totalLength2 += theInfo.Length;
                    }
                    Console.SetCursorPosition(0, Console.CursorTop);

                    if (totalLength1 == totalLength2)
                    {
                        Console.ForegroundColor = ConsoleColor.Green;
                    }
                    else
                    {
                        Console.ForegroundColor = ConsoleColor.Red;
                    }
                    Console.Write(tempPackage.name);
                    Console.CursorLeft = 55;
                    Console.WriteLine(totalLength1 + ":" + totalLength2);
                }
                catch { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("ERROR - " + tempPackage.name); }
            }

            Console.ResetColor();
            Console.WriteLine("Done, Elapsed time: "+ (DateTime.Now - beginTime).Minutes+ " minutes");
            Console.ReadKey();
        }
    }
}