共用方式為


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 管理資料庫。

  • 建立名為「我的傳送埠群組」的新傳送埠群組。

  • 列舉傳送埠群組以顯示新建立的傳送埠群組。

  • 刪除新的傳送埠群組。

    範例中有其他函式,但不會在 Visual C# 版本中執行。 PowerShell 範例腳本會示範一些額外的函式。 其他函式示範下列功能:

  • 將傳送埠新增至新建立的傳送埠群組, (PowerShell 範例中涵蓋) 。

  • 從 PowerShell 範例) 涵蓋的傳送埠群組中刪除傳送埠 (。

  • 啟動傳送埠群組。

  • 停止傳送埠群組。

此範例的位置

這個範例位於下列 SDK 位置:

<範例路徑>\管理員\ExplorerOM\SendPortGroups

下表顯示此範例中的檔案,並描述其用途。

檔案 Description
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. 在主功能表上,按一下 [ 建置],然後按一下 [ 建置方案]。

執行此範例

  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 Samples 資料夾)