PowerShell script to Start or Stop All BizTalk Host Instances

The PowerShell script provided below can be used to start and stop all BizTalk host instances. You can apply a filter condition using the ‘-notlike’ PowerShell command to exclude specific host instances by their names. The script includes two functions, one for starting the host instance and the other for stopping it. You can use these functions as per your requirement.

# Import external assembly and create a new object

[void] [System.reflection.Assembly]::LoadWithPartialName(“Microsoft.BizTalk.ExplorerOM”)

$Catalog = New-Object Microsoft.BizTalk.ExplorerOM.BtsCatalogExplorer

 

 #BizTalk Config

$Catalog.ConnectionString = “SERVER=JEETU;DATABASE=BizTalkMgmtDb;Integrated Security=SSPI”  #connectionstring for the BizTalkMgmtDb

$Hostinst = “” # Define variable for host instances

$resultToWrite=“”

$OutResultLog = ‘E:\Biztalk\BizTalkLive\BizTalkPowerShellService\StartStopHostInstnace\logStartStopHostInstance.txt’

#$ServerName=”” #Define running server name

 

#Function to start host instances with filter condition

function StartHostInstance()

{

    $resultToWrite=‘Process has started to start host instance on date : ‘+(Get-Date).ToString()

    Add-Content $OutResultLog $resultToWrite

    $resultToWrite=“”

 

    #get all in-process host instances

    [ARRAY]$hostInstances = Get-WmiObject MSBTS_HostInstance -namespace “root\MicrosoftBizTalkServer” -Filter “(HostType = 1 )” # and RunningServer = ‘$ServerName’ –If you want to add server in filter co

 

   foreach ($Hosts in $catalog.Hosts )

   {

       foreach($Hostinstance in $Hosts ){

       try

       {

            $Hostinst = $Hostinstance.Name

           

             foreach($hostinst1 in $hostInstances | WHERE {($_.HostName -eq $Hostinst) -and ($_.HostName -notlike ‘BizTalkLive’)}) {

              

                if  ($hostinst1.ServiceState -eq 1)   # 1 = host instance  is stoped

                {

                    $hostinst1.Start()

                    #Wait for few seconds

                    Start-Sleep -s 2

 

                    $resultToWrite=$resultToWrite‘Started host instance: ‘+$Hostinst‘ at datetime :’+(Get-Date).ToString()

                    Add-Content $OutResultLog $resultToWrite

                    $resultToWrite=“”

                }

            }

        }

        catch [Exception]

        {

            $resultToWrite=$resultToWrite‘Getting Exception: ‘$_.Exception.Message +‘ to Start host instance: ‘+$Hostinst‘ at datetime: ‘+(Get-Date).ToString()+“`r`n”

            Add-Content $OutResultLog $resultToWrite

            $resultToWrite=“”

        }

 

       }

}

$resultToWrite=$resultToWrite‘Process end to start host instance on datetime: ‘+(Get-Date).ToString() +“`r`n”

Add-Content $OutResultLog $resultToWrite #logged all details in log file

[void]$Catalog.Refresh()

}

 

#Function to stop host instances with filter condition

function StopHostInstance()

{

    $resultToWrite=‘Process has started to stop host instance on date : ‘+(Get-Date).ToString()

    Add-Content $OutResultLog $resultToWrite

    $resultToWrite=“”

 

    #get all in-process host instances

    [ARRAY]$hostInstances = Get-WmiObject MSBTS_HostInstance -namespace “root\MicrosoftBizTalkServer” -Filter “(HostType = 1 )” # and RunningServer = ‘$ServerName’ –If you want to add server in filter co

 

   foreach ($Hosts in $catalog.Hosts )

   {

       foreach($Hostinstance in $Hosts ){

       try

       {

            $Hostinst = $Hostinstance.Name

           

             foreach($hostinst1 in $hostInstances | WHERE {($_.HostName -eq $Hostinst) -and ($_.HostName -notlike ‘BizTalkLive’)}) {

              

                if  ($hostinst1.ServiceState -eq 4)   # 4 = host instance is running

                {

                    $hostinst1.Stop()

                    #Wait for few seconds

                    Start-Sleep -s 2

 

                    $resultToWrite=$resultToWrite‘Stopped host instance: ‘+$Hostinst‘ at datetime :’+(Get-Date).ToString()

                    Add-Content $OutResultLog $resultToWrite

                    $resultToWrite=“”

                }

            }

        }

        catch [Exception]

        {

            $resultToWrite=$resultToWrite‘Getting Exception: ‘$_.Exception.Message +‘ to Stop host instance: ‘+$Hostinst‘ at datetime: ‘+(Get-Date).ToString()+“`r`n”

            Add-Content $OutResultLog $resultToWrite

            $resultToWrite=“”

        }

 

       }

}

$resultToWrite=$resultToWrite‘Process end to stop host instance on datetime: ‘+(Get-Date).ToString() +“`r`n”

Add-Content $OutResultLog $resultToWrite #logged all details in log file

[void]$Catalog.Refresh()

}

 

 #call function to start host instances

   StartHostInstance

 

 #call function to start host instances

   StopHostInstance

 

Post a comment

Leave a Comment

Scroll to Top