用于自动化BizTalk配置的PowerShell脚本

时间:2017-03-21 14:42:11

标签: .net powershell automation biztalk biztalk-2010

我需要帮助来自动化BizTalk配置,例如发送/接收位置等。 我认为我们可以使用PowerShell脚本实现这一目标。

有什么建议吗?

注意:我们有内部部署基础架构。

2 个答案:

答案 0 :(得分:3)

为了便于管理时配置BizTalk Server,端口,绑定等,您将专注于使用绑定文件。

虽然Powershell可以成为其中的一部分,但Powershell本身并不会驱动配置。

或许你应该关注的是使用Deployment Framework for BizTalk (BTDF)自动化应用部署。

答案 1 :(得分:0)

我的第二个Johns-305的答案 - 如果您正在尝试配置应用程序绑定,您应该利用绑定文件,并且肯定会使用BTDF来管理多个环境。

另一方面,如果您尝试自动设置主机/主机实例/适配器处理程序,那么PowerShell可以成为一种很好的工具,可以简化整个环境中的滚动。您可以使用BizTalk PowerShell提供程序(有关如何使用该here的多个示例,或使用WMI(here的一个很好的示例)来执行此操作。

从第二个链接开始,这是一个使用WMI删除或创建适配器处理程序的示例(它不需要任何特殊的加载项,如BizTalk PowerShell提供程序 - 虽然它是一个很好的插件,但并不总是适用于特定环境):

#############################################################
# This function will delete an existente host handlers 
# in the adapters.
# [direction]: 'Receive','Send'
#############################################################
function DeleteBizTalkAdapterHandler (
    [string]$adapterName,
    [string]$direction,
    [string]$hostName)
{
    try
    {
        if($direction -eq 'Receive')
        {
            [System.Management.ManagementObject]$objHandler = get-wmiobject 'MSBTS_ReceiveHandler' -namespace 'root\MicrosoftBizTalkServer' -filter "HostName='$hostName' AND AdapterName='$adapterName'"
            $objHandler.Delete()
        }
        else
        {
            [System.Management.ManagementObject]$objHandler = get-wmiobject 'MSBTS_SendHandler2' -namespace 'root\MicrosoftBizTalkServer' -filter "HostName='$hostName' AND AdapterName='$adapterName'"
            $objHandler.Delete()
        }

        Write-Host "$direction handler for $adapterName / $hostName was successfully deleted" -Fore DarkGreen
    }
    catch [System.Management.Automation.RuntimeException]
    {
        if ($_.Exception.Message -eq "You cannot call a method on a null-valued expression.")
        {
            Write-Host "$adapterName $direction Handler for $hostName does not exist" -Fore DarkRed
        }
        elseif ($_.Exception.Message.IndexOf("Cannot delete a receive handler that is used by") -ne -1)
        {
            Write-Host "$adapterName $direction Handler for $hostName is in use and can't be deleted." -Fore DarkRed
        }
        elseif ($_.Exception.Message.IndexOf("Cannot delete a send handler that is used by") -ne -1)
        {
            Write-Host "$adapterName $direction Handler for $hostName is in use and can't be deleted." -Fore DarkRed
        }
        elseif ($_.Exception.Message.IndexOf("Cannot delete this object since at least one receive location is associated with it") -ne -1)
        {
            Write-Host "$adapterName $direction Handler for $hostName is in use by at least one receive location and can't be deleted." -Fore DarkRed
        }
        else
        {
            write-Error "$adapterName $direction Handler for $hostName could not be deleted: $_.Exception.ToString()"
        }
    }
}

#############################################################
# This function will create a handler for a specific 
# adapter on the new host, so these get used for processing.
# [direction]: 'Receive','Send'
#############################################################
function CreateBizTalkAdapterHandler(
    [string]$adapterName,
    [string]$direction,
    [string]$hostName,
    [string]$originalDefaulHostName,
    [boolean]$isDefaultHandler,
    [boolean]$removeOriginalHostInstance)
{
    if($direction -eq 'Receive')
    {
        [System.Management.ManagementObject]$objAdapterHandler = ([WmiClass]"root/MicrosoftBizTalkServer:MSBTS_ReceiveHandler").CreateInstance()
        $objAdapterHandler["AdapterName"] = $adapterName
        $objAdapterHandler["HostName"] = $hostName
    }
    else
    {
        [System.Management.ManagementObject]$objAdapterHandler = ([WmiClass]"root/MicrosoftBizTalkServer:MSBTS_SendHandler2").CreateInstance()
        $objAdapterHandler["AdapterName"] = $adapterName
        $objAdapterHandler["HostName"] = $hostName
        $objAdapterHandler["IsDefault"] = $isDefaultHandler
    }

    try
    {
        $putOptions = new-Object System.Management.PutOptions
        $putOptions.Type = [System.Management.PutType]::CreateOnly;

        [Type[]] $targetTypes = New-Object System.Type[] 1
        $targetTypes[0] = $putOptions.GetType()

        $sysMgmtAssemblyName = "System.Management"
        $sysMgmtAssembly = [System.Reflection.Assembly]::LoadWithPartialName($sysMgmtAssemblyName)
        $objAdapterHandlerType = $sysMgmtAssembly.GetType("System.Management.ManagementObject")

        [Reflection.MethodInfo] $methodInfo = $objAdapterHandlerType.GetMethod("Put", $targetTypes)
        $methodInfo.Invoke($objAdapterHandler, $putOptions)

        Write-Host "$adapterName $direction Handler for $hostName was successfully created" -Fore DarkGreen
    }
    catch [System.Management.Automation.RuntimeException]
    {
        if ($_.Exception.Message.Contains("The specified BizTalk Host is already a receive handler for this adapter.") -eq $true)
        {
            Write-Host "$hostName is already a $direction Handler for $adapterName adapter." -Fore DarkRed
        }
        elseif($_.Exception.Message.Contains("The specified BizTalk Host is already a send handler for this adapter.") -eq $true)
        {
            Write-Host "$hostName is already a $direction Handler for $adapterName adapter." -Fore DarkRed
        }
        else {
            write-Error "$adapterName $direction Handler for $hostName could not be created: $_.Exception.ToString()"
        }
    }

    if($removeOriginalHostInstance)
    {
        DeleteBizTalkAdapterHandler $adapterName $direction $originalDefaulHostName
    }
}