SendPortGroups(BizTalk Server 示例)

SendPortGroups 示例演示如何使用 Microsoft.BizTalk.ExplorerOM 管理类枚举和管理发送端口组。

必备条件

  • 必须具有BizTalk Server管理权限才能使用此示例中的管理对象。

  • Windows PowerShell 脚本要求 Windows PowerShell 执行策略允许脚本执行。 有关详细信息,请参阅 about_Execution_Policies

本示例的用途

此示例演示如何使用 Microsoft.BizTalk.ExplorerOM 命名空间中的 BtsCatalogExplorerSendPortGroup 类来管理BizTalk Server环境中的发送端口组。 此示例是用 Microsoft Visual C# 编写的。 本主题中还包含 Windows PowerShell 示例脚本。 本示例将演示以下操作:

  • 使用 BtsCatalogExplorer 类连接到 BizTalk 管理数据库。

  • 创建一个名为“My Send Port Group”的新发送端口组。

  • 枚举发送端口组以显示新创建的发送端口组。

  • 删除新的发送端口组。

    示例中存在其他函数,但不在 Visual C# 版本中执行。 PowerShell 示例脚本演示了一些附加函数。 其他函数演示了以下功能:

  • 将发送端口添加到新创建的发送端口组 (PowerShell 示例) 中所述。

  • 从发送端口组中删除发送端口 (PowerShell 示例) 中所述。

  • 启动发送端口组。

  • 停止发送端口组。

本示例的所在位置

本示例位于以下 SDK 位置中:

<示例路径>\管理员\ExplorerOM\SendPortGroups

下表显示了本示例中的文件及其用途说明:

文件 说明
SendPortGroups.cs 此示例中演示的操作的 Visual C# 源文件。
SendPortGroups.sln、SendPortGroups.csproj、SendPortGroups.suo 示例的解决方案文件和项目文件。

生成并运行本示例

生成示例

  1. 在 Visual Studio 中,打开解决方案文件 SendPortGroups.sln。

  2. 在 解决方案资源管理器中,双击 SendPortGroups.cs 打开示例代码。

  3. root.ConnectionString 函数中找到 CreateSendPortGroup 。 必须更改服务器规范,以正确指向托管 BizTalk 管理数据库的 SQL Server。 还可以使用句点 (.) 连接到本地 SQL Server。 例如:

    root.ConnectionString = "Integrated Security=SSPI;database=BizTalkMgmtDb;server=.";
    
  4. 对以下函数重复步骤 3:

    • EnumerateSendPortGroups

    • DeleteSendPortGroup

  5. 保存 SendPortGroups.cs

  6. 在“main”菜单上,单击“生成”,然后单击“生成解决方案”。

运行本示例的步骤

  1. 打开命令窗口并导航到以下文件夹:

    <示例路径>\管理员\ExplorerOM\SendPortGroups\bin\Debug

  2. SendPortGroups.exe 运行文件。

Windows Powershell 脚本示例

以下Windows PowerShell脚本片段可用于演示 ExplorerOM 类的相同功能:

Function CreateSendPortGroup($Catalog, $strName)
{
   #=== 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 send port group and set its name ===#

   $NewSendPortGroup = $Catalog.AddNewSendPortGroup()
   $NewSendPortGroup.Name = $strName

   #=== Persist new ports to the BizTalk Management database ===#

   Write-Host Adding "`"$($NewSendPortGroup.Name)`"..."
   $catalog.SaveChanges();
   Write-Host "`r`nCreateSendPortGroup() completed."
}

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

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

   #=== Delete this sample's new send ports by name ===#

   $NewSendPortGroup = $Catalog.SendPortGroups[$strName]
   $Catalog.RemoveSendPortGroup($NewSendPortGroup)

   #=== Persist changes to the BizTalk Management database ===#

   Write-Host "Deleting `"$strName`"..."
   $catalog.SaveChanges();
   Write-Host "DeleteSendPortGroup() completed."
}

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

   $ErrorActionPreference="silentlycontinue"
   trap { "Exception encountered During Enumeration:`r`n"; $_; "`r`n"}

   #=== Display all send port groups by name ===#

   $count = 1
   foreach ($group in $catalog.SendPortGroups)
   {
     Write-Host "$count. $($group.Name)"
     $count++
   }

   Write-Host "`r`nEnumerateSendPortGroups() completed."
}

Function PromptForSendPort($Catalog)
{
   #=== Provide a list of the send ports for the user to make a selection ===#

   Write-Host "Here is a list of send ports:`r`n"
   $count = 1
   foreach($sendport in $Catalog.SendPorts)
   {
      Write-Host ($Count). ($Sendport.Name)
      $count++
   }

   Write-Host "`r`nChoose a port to add to the group by ordinal (ex. 1,2, etc): " -nonewline
   $selection = read-host
   $selection = $Catalog.SendPorts[([int]$selection)-1]
   Write-Host $Selection.Name selected.

   return $Selection.Name
}

Function AddSendPortToSendPortGroup($Catalog,$strPortName,$strGroupName)
{

   #=== Add the user's selected send port to the group ===#

   #========================================================#
   #=== Presently, we have to use WMI from PowerShell    ===#
   #=== This is because the methods on the collections   ===#
   #=== are marked internal. So unfortunately the        ===#
   #=== following very straightforward code won't work.  ===#
   #========================================================#

   # $sendportgroup = $Catalog.SendPortGroups[$strName]
   # $sendportgroup.SendPorts.Add($Catalog.SendPorts[$strPortName])
   # $catalog.SaveChanges();

   #============================================================================#
   #=== Get the WMI send port group representation to look up DB and server. ===#
   #=== Expecting only one to match the query in this example.               ===#
   #============================================================================#

   $WQL = "select * from MSBTS_SendPortGroup where Name='" + $strGroupName + "'"
   $groupset = gwmi -query ($WQL) -namespace "root\MicrosoftBizTalkServer"

   #=== Create a new MSBTS_SendPortGroup2SendPort instance to map the port to the group ===#

   $group2port = [WMICLASS]"root\MicrosoftBizTalkServer:MSBTS_SendPortGroup2SendPort"
   $newPortEntry = [WMI] $group2port.psbase.CreateInstance()
   $newPortEntry.MgmtDbServerOverride = $groupset.MgmtDbServerOverride
   $newPortEntry.MgmtDbNameOverride = $groupset.MgmtDbNameOverride
   $newPortEntry.SendPortGroupName = $groupset.Name
   $newPortEntry.SendPortName = $strPortName
   Write-Host "Adding $strPortName to $($groupset.Name)"

   #=== Persist changes to the BizTalk Management database ===#

   #=== POWERSHELL V1 BUG WORKAROUND =============================#
   #=== First method call on a non-cimv2 WMI object can fail.  ===#
   #=== The workaround in PowerShell V1 is to call GetType()   ===#
   #=== as the first method.                                   ===#
   $ErrorActionPreference="silentlycontinue"
   trap {}
   $newPortEntry.GetType()
   #=== POWERSHELL V1 BUG WORKAROUND =============================#

   $ErrorActionPreference="continue"
   [void] $newPortEntry.Put()

   #=== Since we used WMI to update the BizTalk Management database, ===#
   #=== refresh our BtsExplorerCatalog to have an updated DB view.   ===#

   $Catalog.Refresh()
   Write-Host "AddSendPortToSendPortGroup() completed."
}

Function DeleteSendPortFromSendPortGroup($Catalog, $strPortName, $strGroupName)
{
   #========================================================#
   #=== Presently, we have to use WMI from PowerShell    ===#
   #=== This is because the methods on the collections   ===#
   #=== are marked internal. So unfortunately the        ===#
   #=== following very straightforward code won't work.  ===#
   #========================================================#

   # $sendportgroup = $Catalog.SendPortGroups[$strGroupName]
   # $sendportgroup.SendPorts.Remove($Catalog.SendPorts[$strPortName])
   # $catalog.SaveChanges();

   #============================================================================#
   #=== Get the WMI send port to group instance and delete it.               ===#
   #=== Expecting only one to match the query in this example.               ===#
   #============================================================================#

   $WQL = "select * from MSBTS_SendPortGroup2SendPort where " +
          "SendPortName='" + $strPortName + "' and " +
          "SendPortGroupName='" + $strGroupName + "'"

   $groupset = gwmi -query ($WQL) -namespace "root\MicrosoftBizTalkServer"

   write-host "Removing $strPortName from $strGroupName..."
   $groupset.Delete();

   #=== Since we used WMI to update the BizTalk Management database, ===#
   #=== refresh our BtsExplorerCatalog to have an updated DB view.   ===#

   $Catalog.Refresh()
   Write-Host "DeleteSendPortFromSendPortGroup() completed."
}

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

#=== 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 = "DATABASE=BizTalkMgmtDb;Integrated Security=SSPI;SERVER=."

#=== Exercise the CreateSendPortGroup function to create a new send port group ===#

$SendPortGroupName = "PowerShell Sample Send Port Group"

Write-Host "`r`n============================="
Write-Host "=== CreateSendPortGroup() ==="
Write-Host "=============================`r`n"
CreateSendPortGroup $Catalog $SendPortGroupName

#=== Enumerate all send port groups to view the new one ===#

Write-Host "`r`n================================="
Write-Host "=== EnumerateSendPortGroups() ==="
Write-Host "=================================`r`n"
EnumerateSendPortGroups $Catalog

#=== Add a send port to the group ===#

Write-Host "`r`n===================================="
Write-Host "=== AddSendPortToSendPortGroup() ==="
Write-Host "====================================`r`n"
$SendPortName = PromptForSendPort($Catalog)
AddSendPortToSendPortGroup $Catalog $SendPortName $SendPortGroupName

#=== Remove the send port from the group ===#

Write-Host "`r`n========================================="
Write-Host "=== DeleteSendPortFromSendPortGroup() ==="
Write-Host "=========================================`r`n"
DeleteSendPortFromSendPortGroup $Catalog $SendPortName $SendPortGroupName

#=== Delete the group ===#

Write-Host "`r`n============================="
Write-Host "=== DeleteSendPortGroup() ==="
Write-Host "=============================`r`n"
DeleteSendPortGroup $Catalog $SendPortGroupName

Write-Host

下面是运行Windows PowerShell脚本示例的预期输出示例。

PS C:\> .\sendportgroups.ps1

=============================
=== CreateSendPortGroup() ===
=============================

Adding "PowerShell Sample Send Port Group"...

CreateSendPortGroup() completed.

=================================
=== EnumerateSendPortGroups() ===
=================================

1. PowerShell Sample Send Port Group

EnumerateSendPortGroups() completed.

====================================
=== AddSendPortToSendPortGroup() ===
====================================

Here is a list of send ports:

1 . ResendPort
2 . HelloWorldSendPort
3 . ToCustomerSendPort
4 . CBRUSSendPort
5 . CBRCANSendPort
6 . SendportCANOrders

Choose a port to add to the group by ordinal (ex. 1,2, etc): 2
HelloWorldSendPort selected.
Adding HelloWorldSendPort to PowerShell Sample Send Port Group
AddSendPortToSendPortGroup() completed.

=========================================
=== DeleteSendPortFromSendPortGroup() ===
=========================================

Removing HelloWorldSendPort from PowerShell Sample Send Port Group...
DeleteSendPortFromSendPortGroup() completed.

=============================
=== DeleteSendPortGroup() ===
=============================

Deleting "PowerShell Sample Send Port Group"...
DeleteSendPortGroup() completed.

另请参阅

Admin-ExplorerOM(BizTalk Server 示例文件夹)