Friday, October 18, 2013

Powershell to create BizTalk applications with references

The latest script to undergo migration to powershell is my application creation script. Our applications have references to each other so this script takes command line parameters for the application name and the references. If the application already exists the references will be added.

As usual for my latest blog entries, this script uses WMI and the BizTalk ExplorerOM. I also have minimized the error handling in order make the code easier to read.

Watch out for line wrapping from the blog software!
# define parameters: $app is application name
# $ref is a comma delimited string of references to create "Common,Schemas"
param([string] $app, [string[]] $ref)
 
 # Get local BizTalk DBName and DB Server from WMI
 $btsSettings = get-wmiobject MSBTS_GroupSetting -namespace 'root\MicrosoftBizTalkServer'
 $dbInstance = $btsSettings.MgmtDbServerName
 $dbName = $btsSettings.MgmtDbName
 
 # Load BizTalk ExplorerOM
 [void] [System.reflection.Assembly]::LoadWithPartialName("Microsoft.BizTalk.ExplorerOM")
 $BizTalkOM = New-Object Microsoft.BizTalk.ExplorerOM.BtsCatalogExplorer
 $BizTalkOM.ConnectionString = "SERVER=$dbInstance;DATABASE=$dbName;Integrated Security=SSPI"

#check incoming parameter for application name exists
 if (! $app)
{ 'Syntax is: Add_References -app "NewAppName" -ref Common,Schemas'; exit}

#create the application if it doesn't exist already 
if ($BizTalkOM.Applications[$app] -eq $null)
{
   $result =( btstask addapp /application:$app)
   "btstask result:" + $result
   $BizTalkOM.Refresh()
}

#add application references
foreach ($reference in $ref)
{
   "adding reference to " + $reference
   $BizTalkOM.Applications[$app].AddReference($BizTalkOM.Applications[$reference])
}

# Commit changes
$BizTalkOM.SaveChanges()  

I couldn't find a way to add an application without using btstask. Let me know if you know of a way, without using the Powershell Provider.

No comments:

Post a Comment