Monday 22 July 2013

PowerShell Script to Export All BizTalk Applications from Server (using Powershell Provider for BizTalk)

I recently wrote a PowerShell script to export all the applications from a BizTalk server as MSIs and xml files for Bindings.

I have used "Powershell provider for BizTalk" for this. I found this tool quite useful, it really reduces the line of code you have to write.

I thought I will share it, may be someone finds it useful:

The first 5 lines are to load Powershell Provider.You will need to provide the SQL Server Name to connect to.

Set-ExecutionPolicy –ExecutionPolicy RemoteSigned

Write-Host "Loading Powershell"


$InitializeDefaultBTSDrive = $false


Add-PSSnapin BizTalkFactory.Powershell.Extensions


New-PSDrive -Name BizTalk -PSProvider BizTalk -Root BizTalk:\ -Instance SQLServerName -Database BizTalkMgmtDb

Write-Host "Creating MSI"



Now we will get all applications from Biztalk, excluding System ones.
$Applications = @(Get-ChildItem -Path Biztalk:\Applications |  Where-Object {$_.IsSystem -eq $false})

Set-Location –Path BizTalk:\Applications



Loop through applications to export, I have used Resource specification to exclude IIS web directories and bindings, if you want to export everything , just skip the portion which removes nodes from ResSpec xml file :

while($Applications.length -ne 0 -and $Applications -ne $null)

{
Write-Host $Applications.length
Write-Host $Applications[0].Name

$ApplPath = "C:\MSI\" + $Applications[0].Name + ".msi"
$BindPath = "C:\MSI\" + $Applications[0].Name + ".xml"
$ResPath = "C:\MSI\" + $Applications[0].Name + "ResSpec.xml"

Write-Host $ApplPath


(Get-ApplicationResourceSpec -Path $Applications[0].Name).OuterXml | Out-File $ResPath 


$xmlResource = [xml] (Get-Content $ResPath )


$delnodes = $xmlResource.SelectNodes("/*[local-name()='ResourceSpec' and namespace-uri()='http://schemas.microsoft.com/BizTalk/ApplicationDeployment/ResourceSpec/2004/12']/*[local-name()='Resources' and namespace-uri()='http://schemas.microsoft.com/BizTalk/ApplicationDeployment/ResourceSpec/2004/12']/*[local-name()='Resource' and namespace-uri()='http://schemas.microsoft.com/BizTalk/ApplicationDeployment/ResourceSpec/2004/12'][@Type='System.BizTalk:WebDirectory']")


    ForEach($delnode in $delnodes)

    {
        [void]$xmlResource.ResourceSpec.Resources.RemoveChild($delnode)
    }

 $delnodes = $xmlResource.SelectNodes("/*[local-name()='ResourceSpec' and namespace-uri()='http://schemas.microsoft.com/BizTalk/ApplicationDeployment/ResourceSpec/2004/12']/*[local-name()='Resources' and namespace-uri()='http://schemas.microsoft.com/BizTalk/ApplicationDeployment/ResourceSpec/2004/12']/*[local-name()='Resource' and namespace-uri()='http://schemas.microsoft.com/BizTalk/ApplicationDeployment/ResourceSpec/2004/12'][@Type='System.BizTalk:BizTalkBinding'][@Luid='Application/$appName']")


    ForEach($delnode in $delnodes)

    {
        [void]$xmlResource.ResourceSpec.Resources.RemoveChild($delnode)
    }

$xmlResource.Save($ResPath )


Export-Application -Path $Applications[0].Name -Package $ApplPath –ResourceSpec $ResPath


Export-Bindings -Path $Applications[0].Name -Destination $BindPath


if($Applications.length -eq 1)

{
$Applications = $null
}
else
{
$Applications = $Applications[1..($Applications.length -1)]
}
}

Write-Host "MSI and Bindings exported"


When you run the script (using Powershell (x86)) it will export all the applications as MSI and binding files in a file location(C:\MSI).