Friday, October 30, 2020

Automate BRE Xml File Import - PowerShell Code

There isn't a lot of information on how to automate a BRE xml file import.  Many suggest using the PowerShell provider that used to be available on Codeplex, but unfortunately it's now a dead page.  There are other resources (QuickLearn) that explain using assemblies in the BizTalk SDK folder.  These have been available since BizTalk 2013.  It appears that what was available in Codeplex has been lifted and shifted to the BizTalk installation process (to the SDK subfolder) on your local server.

This is all good, but there are some limitations.  First, Microsoft doesn't offer any real documentation for the PowerShell SDK.  I had to revert back to a different site to get documentation (version 1.4.0.1, published back in 2014 by Randal van Splunteren and Maxime Labelle) for the PowerShell Provider documentation.

Second, upon looking at this documentation, the BizTalk PowerShell provider doesn't allow for the creation (or importing) of certain artifacts. See screenshot below, highlighted area.


In my case I wanted to automate the deployment of a BRE Policy xml file to the BRE store.  I tested this using the BizTalk SDK assemblies, and the results are the same as the (older) documentation states - function not supported.

So I came across this on the Microsoft website which had a C# snippet of doing what I wanted.  I decided to convert it to PowerShell, since the rest of my processes use PowerShell for builds, releases, and deployments.  I now can now script this out to automate deployments of BRE xml files.  In my case, I use Azure DevOps to perform this using a 'PowerShell on Target Machines' task.

So here's my version of that - use and modify at your own risk.  Enjoy!

param
(
[Parameter(Position=0,Mandatory=$true, HelpMessage="Path to biztalk resource, e.g. C:\BizTalkDeploy\Maps\1.0.0.10")]
[string]$resourcePath,
[Parameter(Position=1,Mandatory=$true, HelpMessage="list of files to import, separated by a comma, e.g. (BRE.1.60.xml,BRE.1.61.xml)")]
[string]$resourceNames
)
# Load necessary BizTalk assemblies
try
{
Add-Type -Path 'C:\Program Files (x86)\Common Files\Microsoft BizTalk\Microsoft.Biztalk.RuleEngineExtensions.dll'
Add-Type -Path 'C:\Program Files (x86)\Common Files\Microsoft BizTalk\Microsoft.RuleEngine.dll'
}
catch [System.Reflection.ReflectionTypeLoadException]
{
Write-Host "Message: $($_.Exception.Message)"
Write-Host "StackTrace: $($_.Exception.StackTrace)"
Write-Host "LoaderExceptions: $($_.Exception.LoaderExceptions)"
}
# Main
try
{
foreach ($resourceName in $resourceNames.Split(','))
{
$RuleStore_Filter_All = 0 #Enum from original code
$fileLocation = Join-Path $resourcePath $resourceName
Write-Verbose "Beginning to import BRE file $($fileLocation)" -Verbose
$dd = New-Object Microsoft.BizTalk.RuleEngineExtensions.RuleSetDeploymentDriver
[Microsoft.RuleEngine.SqlRuleStore]$sqlRuleStore = $dd.GetRuleStore()
$fileRuleStore = [Microsoft.RuleEngine.FileRuleStore]::new($fileLocation)
[Microsoft.RuleEngine.RuleSetInfoCollection]$rsic = $fileRuleStore.GetRuleSets($RuleStore_Filter_All)
foreach($rsi in $rsic)
{
[Microsoft.RuleEngine.RuleSet] $ruleSet = $fileRuleStore.GetRuleSet($rsi)
$publishRuleSets = $true
$sqlRuleStore.Add($ruleSet, $publishRuleSets)
$dd.Deploy($rsi)
Write-Verbose "Successfully imported: $($rsi.Name), Version $($rsi.MajorRevision).$($rsi.MinorRevision)" -Verbose
}
}
Write-Verbose "Importing BRE file(s) completed" -Verbose
}
catch
{
throw
}