user401
user401

Reputation: 63

Create Receive Location and Send Port in Existing BizTalk Application using Powershell

I am needing to create receive locations and send ports in an already existing BizTalk application using Powershell. I have only seen some documentation on how to create an application but not to call upon one. Any suggestions would be beneficial. There are some things that are commented out, and that is because I cannot disclose that information. I added at the last part on what I have learned of how to create an application, but that is not something that I want for my script. The program below is what I have so far:

#===Create a receive port and location function===#

Function CreateRPandRL ()
{
    #Creating Receive Port
    $myReceivePort = $catalog.AddNewReceivePort($false)
    $myReceivePort.Name = "My Receive Port"

    #Creating Receive Location
    $myReceiveLocation = $myReceivePort.AddNewReceiveLocation()

    foreach ($handler in $catalog.ReceiveHandlers)
    {
        if ($handler.TransportType.Name -eq "FILE")
        {
            $myReceiveLocation.ReceiveHandler = $handler
            break
        }
    }

    #Associate a transport protocol and file location with receive location
    $myReceiveLocation.TransportType = $catalog.ProtocolTypes["FILE"]
    $myReceiveLocation.Address = #pick-up file location

    #Assign the first receive pipeline found to process the message
    foreach ($pipeline in $catalog.Pipelines)
    {
        if ($pipeline.Type -eq [Microsoft.BizTalk.ExplorerOM.PipelineType] "File_Receive")
        {
            $myReceiveLocation.ReceivePipeline = $pipeline
            break
        }

    #Enable the receive location
    $myReceiveLocation.Enable = $true
    }

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

Function CreateSendPorts($Catalog)
{
    #=== Register a trap handler to discard changes on exceptions ===#

    $ErrorActionPreference="silentlycontinue"
    trap { "Exception encountered:`r`n"; $_; "`r`nDiscarding Changes.`r`n";$Catalog.DiscardChanges();exit; }

    #=== create a new static one-way send port using FILE transport ===#

    $mySendPort = $Catalog.AddNewSendPort($false,$false)
    $mySendPort.Name = "My Send Port"
    $mySendPort.PrimaryTransport.TransportType = $catalog.ProtocolTypes["FILE"]
    $mySendPort.PrimaryTransport.Address = #drop-off file location
    $mySendPort.SendPipeline = $Catalog.Pipelines["Microsoft.BizTalk.DefaultPipelines.EdiSend"]

    #=== Persist new ports to BizTalk configuration database ===#

    Write-Host "Adding $mySendPort.Name..."
    $catalog.SaveChanges();
    Write-Host "`r`n $mySendPort.Name has been created."

    #=== specify filters for content-based routing ===#
    Write-Host $mySendPort.Name: Adding a filter
    $mySendPort.Filter = "<Filter><Group>" +
                       "<Statement Property='EDI.ISA06' Operator='0' Value='9999999999'/>" +
                       "<Statement Property='EDI.ST01' Operator='0' Value='999'/>" +
                       "<Statement Property='EDI.IsSystemGeneratedAck' Operator='0' Value='true'/>" +
                       "<Statement Property='BTS.ReceivePortName' Operator='0' Value= $myReceivePort.Name/>" +
                       "</Group></Filter>"
    #=== Persist all changes to BizTalk configuration database ===#

    Write-Host $mySendPort.Name + ": Saving changes"
    $catalog.SaveChanges();
    Write-Host "`r`nFilters for $mySendPort.Name created"

    #===========Changing Send Port status===========#

    #Register a trap handler to discard changes on exceptions
    $ErrorActionPreference="silentlycontinue"
    trap { "Exception encountered:`r`n"; $_; "`r`nDiscarding Changes.`r`n";$Catalog.DiscardChanges();exit; }

    #start the send port to begin processing messages
    $mySendPort.Status = [Microsoft.BizTalk.ExplorerOM.PortStatus] "Started"
    Write-Host "Changing" + $mySendPort.Name + "status to ($mySendPort.Status)..."
    $catalog.SaveChanges()
    Write-Host "Complete."


}
#===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
$Catalog.ConnectionString = "SERVER = #server_address; DATABASE=BizTalkMgmtDB; Integrated Security=SSPI"

#Implementing functions
CreateRPandRL
CreateSendPorts

Start-Sleep -Seconds 60


<#
#===BizTalk Application===#
$app = $Catalog.AddNewApplication()
$app.Name = "TestingPowershellScript"
$app.CreateRPandRL()
$app.CreateSendPorts()
#>

Upvotes: 0

Views: 667

Answers (2)

DTRT
DTRT

Reputation: 11040

My advice, don't even try this. Most of the useful settings for Adapters are not exposed by any API so this will get you maybe half way at most.

Instead, script the import of a binding file which does support all settings for all Adapters.

Upvotes: 1

Daniel Morritt
Daniel Morritt

Reputation: 1817

Hoo boy, this takes me back a few years, I'm glad I'm not the only one to struggle with this. You want to leave that alone and switch to the BizTalk PowerShell Extensions (information on this is sketchy), they are sooooooo much easier to work with in PowerShell.

I cobbled this together from some scripts I used, and left out some of the fancy stuff, but what you want is basically:

$InitializeDefaultBTSDrive = $false
Import-Module "$env:BTSINSTALLPATH\SDK\Utilities\PowerShell\BizTalkFactory.PowerShell.Extensions.dll" -WarningAction Ignore

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

This opens up a whole world of goodies, because it's loaded as a PSDrive, you can navigate round it, create things, delete things, use it all as native as any other drive/filesystem, such as:

Get-ChildItem "BizTalk:\All Artifacts\Receive Locations"
Get-ChildItem "BizTalk:\All Artifacts\Receive Locations" | Disable-ReceiveLocation

Get-ChildItem "BizTalk:\Platform Settings\Host Instances" | Stop-HostInstance
Get-ChildItem "BizTalk:\Platform Settings\Host Instances" | Where-Object { $_.IsDisabled -eq $false  } | Start-HostInstance

Get-ChildItem "BizTalk:\All Artifacts\Receive Locations" | Enable-ReceiveLocation

Get-ChildItem -Path "BizTalk:\Health and Activity\Service Instances"

There's so much more than the above, and none of this is what you really asked for, what you actually want is:

Import-Bindings -Path "BizTalk:" -Source $bindings

Where $bindings is your XML bindings file.

Upvotes: 2

Related Questions