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" 

One of the pains of the BizTalk Deployment Framework (BTDF) is that frequently, you build a PortBindingMaster file, then you change your port names, or make other changes, and have to rebuild it manually.

The manual process is to export the binding file, then carefully go through the file, making the changes to the BTDF variables (which have the pattern ${variableName}. You define those variables in the SettingFileGenerator.xml file (which is opened and edited as an Excel spreadsheet).

So I built a Powershell that would do the work for me. It could be more complicated, but for now, I usually only have to change my RabbitMQ host name, the SQL server and instance name, and sometimes a filename.

$appName = "MyApp" 
$exportFilename = "d:\GitMyApp_Dev\MyApp\MyApp.Deployment\PortBindingsMaster.xml"

cd "Biztalk:\Applications"
$app = get-item $appName 
export-bindings $app $exportFilename

$binding = get-content $exportFilename 
#Note: $ sign must be escaped in the replace statements 
$binding = $binding.replace("rabbit@localhost","rabbit@`${RabbitMQHostName}") 
#Note: if file has different case of string - the match will not happen 
$binding = $binding.replace("mssql://SQLEnv.dev.domain.net/db01/","mssql://`${DB01Server}/`${DB01Instance}") 
$binding = $binding.replace("mssql://.//","mssql://`${DB01Server}/`${DB01Instance}")
$binding = $binding.replace("c:\Integrations\MyApp\BizTalk2010File","`${FilePathFor2010}") 
Set-Content -Path $exportFilename -Value $binding 

 

Assumes you have installed the Powershell Extensions for BizTalk and have the:
“Add-PSSnapin BiztalkFactory.Powershell.Extensions” set up in your startup script.

It could be fancier, for example it could use RegEx to find the pattern of the SQL server name, but for now, it’s saving me a lot of time as it is.

I’m starting to write a script to build my BizTalk Deployment Framework Binding (BTDF) files. I’m planning to substite the few things that are unique with the BTDF variables. So the first step is to automate the export of the binding file.

$appName = "Echo.BSSR.FrontEnd.LTLShipmentOut" 
$bindingsDirectory = "c:\Users\NWalters\AppData\Local\BizTalk\Binding Files"
$exportFilename = "$bindingsDirectory.\$appName.xml"

cd "Biztalk:\Applications"
$app = get-item $appName 
export-bindings $app $exportFilename

 

Assumes you have installed the Powershell Extensions for BizTalk and have the:
“Add-PSSnapin BiztalkFactory.Powershell.Extensions” set up in your startup script.

When you have many BizTalk host instances and you want to do a “Debug Attach” to one specific one (for example to debug a pipeline component), it really helps to know the process ID (else you might have to attach to many host instances).

The Powershell code below conveniently lists the process ids along with the host name.
Credit for this code goes to Randal van Sputeren’s blog:

## https://biztalkmessages.wordpress.com/2010/01/05/retrieve-the-btsntsvc-exe-pid-with-powershell/
## Requires the BizTalk add-ons for Powershell 
## Alternative is: TASKLIST /FI “USERNAME ne NT AUTHORITYSYSTEM” /FI “IMAGENAME eq BTSNTSvc.exe” /SVC

function GetHostPID
{
   Get-ChildItem -Path 'Biztalk:\Platform Settings\Host Instances' | ForEach-Object {

   if ($_.HostType -ne 'Isolated')
      {
          [string]$a = (Get-WmiObject Win32_Process -filter "CommandLine Like '%$($_.HostName)%'").ProcessId
         $_ | Add-Member -MemberType NoteProperty -Name PID -Value $a
        Write-Output $_
 }
   } | Format-Table PID, Name, HostName, NTGroupName, RunningServer, HostType, ServiceState
}

GetHostPID   #call the above function 

Example Output

Debug Attach

In Visual Studio, you can attach your code to one or many BizTalk Host Instances in order to debug a helper routine or pipeline component. If you have many host instances, it’s difficult to know which one to pick. The screen below doesn’t list the Host Instance Names, only the Process IDs. So you need the Powershell command above, to xref the Host Instance to the Process ID.