Two ways to loop through BizTalk Sendports using Powershell Extensions (ForEach & ForEach-Object)

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).

<pre>
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" 

</pre>

Example of selection criteria:

<pre>
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 
       }
} 

</pre>

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

Uncategorized  

Leave a Reply