Tuesday, September 3, 2013

Exporting BizTalk Applications with BizTalkFactory PowerShell Provider

I've been reading "Powershell in a month of Lunches" and wanted to have something to apply the lessons to. Since I do a lot of BizTalk deployments, a BizTalk oriented script was a natural choice.

A lot of the scripts out there look like they come from folks who are used to scripting from the BizTalk / btstask perspective rather than really using the power of powershell and the BizTalkFactory PowerShell Provider.

First of all I have the following in my powershell profile so the powershell provider automatically loads every time I run powershell:

$InitializeDefaultBTSDrive = $false
Add-PSSnapin BiztalkFactory.Powershell.Extensions
Function Biztalk: { Set-Location Biztalk: }
Function Biztalk:\ { Set-Location Biztalk:\ }
The script itself is designed to export all applications on a server, including any binding file resources but not the default bindings or the webs, into an msi package. The default bindings are exported separately. It is useful to be able to see what the current bindings are as they may have been modified after installation.

My servers have environment variables that specify the BizTalk server and BizTalk database so these should be replaced as appropriate for your environment.

NOTE: The first line gets wrapped by the blog software!

new-psdrive -name Biztalk -psprovider Biztalk -root biztalk:\ -instance $env:BT_SERVER -database $env:BT_DATABASE

$apps = get-childitem -path biztalk:\applications\*

foreach($app in $apps)
{

  if ($app.isSystem -eq $false)
  {

    $appName = $app.Name

    $spec = Get-ApplicationResourceSpec $app

    foreach ($resource in $spec.ResourceSpec.Resources.Resource)
    {
      if ( ($resource.Type -eq "System.BizTalk:WebDirectory")
         -or  ($resource.Type -eq "System.BizTalk:BizTalkBinding"
         -and $resource.luid -eq "Application/$appName"))
      {
        $spec.ResourceSpec.Resources.RemoveChild($resource)
      }
    }

  export-application $app ".\$appName.msi" $spec
  export-bindings $app ".\$appName.xml"

  }
}
The script goes through all applications and retrieves the resource specification for each one. Web resources and default bindings are removed from the specification before the export-application call. Any bindings added as resources will remain in the resource spec.

The if statement selecting the resource.types may need to be on one line but was wrapped here for readability.

This was a very satisfying little project that I hope you can find a good use for!

No comments:

Post a Comment