Friday, January 8, 2021

Automate BRE Xml File Import - PowerShell Code, Part 2

BRE comes in two flavors - Policies and Vocabularies.  In the previous post, I gave an overview on how to publish a Policy via code.  

What about Vocabularies?  

The process is somewhat similar, but the methods are a little different.  In a Policy, you need to add the Policy, and then deploy it (optional) if you want it active.  For a Vocabulary, you just need to add it, since Vocabularies don't get deployed.

Also, if a Policy uses a Vocabulary, the Vocabulary needs to be added first (much like an assembly reference).

The code snippet below gives an overview of how I accomplished this.

# determine if Policy or Vocabulary - makes a difference on how we import it
[xml]$breFileXml = Get-Content $fileLocation
# node in Policy or Vocabulary. If one exists, the other will be null
$testPolicy = $breFileXml.brl.ruleset
$testVocab = $breFileXml.brl.vocabulary
if($testPolicy)
{
# BRE Policy code here
}
elseif($testVocab)
{
Write-Verbose "Beginning to import BRE Vocabulary file $($fileLocation)" -Verbose
$dd = New-Object Microsoft.BizTalk.RuleEngineExtensions.RuleSetDeploymentDriver
[Microsoft.RuleEngine.SqlRuleStore]$sqlRuleStore = $dd.GetRuleStore()
$fileRuleStore = [Microsoft.RuleEngine.FileRuleStore]::new($fileLocation)
[Microsoft.RuleEngine.VocabularyInfoCollection]$vic = $fileRuleStore.GetVocabularies($RuleStore_Filter_All)
foreach($vi in $vic)
{
[Microsoft.RuleEngine.Vocabulary] $vocabulary = $fileRuleStore.GetVocabulary($vi)
$publishedVocabulary = $true
$sqlRuleStore.Add($vocabulary, $publishedVocabulary)
# Vocabs can only be added/published, no need to deploy
Write-Verbose "Successfully imported: $($vi.Name), Version $($vi.MajorRevision).$($vi.MinorRevision)" -Verbose
}
}
else
{
#???
}