BizTalk Benchmark Wizard

Quick C# console to install host instances/prerequisites for BizTalk Benchmark Wizard

// --
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Management;

namespace ConCreateAndMapBizTalkHost
{
    class Program
    {
        private const string ServerName = "";
        private const string NTGroupName = "Biztalk Application Users";
        private const string UserName = "bts_user";
        private const string Password = "Pa$$w0rd";
        private const string ReceiveHost = "Receiving";
        private const string SendHost = "Sending";
        private const string ProcessingHost = "Processing";

        static void Main(string[] args)
        {
            CreateHost("BBW_RxHost", 1, NTGroupName, false, false);
            MapInstallHostInstance("BBW_RxHost", ServerName, UserName, Password);
            CreateReceiveHandler("BBW_RxHost", "WCF-NetTcp", "MSBTS_ReceiveHandler");

            CreateHost("BBW_TxHost", 1, NTGroupName, false, false);
            MapInstallHostInstance("BBW_TxHost", ServerName, UserName, Password);
            CreateSendHandler("BBW_TxHost", "WCF-NetTcp", "MSBTS_SendHandler2");

            CreateHost("BBW_PxHost", 1, NTGroupName, false, true);
            MapInstallHostInstance("BBW_PxHost", ServerName, UserName, Password);

            System.Console.ReadKey();
        }


        //Basic WMI opertaion - Create  
        //Sample to show MSBTS_HostSetting instance creation  
        public static void CreateHost(string hostName, int hostType, string ntGroupName, bool authTrusted, bool hostTracking)
        {
            try
            {
                var options = new PutOptions
                {
                    Type = PutType.CreateOnly
                };

                //create a ManagementClass object and spawn a ManagementObject instance  
                ManagementClass objHostSettingClass = new ManagementClass("root\\MicrosoftBizTalkServer", "MSBTS_HostSetting", null);
                ManagementObject objHostSetting = objHostSettingClass.CreateInstance();

                //set the properties for the Managementobject  
                objHostSetting["Name"] = hostName;
                objHostSetting["HostType"] = hostType;
                objHostSetting["NTGroupName"] = ntGroupName;
                objHostSetting["AuthTrusted"] = authTrusted;
                objHostSetting["IsHost32BitOnly"] = false;
                objHostSetting["HostTracking"] = hostTracking;

                //create the Managementobject  
                objHostSetting.Put(options);
                System.Console.WriteLine("Host - " + hostName + " - has been created successfully");
            }
            catch (Exception excep)
            {
                System.Console.WriteLine("CreateHost - " + hostName + " - failed: " + excep.Message);
            }
        }

        public static void MapInstallHostInstance(string hostName, string svrName, string uid, string pwd)
        {

            ManagementClass svrHostClass = null;
            ManagementObject svrHostObject = null;

            ManagementClass hostInstClass = null;
            ManagementObject hostInstObject = null;

            try
            {
                //Build the name of the HostInstance - name has to be in the below format  
                string hostInstanceName = "Microsoft BizTalk Server" //Name of product  
                                          + " " + hostName //Name of Host of which instance is to be created  
                                          + " " + svrName; //Name of Server on which instance is to be created  

                //Create an instance of the ServerHost class using the System.Management namespace  
                ObjectGetOptions svrHostOptions = new ObjectGetOptions();
                svrHostClass = new ManagementClass("root\\MicrosoftBizTalkServer", "MSBTS_ServerHost", svrHostOptions);
                svrHostObject = svrHostClass.CreateInstance();

                //Set the properties of the ServerHost instance  
                svrHostObject["ServerName"] = svrName;
                svrHostObject["HostName"] = hostName;

                //Invoke the Map method of the ServerHost instance  
                svrHostObject.InvokeMethod("Map", null);

                //Create an instance of the HostInstance class using the System.Management namespace  
                ObjectGetOptions hostInstOptions = new ObjectGetOptions();
                hostInstClass = new ManagementClass("root\\MicrosoftBizTalkServer", "MSBTS_HostInstance", hostInstOptions);
                hostInstObject = hostInstClass.CreateInstance();


                //Set the properties of the HostInstance class  
                hostInstObject["Name"] = hostInstanceName;

                //Build a parameter array  
                object[] args = new object[2];
                args[0] = uid;
                args[1] = pwd;

                //Invoke the Install method of the HostInstance  
                hostInstObject.InvokeMethod("Install", args);

                Console.WriteLine("HostInstance was mapped and installed successfully. Mapping created between Host: " +
                                  hostName + " and Server: " + svrName);
                return;
            }
            catch (Exception excep)
            {
                Console.WriteLine("Failure during HostInstance creation: " + excep.Message);
            }
            finally
            {
                if (hostInstObject != null) hostInstObject.Dispose();
                if (hostInstClass != null) hostInstClass.Dispose();

                if (svrHostObject != null) svrHostObject.Dispose();
                if (svrHostClass != null) svrHostClass.Dispose();
            }

        }

        //Sample to show MSBTS_ReceiveHandler instance creation with CustomCfg property  
        public static void CreateReceiveHandler(string hostName, string adapterName, string handlerType)
        {
            try
            {
                var options = new PutOptions
                {
                    Type = PutType.CreateOnly
                };

                //create a ManagementClass object and spawn a ManagementObject instance  
                var objReceiveHandlerClass = new ManagementClass("root\\MicrosoftBizTalkServer", "MSBTS_ReceiveHandler", null);
                var objReceiveHandler = objReceiveHandlerClass.CreateInstance();

                //set the properties for the Managementobject  
                objReceiveHandler["AdapterName"] = adapterName;
                objReceiveHandler["HostName"] = hostName;
                // objReceiveHandler["CustomCfg"] = "<CustomProps><AdapterConfig vt=\"8\"><Config xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"><userName>Username</userName><password>Password</password><accountName>FTP Account</accountName><representationType>binary</representationType><spoolingFolder>c:\\Temp</spoolingFolder><maximumBatchSize>10</maximumBatchSize><maximumNumberOfFiles>10</maximumNumberOfFiles><passiveMode>False</passiveMode><beforeGet>BeforeGet</beforeGet><afterGet>AfterGet</afterGet><firewallType>NoFirewall</firewallType><firewallAddress>FireWallAddress</firewallAddress><firewallPort>21</firewallPort><firewallUserName>Username</firewallUserName><firewallPassword>Password</firewallPassword><commandLogFilename>c:\\Log</commandLogFilename><errorThreshold>10</errorThreshold><maxFileSize>100</maxFileSize></Config></AdapterConfig></CustomProps>";

                //create the Managementobject  
                objReceiveHandler.Put(options);
                System.Console.WriteLine("ReceiveHandler - " + adapterName + " " + hostName + " - has been created successfully");
            }
            catch (Exception excep)
            {
                System.Console.WriteLine("CreateReceiveHandler - " + adapterName + " " + hostName + " - failed: " + excep.Message);
            }
        }

        public static void CreateSendHandler(string hostName, string adapterName, string handlerType)
        {
            try
            {
                var options = new PutOptions
                {
                    Type = PutType.CreateOnly
                };

                //create a ManagementClass object and spawn a ManagementObject instance  
                var objSendHandlerClass = new ManagementClass("root\\MicrosoftBizTalkServer", handlerType, null);
                var objSendHandler = objSendHandlerClass.CreateInstance();

                //set the properties for the Managementobject  
                objSendHandler["AdapterName"] = adapterName;
                objSendHandler["HostName"] = hostName;
                // objReceiveHandler["CustomCfg"] = "<CustomProps><AdapterConfig vt=\"8\"><Config xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"><userName>Username</userName><password>Password</password><accountName>FTP Account</accountName><representationType>binary</representationType><spoolingFolder>c:\\Temp</spoolingFolder><maximumBatchSize>10</maximumBatchSize><maximumNumberOfFiles>10</maximumNumberOfFiles><passiveMode>False</passiveMode><beforeGet>BeforeGet</beforeGet><afterGet>AfterGet</afterGet><firewallType>NoFirewall</firewallType><firewallAddress>FireWallAddress</firewallAddress><firewallPort>21</firewallPort><firewallUserName>Username</firewallUserName><firewallPassword>Password</firewallPassword><commandLogFilename>c:\\Log</commandLogFilename><errorThreshold>10</errorThreshold><maxFileSize>100</maxFileSize></Config></AdapterConfig></CustomProps>";

                //create the Managementobject  
                objSendHandler.Put(options);
                System.Console.WriteLine("SendHandler - " + adapterName + " " + hostName + " - has been created successfully");
            }
            catch (Exception excep)
            {
                System.Console.WriteLine("CreateSendHandler - " + adapterName + " " + hostName + " - failed: " + excep.Message);
            }
        }

    }
}