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
{
///
/// Command line application that can stop,
/// start or restart the BizTalk Application
/// hosts on the local machine.
///

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!


Tomas Restrepo

Software developer located in Colombia.