Enable All BizTalk Receive Locations Except Filter Condition
Before executing the script, please complete the following tasks:
a. Set the server name in ConnectionString.
b. Set the value for ($FilterCondition), which in my case is “DoNotEnable”.
c. Set the path value for $OutResultLog to generate a log text file.
# 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=.;DATABASE=BizTalkMgmtDb;Integrated Security=SSPI”  #connectionstring for the BizTalkMgmtDb
$rcvLocation = “” # Define variable for receive location
$BizAppName =“”
$FilterCondition=“DoNotEnable*”;
$resultToWrite =“”
$OutResultLog = ‘D:\ BizTalkPowerShellService\EnableReceiveLocation\logEnableReceiveLocation.txt’
Â
#Function to retrieve the status of the specify receive location
function CheckReceiveLocatonStatusThenEnable(){
$resultToWrite=‘Process has started to enable receive location on date : ‘+(Get-Date).ToString()
Add-Content $OutResultLog $resultToWrite
$resultToWrite=“”
   foreach ($receivePort in $catalog.ReceivePorts)
   {
   try
    {
       foreach($receiveLoc in $receivePort.ReceiveLocations  | Where {($_.Enable -eq $false)-and ($_.Name -notlike $FilterCondition)})
       {
          try
          {
            $rcvLocation = $receiveLoc.Name
            $BizAppName = $receivePort.Application.Name
            if($receiveLoc.Enable -eq $false)
              {
                #Enable receive location
                enableReceiveLocation
Â
                #Wait for few seconds
                Start-Sleep -s 5
              }
          }
          catch [Exception]
          {
          $resultToWrite=$resultToWrite+ ‘Getting Exception: ‘+ $_.Exception.Message +‘ to enable receive location: ‘+$rcvLocation+ ‘ in BizTalk Application: ‘+$BizAppName+‘ at datetime: ‘+(Get-Date).ToString()+“`r`n”
          Add-Content $OutResultLog $resultToWrite
          $resultToWrite=“”
          }
      }
    }
   catch [Exception]
    {
    $resultToWrite=$resultToWrite+ ‘Getting Exception : ‘ +$_.Exception.Message +‘ on receive port loop at datetime: ‘+(Get-Date).ToString()+“`r`n”
    Add-Content $OutResultLog $resultToWrite
    $resultToWrite=“”
    }
}
$resultToWrite=$resultToWrite+Â ‘Process end to enabled receive location on datetime: ‘+(Get-Date).ToString()Â +“`r`n”
Add-Content $OutResultLog $resultToWrite #logged all details in log file
[void]$Catalog.Refresh()Â # refresh biztalk console
}
Â
#Function to enable the receive location
function enableReceiveLocation(){
   $location = get-wmiobject MSBTS_ReceiveLocation -Namespace ‘root\MicrosoftBizTalkServer’ -Filter “name=’${rcvLocation}‘”
   [void]$location.Enable()
   $resultToWrite=$resultToWrite+ ‘Receive location: ‘+$rcvLocation+ ‘ has enabled in BizTalk Application: ‘+$BizAppName+‘ at datetime :’+(Get-Date).ToString()
   Add-Content $OutResultLog $resultToWrite
   $resultToWrite=“”
   }
Â
 #check status of Receive Location
  CheckReceiveLocatonStatusThenEnable
Please keep in mind the following instructions: Use the BizTalk Admin Group user to create a Window Task Scheduler. If the BizTalkMgmtDb database is running under a different user, please define the username and password in the ConnectionString.
Additionally, you can refer to another PowerShell script to enable only one receive location. The script is titled “PowerShell Script to Enable a ReceiveLocation“.
