Ron's Sandbox

Clearing Your Cache

Tuesday, 21 July 2009 03:46 by hagermanr

While working with the Exchange Server Admins, we had an issue where they had to delete the SCOM cache rather often on a large number of servers. The process for this is to log into each host, stop the OpsMgr Health Service service, delete the Health Service State folder then restart the service.

To simplify this, I created a simple C# console application to do this remotely. You pass it a host name and if you have admin rights, it will do the work for you.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.IO;

namespace ClearCache
{
    class Program
    {
        static void Main(string[] args)
        {
            string sMessage = "";
            string sServiceStartCmd = @"\\" + args[0].ToString() + @" start HealthService";
            string sServiceStopCmd = @"\\" + args[0].ToString() + @" stop HealthService";
            string sServiceQueryCmd = @"\\" + args[0].ToString() + @" query HealthService";

            ConsoleKeyInfo cki;

            if (args.Length != 1)
            {
                Console.WriteLine(@"Usage: ClearCache.exe <Host Name>");

                Environment.Exit(1);
            }

            if (args[0].Substring(0, 1) == "/")
            {
                switch (args[0].Substring(1, args[0].Length-1))
                {
                    case "help":
                        sMessage = "Usage: ClearCache.exe <Host Name>" + Environment.NewLine;
                        sMessage += "This application uses your admin rights to access the c$ share on ";
                        sMessage += "your servers. It will stop the health service, delete the cache files ";
                        sMessage += "then start the service."+Environment.NewLine;
                        sMessage += "Example: ClearCache.exe mp-dsmt-23" + Environment.NewLine;

                        Console.WriteLine(sMessage);
                        Console.ReadKey();

                        Environment.Exit(0);

                        break;
                    case "?":
                        sMessage = "Usage: ClearCache.exe <Host Name>" + Environment.NewLine;
                        sMessage += "This application uses your admin rights to access the c$ share on ";
                        sMessage += "your servers. It will stop the health service, delete the cache files ";
                        sMessage += "then start the service." + Environment.NewLine;
                        sMessage += "Example: ClearCache.exe mp-dsmt-23" + Environment.NewLine;

                        Console.WriteLine(sMessage);
                        Console.ReadKey();

                        Environment.Exit(0);

                        break;
                    case "Help":
                        sMessage = "Usage: ClearCache.exe <Host Name>" + Environment.NewLine;
                        sMessage += "This application uses your admin rights to access the c$ share on ";
                        sMessage += "your servers. It will stop the health service, delete the cache files ";
                        sMessage += "then start the service." + Environment.NewLine;
                        sMessage += "Example: ClearCache.exe mp-dsmt-23" + Environment.NewLine;

                        Console.WriteLine(sMessage);
                        Console.ReadKey();

                        Environment.Exit(0);

                        break;
                }
            }

            try
            {
                Console.WriteLine("Stopping the health service...");

                System.Diagnostics.Process.Start(@"C:\Windows\System32\sc.exe", sServiceStopCmd);

                string sBaseFile = @"\\" + args[0] + @"\c$\Program Files\System Center Operations Manager 2007\";

                sBaseFile += "Health Service State";

                Console.WriteLine("Deleting Cache located at " + sBaseFile + "...");

                Directory.Delete(sBaseFile, true);

                Console.WriteLine("Starting Health Service...");

                System.Diagnostics.Process.Start(@"C:\Windows\System32\sc.exe", sServiceStartCmd);

                Console.WriteLine("Cache has been cleared and services started...");
            }
            catch(Exception e)
            {
                Console.WriteLine("Failed to clear the cache. The error is: ");
                Console.WriteLine(e.Message);
                Console.WriteLine("The current service state is: ");

                System.Diagnostics.Process.Start(@"C:\Windows\System32\sc.exe", sServiceQueryCmd);

                Console.WriteLine("Would you like to restart the service? [y/n]");

                cki = Console.ReadKey(true);

                if (cki.ToString() == "y")
                    System.Diagnostics.Process.Start(@"C:\Windows\System32\sc.exe", sServiceStartCmd);
                else
                    Console.WriteLine("Ok then, we are done here!");

                Console.ReadKey();
                Environment.Exit(1);
            }

            Console.ReadKey();
        }
    }
}

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Comments

Add comment


(Will show your Gravatar icon)  

  Country flag

biuquote
  • Comment
  • Preview
Loading