February 2008 - Posts - Rudolf Henning

February 2008 - Posts

Following on yesterdays post about creating a host the next step is to create a host instance. This sample create the instance and start it as well.

Here is the code:

---------------------------------------------------------------

using System;
using System.Management;

namespace CreateStartHostInstance
{
    class AddStartHostInstance
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main(string[] args)
        {
            string bts_WMINameSpace;
            string bts_ServerAppTypeNameSpace;
            string bts_HostInstanceNameSpace;
            string user;
            string pwd;
            string hostName;
            string serverName;

            bts_ServerAppTypeNameSpace = "MSBTS_ServerHost";
            bts_HostInstanceNameSpace = "MSBTS_HostInstance";
            // WMI NameSpace for BizTalk Server
            bts_WMINameSpace = @"root\MicrosoftBizTalkServer";

            try
            {
                if (args.Length < 4)
                {
                    ShowHelp();
                }
                else if (args[0] == "-?" || args[0] == "/?" || args[0] == "/h" || args[0] == "/h")
                {
                    ShowHelp();
                }
                else
                {
                    hostName = args[0];
                    serverName = args[1];
                    user = args[2];
                    pwd = args[3];

                    PutOptions options = new PutOptions();
                    options.Type = PutType.CreateOnly;
                    ManagementObject bts_AdminObjectServerHost = null;
                    ManagementObject bts_AdminObjectHostInstance = null;
                    System.Management.ObjectGetOptions bts_objOptions = new ObjectGetOptions();
                    // Creating instance of BizTalk Host.
                    ManagementClass bts_AdminObjClassServerHost = new ManagementClass(bts_WMINameSpace, bts_ServerAppTypeNameSpace, bts_objOptions);
                    bts_AdminObjectServerHost = bts_AdminObjClassServerHost.CreateInstance();
                    // Make sure to put correct Server Name,username and // password
                    bts_AdminObjectServerHost["ServerName"] = serverName;
                    bts_AdminObjectServerHost["HostName"] = hostName;
                    bts_AdminObjectServerHost.InvokeMethod("Map", null);

                    ManagementClass bts_AdminObjClassHostInstance = new ManagementClass(bts_WMINameSpace, bts_HostInstanceNameSpace, bts_objOptions);
                    bts_AdminObjectHostInstance = bts_AdminObjClassHostInstance.CreateInstance();
                    // Make Sure you correct HostName and MachineName for HostInstance name,
                    bts_AdminObjectHostInstance["Name"] = "Microsoft BizTalk Server " + hostName + " " + serverName;

                    object[] objparams = new object[3];
                    objparams[0] = user;
                    objparams[1] = pwd;
                    objparams[2] = true;
                    bts_AdminObjectHostInstance.InvokeMethod("Install", objparams);
                    Console.WriteLine("Host instance created successfully!");
                    bts_AdminObjectHostInstance.InvokeMethod("Start", null);
                    Console.WriteLine("Host instance started successfully!");
                }
            }
            catch (Exception e)
            {

                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine(e.Message);
                Console.ForegroundColor = ConsoleColor.White;
            }
        }

        private static void ShowHelp()
        {
            Console.WriteLine("Usage: CreateStartHostInstance.exe <HostName> <ServerName> <UserName> <Password>");
        }
    }
}
 

---------------------------------------------------------------
 

Something I only discovered this morning (thanks Brandon) is a nice small utility to stop/start BizTalk 2006 applications. You would have thought MS should have include this functionality in the plain BTSTask utility.

 It can be found here
 

with no comments
Filed under:

Being an BizTalk server administrator now I've been looking around for some tool and scripts to make life easier (being a developer at heart). I found some good samples here . I made some 'additions'  to make it more useful and have some reuse of it through different environments. I though of sharing this so others might have some use for it too.

 I hope the formatting doesn't suck to much :)

--------------------------------- 

using System;
using System.IO;
using System.Xml;
using System.Management;

/**
 * As per sample
 * http://book.itzero.com/read/microsoft/0505/sams.microsoft.biztalk.server.2004.unleashed.nov.2004.ebook-lib_html/0672325985/ch17lev1sec4.html#PLID0
 *
 */

namespace CreateHost
{
    class Program
    {
        static void Main(string[] args)
        {
            if (args.Length < 1)
            {
                ShowHelp();
            }
            else if (args[0] == "-?" || args[0] == "/?" || args[0] == "/h" || args[0] == "/h")
            {
                ShowHelp();
            }
            else
            {
                string bts_WMINameSpace;
                string bts_HostSettingNameSpace;
                string newHostName = args[0];
                string ntGroupName = "BizTalk Application Users";
                bool isDefault = false;
                bool hostTracking = false;
                bool authTrusted = false;

                // WMI NameSpace for BizTalk Server
                bts_WMINameSpace = @"root\MicrosoftBizTalkServer";
                // WMI class for HostSetting
                bts_HostSettingNameSpace = "MSBTS_HostSetting";

                try
                {
                    for (int i = 1; i < args.Length; i++)
                    {
                        if (argsIdea.StartsWith("NTGroupName:"))
                        {
                            ntGroupName = argsIdea.Replace("NTGroupName:", "");
                        }
                        else if (argsIdea.StartsWith("IsDefault:"))
                        {
                            isDefault = bool.Parse(argsIdea.Replace("IsDefault:", ""));
                        }
                        else if (argsIdea.StartsWith("HostTracking:"))
                        {
                            hostTracking = bool.Parse(argsIdea.Replace("HostTracking:", ""));
                        }
                        else if (argsIdea.StartsWith("AuthTrusted:"))
                        {
                            authTrusted = bool.Parse(argsIdea.Replace("AuthTrusted:", ""));
                        }
                    }

                    PutOptions options = new PutOptions();
                    options.Type = PutType.CreateOnly;
                    ManagementObject bts_AdminObject = null;
                    System.Management.ObjectGetOptions bts_objOptions = new ObjectGetOptions();
                    // Creating instance of BizTalk Host.
                    ManagementClass bts_AdminObjClass = new ManagementClass(bts_WMINameSpace, bts_HostSettingNameSpace, bts_objOptions);
                    bts_AdminObject = bts_AdminObjClass.CreateInstance();
                    ManagementObject appObject = bts_AdminObject;
                    // Assigning Host Properties.
                    appObject["Name"] = newHostName;
                    appObject["NTGroupName"] = ntGroupName;
                    appObject["IsDefault"] = isDefault;
                    appObject["HostTracking"] = hostTracking;
                    appObject["AuthTrusted"] = authTrusted;
                    appObject["HostType"] = 1;
                    appObject.Put(options);
                    Console.WriteLine("Host created successfully!");
                }
                catch (Exception e)
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine (e.Message);
                    Console.ForegroundColor = ConsoleColor.White;
                }
            }
        }

        private static void ShowHelp()
        {
            Console.WriteLine("Usage: CreateHost.exe <HostName> [NTGroupName:<NTGroupName>] [IsDefault:<true|false>] [HostTracking:<true|false>] [AuthTrusted:<true|false>]");
            Console.WriteLine("Note: only In-process hosts can be created this way");
        }
    }
}


------------------------------ 

with no comments
Filed under: