The field “FilterBytes” contains the subscription filters for batching. We are converting from 2010 to 2013, and I need the a report of the “source of truth” as to which of our customers goes to which party, so we build the same parties correctly in 2013.

select top 1000 
  p1.Name as PartnerNameA, 
  p2.Name as PartnerNameB, 
  ag.Name as AgreementName,
  bd.OnewayAgreementId,
  bd.Name as BatchDescriptionName,
  ag.PartnershipId,
  --bd.ReleaseCriteriaType,
  --bd.MessageCount,
  --bd.MessageScope,
  --bd.InterchangeSize,
  --bd.RecurrenceType,
  --bd.FilterBytes,
  --Convert(varchar(max), bd.FilterBytes) as 'ConvertFilterBytes',
  replace(replace(replace(replace(replace(replace(replace(CONVERT(VARCHAR(max), FilterBytes),'','') ,'',''), '',''),'',''),'',''),'',''),'','') as FilterBytesStripped,
  p1.PartnerId as P1_PartnerID,
  p2.PartnerId as P2_PartnerID,
  ps.PartnerAId ,
  ps.PartnerBId 
  from tpm.BatchDescription bd 
  inner join tpm.Agreement ag on ag.ReceiverOnewayAgreementId = bd.OnewayAgreementId 
  inner join tpm.Partnership ps on ps.PartnershipId = ag.PartnershipId 
  inner join tpm.Partner p1 on p1.PartnerId = ps.PartnerAId 
  inner join tpm.Partner p2 on p2.PartnerId = ps.PartnerBID 
  where bd.Protocol = 'x12' 
  order by p1.Name 

The replace functions above remove some of the special characters (but not all of them). Some of those special characters are used as separators between keywords like filterOperator, filterValue, and property and the following value. Sorry if the replace characters don’t come through to the blog; not sure how to easily put them here.
FilterBytes is stored in hex, so you can use the convert to VARCHAR(max) to make it somewhat more readable.

You can take a sample value of FilterBytes and put in NotePad++.
If you split the FilterBytes on the at sign, it looks something like this:

@FilterPredicateOhttp://schemas.datacontract.org/2004/07/Microsoft.BizTalk.B2B.PartnerManagement i)http://www.w3.org/2001/XMLSchema-instance@groups@FilterGroup
@ statements
@FilterStatement
@filterOperator™Equals
@filterValue™?http://schemas.microsoft.com/BizTalk/EDI/X12/2006#X12_00501_210
@property™BTS.MessageType
@FilterStatement
@filterOperator™Equals@filterValue™123456
@property™5ABC.Client.Schemas.EDI210_5010.ST03_CustId

Note: Some of the other special characters seem to prevent the find command from working in NotePad++ and even in Powershell. I never stopped to figure that out or research more.

I don’t have access to 2010 prod where I work, so I asked the BizTalk admin to run the above query then save to a CSV. I then wrote a Powershell to do the more difficult work of parsing the filterBytes there. I might be able to share that in the future.

You might open a file like this:

ISA*00* *00* *ZZ*123000013 *ZZ*ABC3001 *160719*1600*U*00401*201160030*0*P*;~

and then want to know what is company with ID=123000013? I don’t know of any any way in the BizTalk Admin console to do a reverse lookup like this.


select 
       P.PartnerId as 'ParterID', 
       P.Name as 'PartyName', 
       BP.Name as 'ProfileName', 
	   BP.ProfileID,
	   BI.Qualifier, 
	   BI.Description, 
	   BI.Value, 
       P2.PartnerId as 'PartnerID', 
	   P2.Name as 'OtherPartyName', 
	   BP2.Name as 'OtherProfileName' ,
	   BP2.ProfileID,
	   BI2.Qualifier, 
	   BI2.Description, 
	   BI2.Value, 
	   * 
from tpm.BusinessIdentity  BI 
inner join tpm.BusinessProfile BP   on BI.ProfileId = BP.ProfileId
inner join tpm.Partner P            on P.PartnerID = BP.PartnerID 
inner join tpm.Partnership ps       on P.PartnerID = ps.PartnerAId 
inner join tpm.Partner P2           on P2.PartnerID = ps.PartnerBId
inner join tpm.BusinessProfile BP2  on P2.PartnerID = BP2.PartnerID 
inner join tpm.BusinessIdentity BI2 on BI2.ProfileId = BP2.ProfileId
where BI.Value = '123000013' 
-- and BI2.Value = 'xxxxxxx'
order by P.Name, BP.Name 

I’ve been doing a lot of “forensic” research on converting old systems lately. Sometimes I need to look at hundreds of ports at once, and opening each one separately to check the map is way to slow!

There are several joins required. The actual mapname is in the more generic bts_item table. You can get there by joining bt_MapSpec.

use BizTalkMgmtDb
select app.nvcName,
       sp.nvcName as SendPort, 
       item.Name as MapName,
       spt.nvcAddress, 
       bTwoWay, 
       sp.nvcSendPipelineData, 
       sp.nvcFilter
  from bts_sendport sp 
inner join bts_sendport_transport spt on spt.nSendPortID = sp.nID and spt.nTransportTypeId is not null 
inner join bts_sendport_transform spMap on spMap.nSendPortID = sp.nID 
inner join bt_MapSpec ms on ms.id = spmap.uidTransformGUID 
inner join bts_item item on ms.itemid = item.id 
inner join bts_application app on sp.nApplicationID = app.nID 
where sp.nvcName like '%210%'
order by app.nvcName, sp.nvcName 

select app.nvcName,
       rp.nvcName as ReceivePort, 
       item.Name as MapName,
       bTwoWay
from bts_receiveport rp 
inner join bts_receiveport_transform tr on tr.nReceivePortID = rp.nID 
inner join bt_MapSpec ms on ms.id = tr.uidTransformGUID 
inner join bts_item item on ms.itemid = item.id 
inner join bts_application app on rp.nApplicationID = app.nID 
where rp.nvcName like '%210%'
order by app.nvcName, rp.nvcName 

This program uses a mix of Object Explore Model (ExplorerOM) and BizTalk Provider for Powershell
to delete Send/ReceivePorts that match certain naming conventions.

We were refactoring an application and re-adding ports under a different application, so needed a script to delete the old pots.

If you run this script, I do of course suggest you run it once with the Delete Statements commented out!
Recode the selection criteria as needed for your purposes.

Parts of code from examples here: https://docs.microsoft.com/en-us/biztalk/core/receiveports-biztalk-server-sample

#===============================================================#
#=== ===#
#=== Delete the receive port named “My Receive Port” ===#
#=== ===#
#===============================================================#
Function DeleteSendPort ($sendPortName)
{
$receivePort = $catalog.ReceivePorts[$sendPortName]

if ($receivePort -ne $null)
{
$catalog.RemoveReceivePort($receivePort)

#=== Try to commit the changes made so far. If the commit fails, ===#
#=== roll back all changes in the trap handler. ===#
$catalog.SaveChanges()
}
}

Function DeleteReceivePort ($receivePortName)
{
$receivePort = $catalog.ReceivePorts[$receivePortName]

if ($receivePort -ne $null)
{
$catalog.RemoveReceivePort($receivePort)

#=== Try to commit the changes made so far. If the commit fails, ===#
#=== roll back all changes in the trap handler. ===#
$catalog.SaveChanges()
}
}

#===================#
#=== Main Script ===#
#===================#

#=== Make sure the ExplorerOM assembly is loaded ===#

[void] [System.reflection.Assembly]::LoadWithPartialName(“Microsoft.BizTalk.ExplorerOM”)

#=== Connect to the BizTalk Management database ===#

$Catalog = New-Object Microsoft.BizTalk.ExplorerOM.BtsCatalogExplorer
$MyBTSQLServer = “.” #substitute your SQL server name here
$Catalog.ConnectionString = “SERVER=$MyBTSQLServer;DATABASE=BizTalkMgmtDb;Integrated Security=SSPI”

#==================================================================#
#=== Register a trap handler to discard changes on exceptions ===#
#=== Execution will continue in the event we want to delete the ===#
#=== receive port. ===#
#==================================================================#

$Script:NoExceptionOccurred = $true
$ErrorActionPreference=”silentlycontinue”
trap
{
$Script:NoExceptionOccurred = $false
“Exception encountered:`r`n”; $_; “`r`nDiscarding changes and continuing execution so we can attempt to clean up the receive port…`r`n”
$Catalog.DiscardChanges()
}

#=== Create the new receive port ===#
#Write-Host “`r`nAttempting to create `”My Receive Port`”…”
#CreateReceivePort

# used on EDI1 to get lists of TRLG send/receive ports related to 204/990
# and create CSV file
cls
$showDate = Get-Date -DisplayHint Date
Write-Host “Started at $showDate”

Add-PSSnapIn -Name BiztalkFactory.PowerShell.Extensions #NOTE: Must be in 32-bit version of Powershell to use this SnapIn
#New-PSDrive -Name BizTalk -Root BizTalk:\ -PsProvider BizTalk -Instance EDI1-BTSDB2014\DB04 -Database BizTalkMgmtDb

Write-Host “— SendPorts —”
cd “Biztalk:\Applications\Echo.BSSR.Surface\Send Ports”
$SendPorts = Get-ChildItem
ForEach ($SendPort in $SendPorts)
{
#Write-Host $SendPort.Name $SendPort.Status
if (
($SendPort.Name.Contains(‘204’) -and $SendPort.Name.ToUpper().Contains(‘OUT’)) -or
($SendPort.Name.Contains(‘990’) -and $SendPort.Name.ToUpper().Contains(‘IN’))
)
{
## two formats of SendPorts spOutbound204XXXX and spSurfaceXXXoutbound204
Write-Host “Delete SendPort $($SendPort.Name)”
#DeleteSendPort($SendPort.Name)

}
}

Write-Host “— Receives —”
cd “Biztalk:\Applications\Echo.BSSR.Surface\Receive Ports”
$ReceivePorts = Get-ChildItem
ForEach ($ReceivePort in $ReceivePorts)
{
#Write-Host $ReceivePort.Name
if ($ReceivePort.Name.Contains(‘990’) -and $ReceivePort.Name.ToUpper().Contains(‘INBOUND’) )
{
Write-Host “Delete ReceivePort $($ReceivePort.Name )”
#DeleteReceivePort($ReceivePort.Name)

}
}

$showDate = Get-Date -DisplayHint Date
Write-Host “Completed at $showDate”

If you try to delete an application or a SendPort, you might find that SendPort is used on an EDI Trading-Partner (party).
There is no easy xref in BizTalk to show you which party might be tied to that send port. Usually the naming conventions will help, but not always.
This SQL query gives you the cross-reference.

USE BizTalkMgmtDb

SELECT 
   party.nvcname [PartyName],
   application.nvcDescription [Application],
   sendport.nvcname [SendPort]
FROM bts_sendport sendport
INNER JOIN bts_party_sendport partysendport ON partysendport.nsendportid = sendport.nid
INNER JOIN bts_party party ON partysendport.npartyid = party.nid
INNER JOIN bts_application application on application.nid = sendport.nApplicationID 
ORDER BY party.nvcname,  sendport.nvcname

You can list the cmdlets yourself with this command:

Get-Command | where { $_.ModuleName -like "BizTalk*" } 

But here is a convenient list in text format:

CommandType Name
———– —-
Cmdlet Add-ApplicationReference
Cmdlet Add-ApplicationResource
Cmdlet Deploy-Policy
Cmdlet Disable-ReceiveLocation
Cmdlet Enable-ReceiveLocation
Cmdlet Enlist-Orchestration
Cmdlet Enlist-SendPort
Cmdlet Enlist-SendPortGroup
Cmdlet Export-Application
Cmdlet Export-Bindings
Cmdlet Export-Policy
Cmdlet Get-ApplicationResourceSpec
Cmdlet Get-TrackedMessageInstance
Cmdlet Import-Application
Cmdlet Import-Bindings
Cmdlet Import-Policy
Cmdlet Remove-ApplicationReference
Cmdlet Restart-HostInstance
Cmdlet Resume-ServiceInstance
Cmdlet Set-DefaultApplication
Cmdlet Start-Application
Cmdlet Start-HostInstance
Cmdlet Start-Orchestration
Cmdlet Start-SendPort
Cmdlet Start-SendPortGroup
Cmdlet Stop-Application
Cmdlet Stop-HostInstance
Cmdlet Stop-Orchestration
Cmdlet Stop-SendPort
Cmdlet Stop-SendPortGroup
Cmdlet Terminate-ServiceInstance
Cmdlet Undeploy-Policy
Cmdlet Unenlist-Orchestration
Cmdlet Unenlist-SendPort
Cmdlet Unenlist-SendPortGroup

Reference: http://www.quicklearn.com/blog/2013/07/19/automating-and-managing-biztalk-server-2013-with-powershell/

It is also surprises me that you have to run a commandlet (like Disable-ReceiveLocation myRcvLoc) rather than calling a method on an object (such as myRcvLoc.Disable).

What seems to be missing from the list above are commands to remove items (or to create new ones).

Here are some Powershell scripts that use the ExplorerOM (Object Explorer) to do so:
https://docs.microsoft.com/en-us/biztalk/core/receiveports-biztalk-server-sample

Or to remove send ports, you can still do:

   cscript RemoveSendPort.vbs "My Business Send Port"  

The sample below shows the use of the “ForEach” and the “ForEach-Object”. I think the first method results in cleaner code. The second method using piping and the $_ to represent the anonymous object. Using Powershell ISE (Integrated Scripting Environment), both methods should give you command completion (i.e. if you type in $SendPort., you will get the property and methods names available).

cls
$showDate = Get-Date -DisplayHint Date
Write-Host "Started at $showDate" 

#NOTE: Must be in 32-bit version of Powershell to use this SnapIn
#Add-PSSnapIn -Name BiztalkFactory.PowerShell.Extensions  
#Use the line below if your SQL server is on a different machine as BizTalk - only need to run it once per Powershell "Session" 
#New-PSDrive -Name BizTalk -Root BizTalk:\ -PsProvider BizTalk -Instance MyServer\MyInstance -Database BizTalkMgmtDb


cd "Biztalk:\Applications\MyApp\Send Ports"
$SendPorts = Get-ChildItem
Write-Host "--- Method 1 ---" 
ForEach ($SendPort in $SendPorts) 
{
    Write-Host $SendPort.Name $SendPort.Status 
} 

Write-Host "--- Method 2 ---" 
Get-ChildItem -Path "Biztalk:\Applications\MyApp\Send Ports" | ForEach-Object {
   Write-Host $_.Name  $_.Status 
}

$showDate = Get-Date -DisplayHint Date
Write-Host "Completed at $showDate" 

Example of selection criteria:

cd "Biztalk:\Applications\Echo.BSSR.Surface\Send Ports"
$SendPorts = Get-ChildItem
Write-Host "--- Method 1 ---" 
ForEach ($SendPort in $SendPorts) 
{
    #Write-Host $SendPort.Name $SendPort.Status 
    if (
        ($SendPort.Name.Contains('204') -and $SendPort.Name.ToUpper().Contains('OUT')) -or 
        ($SendPort.Name.Contains('990') -and $SendPort.Name.ToUpper().Contains('IN')) 
       ) 
       {
          Write-Host "Selected " $SendPort.Name 
       }
} 

On my previous blog about using Powershell Extensions with Send/Receive Ports, I didn’t show how to do a loop.

We have a custom Rabbit-MQ adapter that has a few issues. When we restart the host-instances, the adapter may get “stuck” unless we restart the associated receive locations. So, I wrote a Powershell to restart all RabbitMQ receive locations that are currently enabled.

cls 
#
# Restart All RabbitMQ Receive Locations that are currently enabled 
#
$showDate = Get-Date -DisplayHint Date
Write-Host "Started at $showDate" 

##Add-PSSnapIn -Name BiztalkFactory.PowerShell.Extensions  #NOTE: Must be in 32-bit version of Powershellto use this SnapIn
#cd "Biztalk:\Applications\<All Artifacts>\Receive Locations"
cd "Biztalk:\Applications"
$apps = get-childitem 
# $apps = @("Echo.BSSR.Surface.LTLShipmentOut204")   # use this to limit to a list/array of specific Apps 
foreach ($app in $apps) 
{
    Write-Host "APP=$($app.Name)" 
    cd "Biztalk:\Applications\$($app.Name)\Receive Locations" 
    $rcvlocs = get-childitem  
    foreach ($rcvloc in $rcvlocs) 
    {
      if ($rcvloc.Address.Contains("rabbit")) 
          {
              Write-Host "   $($rcvloc.Name) Enabled=$($rcvLoc.Enable) " 
              if ($rcvloc.Enable -eq $TRUE)   #only restart receive locations that are started 
              {
                 Disable-ReceiveLocation $rcvloc.Name 
                 Enable-ReceiveLocation $rcvloc.Name 
                 #Write-Host "       Restarted $($rcvloc.Name)" 
                 Write-Host "       Restarted " 
              }

          }
    }
}

$showDate = Get-Date -DisplayHint Date
Write-Host "Completed at $showDate" 

Error

When trying to deploy a Business Rule Engine Policy in BizTalk Server 2013, I was getting the error:
The database “Server:Instance” associated with the deployment driver does not match database “Server2:Instance2” of the during product configuration.

Solution

You can run a SQL script like the following (on my machine, there was only one row in the table, so no where clause was needed).

Update adm_group set RuleEngineDBServerName = 'VM3-NWALTERS'
select RuleEngineDBServerName, RuleEngineDBName, * from adm_group

where VM3-NWALTERS was my machine name. We used a SYSPREP procedure originally to copy a machine and rename it.

Suppose you want to find if a map is used on some ReceivePort. In BizTalk, one Receive Port can contain multiple maps. The one that is executed is based on the matching target namespace and root element.

select app.nvcName as ApplicationName, 
       rp.nvcName as ReceivePortName,
       it.Name as MapName, 
       it.FullName,   -- you might have same map name in two different assemblies 
       bTwoWay 
  from bts_receiveport rp 
inner join bts_receiveport_transform rpt on rpt.nReceivePortID = rp.nID 
inner join bt_mapspec ms on ms.id = rpt.uidTransformGUID
inner join bts_item it on ms.itemid = it.id
inner join bts_application app on rp.nApplicationID = app.nID 
where it.Name like '%204%'
order by rp.nvcName, it.Name 

Now, here’s how to do the same for SendPorts.

select app.nvcName as ApplicationName, 
       '' as ReceivePortName, 
       sp.nvcName as SendPortName,
       it.Name as MapName, 
       it.FullName,   -- you might have same map name in two different assemblies 
       bTwoWay 
  from bts_sendport sp 
inner join bts_sendport_transform spt on spt.nSendPortID = sp.nID 
inner join bt_mapspec ms on ms.id = spt.uidTransformGUID
inner join bts_item it on ms.itemid = it.id
inner join bts_application app on sp.nApplicationID = app.nID 
where IT.Name like '%204%'
--order by sp.nvcName, it.Name 
order by 1, 2, 3

Now we can get fancy and do both in one query using the “UNION” clause. This requires having the columns match, so I’ve added the dummy SendPort in the receive query, and the dummy ReceivePort in the SendPort Query. The sort has to then sort by numeric columns.


select app.nvcName as ApplicationName, 
       rp.nvcName as ReceivePortName,
       '' as SendPortName, 
       it.Name as MapName, 
       it.FullName,   -- you might have same map name in two different assemblies 
       bTwoWay 
  from bts_receiveport rp 
inner join bts_receiveport_transform rpt on rpt.nReceivePortID = rp.nID 
inner join bt_mapspec ms on ms.id = rpt.uidTransformGUID
inner join bts_item it on ms.itemid = it.id
inner join bts_application app on rp.nApplicationID = app.nID 
where IT.Name like '%204%'
--order by rp.nvcName, it.Name 
union 
select app.nvcName as ApplicationName, 
       '' as ReceivePortName, 
       sp.nvcName as SendPortName,
       it.Name as MapName, 
       it.FullName,   -- you might have same map name in two different assemblies 
       bTwoWay 
  from bts_sendport sp 
inner join bts_sendport_transform spt on spt.nSendPortID = sp.nID 
inner join bt_mapspec ms on ms.id = spt.uidTransformGUID
inner join bts_item it on ms.itemid = it.id
inner join bts_application app on sp.nApplicationID = app.nID 
where IT.Name like '%204%'
--order by sp.nvcName, it.Name 
order by 1, 2, 3