BTSReset

Link. July 2, 2005. Comments [0]. Posted in: BizTalk
During this week, I overheard a couple of guys mentioning how nice it would be to have a BTSReset tool, just like there is an iisreset one (sorry Mattias, didn't catch your last name, and I'm terrible with names anyway).

So, during a few minutes of boredom I went ahead and put together one, and here's the code (it's really simple thanks to the BTS WMI object model):


//
// BTSReset.cs
//
// Author:
//    Tomas Restrepo (tomasr@mvps.org)
//

using System;
using System.Management;

namespace Winterdom.BizTalk.Utilities
{

   /// <summary>
   /// Command line application that can stop,
   /// start or restart the BizTalk Application 
   /// hosts on the local machine.
   /// </summary>
   class BtsResetApp
   {
      [Flags]
      enum Options
      {
         Stop = 0x01,
         Start = 0x10,
         Reset = Stop | Start
      }

      public static void Main(string[] args)
      {
         Options opts = Options.Reset;

         if ( args.Length == 0 ) {
            opts = Options.Reset;
         } else if ( args[0] == "stop" ) {
            opts = Options.Stop;
         } else if ( args[0] == "start" ) {
            opts = Options.Start;
         } else {
            PrintUsage();
            return;
         }

         ProcessOptions(opts);
      }

      private static void ProcessOptions(Options opts)
      {
         EnumerationOptions enumOptions = new EnumerationOptions();
         enumOptions.ReturnImmediately = false;
         ManagementObjectSearcher searcher = new ManagementObjectSearcher (
                           "root\\MicrosoftBizTalkServer", 
                           "Select * from MSBTS_HostInstance where HostType=1", 
                           enumOptions
                        );

         if ( (opts & Options.Stop) != 0 ) 
         {
            foreach ( ManagementObject inst in searcher.Get() ) 
            {
               if ( (UInt32)inst["ServiceState"] != 1 ) {
                  Console.WriteLine("Stopping {0}...", inst["HostName"]);
                  inst.InvokeMethod("Stop", null);
               }
            }
         }
         if ( (opts & Options.Start) != 0 ) 
         {
            foreach ( ManagementObject inst in searcher.Get() ) 
            {
               if ( (UInt32)inst["ServiceState"] == 1 ) {
                  Console.WriteLine("Starting {0}...", inst["HostName"]);
                  inst.InvokeMethod("Start", null);
               }
            }
         }
         searcher.Dispose();
      }

      private static void PrintUsage()
      {
         Console.WriteLine("usage: btsreset.exe [start | stop]");
         Console.WriteLine("   no arguments = restart all host instances");
         Console.WriteLine("   start = start all stopped host instances");
         Console.WriteLine("   stop = stop all started host instances");
      }

   } // class BtsResetApp

} // namespace Winterdom.BizTalk.Utilities

Enjoy!



Comments are closed.

Syndicate

About

Tomas Restrepo is a software developer located in Colombia, South America. His interests include .NET, Connected Systems, PowerShell and lately dynamic programming languages. More...

tomasrestrepo @ twitter My Flickr photostream My saved links on delicious My Technorati Profile

email: tomas@winterdom.com
msn: tomasr@passport.com

View my profile on LinkedIn

MVP logo

Ads


Categories

Statistics

Total Posts: 1051
This Year: 2
This Month: 2
This Week: 1
Comments: 827

Archive

Other

Copyright © 2002-2008, Tomas Restrepo.

Powered by: newtelligence dasBlog 2.2.8279.16125

Sign In