Exporting all applications on a BizTalk group

Posted: October 4, 2007  |  Categories: BizTalk Uncategorized

 One of the steps I added to the change document at my client where we are separating the high usage applications into their own hosts was that all BizTalk applications should be exported to MSI files before the changes. This way they will have available the most recent version of the applications before any changes are done. We will also export the modified applications after the changes so that the bindings in the MSI files are pointing to the correct host.

 You can export BizTalk applications to MSI files from the BizTalk Server Administration Console, by right clicking on the application and selecting Export -> MSI and then following the different steps of the wizard. This method gives you a lot of flexibility. Another way of exporting an application is by using the BTSTask.exe command line tool with the -ExportApp parameter. This will export the application with the default options you see on the wizard.

Wouldn’t it be nice if BTSTask.exe or the admin console would let you export all applications in a BizTalk group, or let you choose a few to export? Well, just for fun and to save us a few minutes at the client I created a little console application that gets the list of applications and exports them. It uses Microsoft.BizTalk.ExplorerOM.dll to get the list of applications from the management database then loops through them calling BTSTask.exe to perform the export. The app assumes it is running from a computer with BizTalk Server 2006 installed. It accepts two parameters: a connection to the BizTalk management database and the location where the MSI files are going to be created, but they both have default values. This could easily be turned into a windows forms application where the user could choose what applications should be exported.

 Anyways, here is the code:
 

using System;
using System.Reflection;
using Microsoft.BizTalk.ExplorerOM;
using System.Diagnostics;
 
namespace ExportAllBizTalkApps
{
    class Program
    {
        //Build strings with default values
        private static string executableFileName;
        private static string exportLocation;
        private static string connectionString = "SERVER=.;DATABASE=BizTalkMgmtDb;Integrated Security=SSPI";
 
        static void Main(string[] args)
        {
            try
            {
 
                executableFileName = Assembly.GetExecutingAssembly().Location;
                exportLocation = System.IO.Path.GetDirectoryName(executableFileName);
                connectionString = "SERVER=.;DATABASE=BizTalkMgmtDb;Integrated Security=SSPI";
 
                if ((args.Length == 1) && (args[0].Contains("?")))
                {
                    ShowHelp();
                    return;
                }
 
                // Command line arguments
                if (args.Length > 0)
                {
                    Array.ForEach(args, SetArgValue);
                }
 
                //Connect to the root
                BtsCatalogExplorer btsCatalog = new BtsCatalogExplorer();
                btsCatalog.ConnectionString = connectionString;
                string commandArguments = "";
 
                //Loop through list of applications
                foreach (Application application in btsCatalog.Applications)
                {
                    //Skip BizTalk.System - It can't be exported
                    if (application.Name != "BizTalk.System")
                    {
                        Console.WriteLine("Exporting Application " + application.Name);
                        //Set arguments for BTSTask.exe
                        commandArguments = "ExportApp -ApplicationName:\"" + application.Name + "\" -package:\"" +
                                       exportLocation + "\\" + application.Name + ".msi\"";
 
                        //Create and start BTSTask.exe process
                        Process p = new Process();
                        p.StartInfo.UseShellExecute = false;
                        p.StartInfo.FileName = "BTSTask.exe";
                        p.StartInfo.Arguments = commandArguments;
                        p.StartInfo.RedirectStandardError = true;
                        p.StartInfo.RedirectStandardOutput = true;
                        p.StartInfo.CreateNoWindow = true;
                        p.Start();
                        //Write result
                        while (p.StandardOutput.Peek() > -1)
                            Console.WriteLine(p.StandardOutput.ReadToEnd());
                        p.WaitForExit();
                    }
                }
 
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error during exports:");
                Console.WriteLine(ex.Message);
            }
 
        }
 
        private static void SetArgValue(string arg)
        {
            //Check first 3 characters
            if (arg.Length > 3)
            {
                switch (arg.Substring(0, 3))
                {
                    case "-c:":
                        connectionString = arg.Substring(3, arg.Length - 3);
                        break;
                    case "-l:":
                        exportLocation = arg.Substring(3);
                        break;
                    default:
                        ShowHelp();
                        throw new Exception("Invalid parameter: " + arg);
                }
            }
            else
            {
                ShowHelp();
                throw new Exception("Invalid parameter: " + arg);
            }
        }
 
        static private void ShowHelp()
        {
            Console.WriteLine("Usage:");
            Console.WriteLine();
            Console.WriteLine("ExportAllBizTalkApps -c:<Connection String> -l:<Export Location>");
            Console.WriteLine("or");
            Console.WriteLine("ExportAllBizTalkApps -?");
            Console.WriteLine();
            Console.WriteLine("Where:");
            Console.WriteLine();
            Console.WriteLine("-? = Returns this usage message.");
            Console.WriteLine();
            Console.WriteLine("-c:<Connection String> = Connection string between double quotes to the BizTalk management database from where the list of applications will be retrieved. Defaults to \"SERVER=.;DATABASE=BizTalkMgmtDb;Integrated Security=SSPI\"");
            Console.WriteLine();
            Console.WriteLine("-l:<Export Location> = The folder where the MSI files are going to be created. Optional, defaults to the location of the executable.");
            Console.WriteLine();
            Console.WriteLine("Examples:");
            Console.WriteLine("ExportAllBizTalkApps -l:c:\\ExportedMSIs");
            Console.WriteLine("ExportAllBizTalkApps -l:\"c:\\Program Files\\Microsoft BizTalk Server\\ExportedMSI\"");
            Console.WriteLine("ExportAllBizTalkApps -c:\"SERVER=MyBizTalkSQLTestServer;DATABASE=TESTBizTalkMgmtDb;Integrated Security=SSPI\"");
            Console.WriteLine();
        }
    }
}
BizTalk360
BizTalk Server

Over 500+ customers across
30+ countries depend on BizTalk360

Learn More
Serverless360
Azure

Manage and monitor serverless
components effortlessly

Learn More
Atomicscope
Business Users

Monitor your Business Activity in iPaaS
or Hybrid integration solutions

Learn More

Back to Top