locked
Power Shell Script to make Send And Receive Handlers as Default Handlers. RRS feed

  • Question

  • Hi,

    I have created a Host and Host Instances using Power shell script and now i want to create another script to make all the newly added handlers as Default handlers for Send and receive.

    I have created These Host and Host Instances with Handlers for ReceiveHost, SendHost, OrchestrationHost, TrackingHost.

    i have followed the below links, but it is not working 

    https://sandroaspbiztalkblog.wordpress.com/2016/04/05/biztalk-devops-how-to-configure-receive-handlers-from-existing-receive-locations-with-powershell/

    is there any script for default handlers?

    Thanks

    kasi

    Monday, July 11, 2016 12:22 PM

Answers

  • To change the Host settings for the existing host make use of below script,

    1) You cannot set Receive Handler as Default even in BizTalk Console.

    2) Its boolean you have to pass it true.

    #############################################################
    # This function will update an existent BizTalk Host
    # HostType: Invalid: 0, In-process:	1, Isolated: 2
    #############################################################
    function UpdateBizTalkHost ( 
        [string]$hostName, 
        [int]$hostType, 
        [string]$ntGroupName, 
        [bool]$authTrusted, 
        [bool]$isTrackingHost, 
        [bool]$is32BitOnly,
    	[bool]$isDefaultHost)
    {
        try
        {
            [System.Management.ManagementObject]$objHostSetting = ([WmiClass]"root/MicrosoftBizTalkServer:MSBTS_HostSetting").CreateInstance()
    
            $objHostSetting["Name"] = $hostName
            $objHostSetting["HostType"] = $hostType
            $objHostSetting["NTGroupName"] = $ntGroupName
            $objHostSetting["AuthTrusted"] = $authTrusted
            $objHostSetting["IsHost32BitOnly"] = $is32BitOnly 
            $objHostSetting["HostTracking"] = $isTrackingHost
    		$objHostSetting["IsDefault"] = $isDefaultHost
    
            $putOptions = new-Object System.Management.PutOptions
            $putOptions.Type = [System.Management.PutType]::UpdateOnly; # This tells WMI it's an update.
    
            [Type[]] $targetTypes = New-Object System.Type[] 1
            $targetTypes[0] = $putOptions.GetType()
    
            $sysMgmtAssemblyName = "System.Management"
            $sysMgmtAssembly = [System.Reflection.Assembly]::LoadWithPartialName($sysMgmtAssemblyName)
            $objHostSettingType = $sysMgmtAssembly.GetType("System.Management.ManagementObject")
    
            [Reflection.MethodInfo] $methodInfo = $objHostSettingType.GetMethod("Put", $targetTypes)
            $methodInfo.Invoke($objHostSetting, $putOptions)
    		
    		Write-Host "Host $hostName was successfully updated" -Fore DarkGreen
        }
        catch [System.Management.Automation.RuntimeException]
        {
            write-Error "$hostName host could not be updated: $_.Exception.ToString()"
        }
    }

    Rachit Sikroria (Microsoft Azure MVP)



    Monday, July 11, 2016 1:06 PM
    Moderator
  • Hi Kasi

    $objAdapterHandler["IsDefault"] = $isDefaultHandler  :Can i give "1" for "IsDefault" ? 

    So, this is a boolean param - pass true as the parameter to the function.


    Thanks Arindam

    • Marked as answer by Kasireddy.G Monday, July 11, 2016 1:11 PM
    Monday, July 11, 2016 1:05 PM
    Moderator

All replies

  • Hi kasi,

    You can use the script available here: https://gallery.technet.microsoft.com/PowerShell-to-Configure-0cee83e8.

    You would be dealing with MSBTS_SendHandler2.IsDefault Property (WMI), This property indicates whether the send handler is the default one for this adapter type.

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

    To change the Host settings for the existing host make use of below script:

    #############################################################
    # This function will update an existent BizTalk Host
    # HostType: Invalid: 0, In-process:	1, Isolated: 2
    #############################################################
    function UpdateBizTalkHost ( 
        [string]$hostName, 
        [int]$hostType, 
        [string]$ntGroupName, 
        [bool]$authTrusted, 
        [bool]$isTrackingHost, 
        [bool]$is32BitOnly,
    	[bool]$isDefaultHost)
    {
        try
        {
            [System.Management.ManagementObject]$objHostSetting = ([WmiClass]"root/MicrosoftBizTalkServer:MSBTS_HostSetting").CreateInstance()
    
            $objHostSetting["Name"] = $hostName
            $objHostSetting["HostType"] = $hostType
            $objHostSetting["NTGroupName"] = $ntGroupName
            $objHostSetting["AuthTrusted"] = $authTrusted
            $objHostSetting["IsHost32BitOnly"] = $is32BitOnly 
            $objHostSetting["HostTracking"] = $isTrackingHost
    		$objHostSetting["IsDefault"] = $isDefaultHost
    
            $putOptions = new-Object System.Management.PutOptions
            $putOptions.Type = [System.Management.PutType]::UpdateOnly; # This tells WMI it's an update.
    
            [Type[]] $targetTypes = New-Object System.Type[] 1
            $targetTypes[0] = $putOptions.GetType()
    
            $sysMgmtAssemblyName = "System.Management"
            $sysMgmtAssembly = [System.Reflection.Assembly]::LoadWithPartialName($sysMgmtAssemblyName)
            $objHostSettingType = $sysMgmtAssembly.GetType("System.Management.ManagementObject")
    
            [Reflection.MethodInfo] $methodInfo = $objHostSettingType.GetMethod("Put", $targetTypes)
            $methodInfo.Invoke($objHostSetting, $putOptions)
    		
    		Write-Host "Host $hostName was successfully updated" -Fore DarkGreen
        }
        catch [System.Management.Automation.RuntimeException]
        {
            write-Error "$hostName host could not be updated: $_.Exception.ToString()"
        }
    }



    Rachit Sikroria (Microsoft Azure MVP)

    Monday, July 11, 2016 12:30 PM
    Moderator
  • Hi Kasi

    You can set the IsDefault property on the MSBTS_SendHandler2 WMI object. Sandro has a sample that shows how to do it-

    #############################################################
    # 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()"
    		}
        }

    The code is downloadable here -

    https://gallery.technet.microsoft.com/PowerShell-to-Configure-01c98d15


    Thanks Arindam



    Monday, July 11, 2016 12:32 PM
    Moderator
  • Hi Arindam and Rachit,

    Thanks for quick reply,

        $objAdapterHandler["File"] = $adapterName
       $objAdapterHandler["SendHost"] = $hostName
       $objAdapterHandler["IsDefault"] = $isDefaultHandler  :Can i give "1" for "IsDefault" ?                                    is it possible to make receive handlers default ?

    Thanks

    Kasi


    Monday, July 11, 2016 12:48 PM
  • and i have created Host and Host Instances successfully using Power shell script.

    now i want to make the handlers default.

    Thanks

    kasi

    Monday, July 11, 2016 12:59 PM
  • Hi Kasi

    $objAdapterHandler["IsDefault"] = $isDefaultHandler  :Can i give "1" for "IsDefault" ? 

    So, this is a boolean param - pass true as the parameter to the function.


    Thanks Arindam

    • Marked as answer by Kasireddy.G Monday, July 11, 2016 1:11 PM
    Monday, July 11, 2016 1:05 PM
    Moderator
  • To change the Host settings for the existing host make use of below script,

    1) You cannot set Receive Handler as Default even in BizTalk Console.

    2) Its boolean you have to pass it true.

    #############################################################
    # This function will update an existent BizTalk Host
    # HostType: Invalid: 0, In-process:	1, Isolated: 2
    #############################################################
    function UpdateBizTalkHost ( 
        [string]$hostName, 
        [int]$hostType, 
        [string]$ntGroupName, 
        [bool]$authTrusted, 
        [bool]$isTrackingHost, 
        [bool]$is32BitOnly,
    	[bool]$isDefaultHost)
    {
        try
        {
            [System.Management.ManagementObject]$objHostSetting = ([WmiClass]"root/MicrosoftBizTalkServer:MSBTS_HostSetting").CreateInstance()
    
            $objHostSetting["Name"] = $hostName
            $objHostSetting["HostType"] = $hostType
            $objHostSetting["NTGroupName"] = $ntGroupName
            $objHostSetting["AuthTrusted"] = $authTrusted
            $objHostSetting["IsHost32BitOnly"] = $is32BitOnly 
            $objHostSetting["HostTracking"] = $isTrackingHost
    		$objHostSetting["IsDefault"] = $isDefaultHost
    
            $putOptions = new-Object System.Management.PutOptions
            $putOptions.Type = [System.Management.PutType]::UpdateOnly; # This tells WMI it's an update.
    
            [Type[]] $targetTypes = New-Object System.Type[] 1
            $targetTypes[0] = $putOptions.GetType()
    
            $sysMgmtAssemblyName = "System.Management"
            $sysMgmtAssembly = [System.Reflection.Assembly]::LoadWithPartialName($sysMgmtAssemblyName)
            $objHostSettingType = $sysMgmtAssembly.GetType("System.Management.ManagementObject")
    
            [Reflection.MethodInfo] $methodInfo = $objHostSettingType.GetMethod("Put", $targetTypes)
            $methodInfo.Invoke($objHostSetting, $putOptions)
    		
    		Write-Host "Host $hostName was successfully updated" -Fore DarkGreen
        }
        catch [System.Management.Automation.RuntimeException]
        {
            write-Error "$hostName host could not be updated: $_.Exception.ToString()"
        }
    }

    Rachit Sikroria (Microsoft Azure MVP)



    Monday, July 11, 2016 1:06 PM
    Moderator
  • is it possible to make receive handlers default ?

    No.  There is no concept of 'default' for Receive Handlers.

    FYI, as of BizTalk Server 2013, you can now specify the Send Handler for Dynamic Ports.

    This means the Default Handler is no longer that significant since you can and should specify a specific Send Host for all operations.

    Monday, July 11, 2016 1:08 PM
    Moderator
  • Thank you guys !! 

    regards

    Kasi

    Monday, July 11, 2016 1:11 PM
  • Hi Arindam,

    below error i'm getting when i try to run the script.

    New-Item : A parameter cannot be found that matches parameter name 'IsDefault'.

    +  New-Item  -Path .\SendHost -HostName SendHost -Direction Send -IsDefault true

    i tried with isDefaultHandler too.

    getting same error.

    is it a correct command or not?

    $FileSendHost = Get-ChildItem | Where-Object{$_.Name -eq "FILE Send Handler (SendHost)"}
    if($FileSendHost.Name -eq $null)

     New-Item  -Path .\SendHost -HostName SendHost -Direction Send -IsDefault true
    }

    Any suggestions please..

    Thanks

    kasi

    Tuesday, July 12, 2016 5:05 AM
  • Hi Kasi

    Can you share the function signature that you are calling?

    To pass a boolean in your script you have to use  $true,

    so -

    New-Item  -Path .\SendHost -HostName SendHost -Direction Send -IsDefault $true


    Thanks Arindam

    Tuesday, July 12, 2016 5:23 AM
    Moderator
  • Hi Arindam,

    Here is link, which i used for creating host and host instances

    http://social.technet.microsoft.com/wiki/contents/articles/32456.powershell-script-for-creating-biztalk-hostshost-instancesadapter-handlers-and-custom-event-log.aspx

    thanks

    kasi

    Tuesday, July 12, 2016 5:39 AM
  • Hi Kasi

    The scripts in the link does not have the IsDefault parameter. You either have to modify the script from your link or use something like this-

    https://gallery.technet.microsoft.com/PowerShell-to-Configure-01c98d15


    Thanks Arindam


    Tuesday, July 12, 2016 5:46 AM
    Moderator
  • Hi ,

    Where to add the [boolean]$isDefaultHandler parameter to the old script? which i'm already using .

    Thanks

    Kasi

    Tuesday, July 12, 2016 6:14 AM
  • Hi Kasi,

    Try using the SetHandlers feature from https://biztalkapplicationdeployment.codeplex.com/ 


    Pi_xel_xar

    Blog: My Blog

    BizTalkApplicationDeploymentTool: BizTalk Application Deployment Tool

    LinkedIn: LinkedIn

    Tuesday, July 12, 2016 6:15 AM
    Answerer
  • Hi Kasi

    Why not use the script that is already built for this -

    https://gallery.technet.microsoft.com/PowerShell-to-Configure-01c98d15

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


    Thanks Arindam

    Tuesday, July 12, 2016 6:24 AM
    Moderator
  • Hi Arindam,

    i want to define the IsDefault parameter , can you please help me on this , how to add it? like "Direction"

    i'm using existing script, so i have to use it.

    Thanks

    kasi

    Wednesday, July 13, 2016 6:20 AM