How to fix or configure the Deployment Properties of a Visual Studio BizTalk Project with PowerShell version 2

Posted: August 18, 2016 in BizTalk, PowerShell
Tags: , , , , , , ,

It is nothing new that before you can deploy a solution from Visual Studio into a BizTalk application, you must first set project properties, especially the Server and the Configuration Database. Otherwise two things may happen:

  • Deployment will fail when you try do to deploy it through Microsoft Visual Studio or will you are redeploying a previously deployed BizTalk project.
  • The assemblies and all their artifacts will be deployed to and unwanted BizTalk Application (normally BizTalk Application 1)

Previous I wrote a post regarding how to fix the Deployment Properties of a Visual Studio BizTalk Project with PowerShell, you can know more about it here. However, during my talks about tips and tricks I realize that the previous script was a little limited, it only worked if the deployment properties already exist in the file. This means that, if we were working on a new project or we obtaining a copy of the project from the source control, the script didn’t correctly setup or fix the deployment settings.

I promised that I would fix it and now I’m keeping my word. And I have to say that, this is probably one of scripts that I use the most and the reason why is…

So, why this script is important?

Well, if a solution in Visual Studio contains multiple projects, you need to manually configure the deployment properties for each project.

And you must keep in mind that these setting are under the Project user option file(s) (“*.btproj.user”) that are typically not under source control. The reason is that the Visual Studio deployment feature main focus is for development scenarios where one user settings might differ from another. And it will be up to you, or your client, to decide if you want these files to be checked in and available to all of the developers on your team. (you can read it more about this here)

So, this seems a slight and easy task but now imagine that you have almost 200 projects inside a unique Visual Studio Solution! It will be an insane and consuming task to do and most likely to happen is you to fall asleep in front of the pc.

With this PowerShell you will be able to parameterize all projects inside a Visual Studio Solution running a single line of code and avoid spending numerous hours doing this task manually.

PowerShell script overview
    $allPropertyGroup = $xml.Project.PropertyGroup
    foreach($node in $allPropertyGroup)
    {
        if($node.Server -ne $null)
        {
            #If the Deployment Setting already exists, then we just need to update them
            $addNewNodeFlag = $false
            $node.Server= $ServerName;
            $node.ConfigurationDatabase= $DatabaseName;
            $node.ApplicationName= $ApplicationName;
            $node.Redeploy= $RedeployFlag;
            $node.Register= $RegisterFlag;
            $node.RestartHostInstances= $RestartHostInstancesFlag;
        }
    }

    if($addNewNodeFlag -eq $true)
    {
        #Add a PropertyGroup node if the Deployment Setting doesn't exist
        $newXmlPropertyGroup = $xml.CreateElement("PropertyGroup", "http://schemas.microsoft.com/developer/msbuild/2003")
        $newXmlPropertyGroup.SetAttribute(“Condition”,”'`$(Configuration)|`$(Platform)' == 'Debug|AnyCPU'”);

        $newXmlElement = $newXmlPropertyGroup.AppendChild($xml.CreateElement("Server", "http://schemas.microsoft.com/developer/msbuild/2003"));
        $newXmlTextNode = $newXmlElement.AppendChild($xml.CreateTextNode($ServerName));
 
        $newXmlElement = $newXmlPropertyGroup.AppendChild($xml.CreateElement("ConfigurationDatabase", "http://schemas.microsoft.com/developer/msbuild/2003"));
        $newXmlTextNode = $newXmlElement.AppendChild($xml.CreateTextNode($DatabaseName));

        $newXmlElement = $newXmlPropertyGroup.AppendChild($xml.CreateElement("ApplicationName", "http://schemas.microsoft.com/developer/msbuild/2003"));
        $newXmlTextNode = $newXmlElement.AppendChild($xml.CreateTextNode($ApplicationName));

        $newXmlElement = $newXmlPropertyGroup.AppendChild($xml.CreateElement("Redeploy", "http://schemas.microsoft.com/developer/msbuild/2003"));
        $newXmlTextNode = $newXmlElement.AppendChild($xml.CreateTextNode($RedeployFlag));

        $newXmlElement = $newXmlPropertyGroup.AppendChild($xml.CreateElement("Register", "http://schemas.microsoft.com/developer/msbuild/2003"));
        $newXmlTextNode = $newXmlElement.AppendChild($xml.CreateTextNode($RegisterFlag));

        $newXmlElement = $newXmlPropertyGroup.AppendChild($xml.CreateElement("RestartHostInstances", "http://schemas.microsoft.com/developer/msbuild/2003"));
        $newXmlTextNode = $newXmlElement.AppendChild($xml.CreateTextNode($RestartHostInstancesFlag));

        $xml.Project.InsertAfter($newXmlPropertyGroup, $xml.Project.PropertyGroup[1])
    }

THIS SQL SCRIPT IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND.

You can download the PowerShell Script used here from:

BizTalk Server: Fixing BizTalk Project Deployment Properties with PowerShell (1 KB)
Microsoft | TechNet Gallery

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s