PowerShell script to restart all host instances in multiple server for SFTP adapter at Send handler side
In my previous post for restart host instance for sftp receive side and same with PowerShell Script that restart all host instances for SFTP protocol at send side. The below following script is very simple and you can also change SFTP adapter to another adapter like WCF-SQL, FTP, File etc. I am writing PS Script for SFTP adapter because if you are working more than 100 SFTP server then after some times it goes to slow to pick file from sftp server after restart sftp host instance the caches is clear and its start working fine.
# 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
$Hostinst = "" #
Define variable for host instances
$resultToWrite=""
$OutResultLog = 'D:\BizTalkPowerShellService\RestartSFTPSndHostInst\logRestartSFTPSendHostInstances.txt'
$ServerName="SPD-PRD-BZT06" #Define running server name
#Function to restart sftp send handler host instances
function RestartSFTPSendHostInstance()
{
$resultToWrite='Process has started
to restart sftp send 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')"
foreach
($Hosts in
$catalog.SendHandlers
| Where {($_.TransportType.Name -eq 'SFTP')})
{
foreach($Hostinstance in
$Hosts ){
try
{
$Hostinst
= $Hostinstance.Name
#Restart-HostInstance
-Path 'BizTalk:\Platform Settings\Host Instances\Microsoft BizTalk Server
'$Hostinst' PFMS-27'
#[ARRAY]$hostInstances
= Get-WmiObject MSBTS_HostInstance -namespace
"root\MicrosoftBizTalkServer" -Filter "(HostType = 1 and RunningServer
= '$ServerName' and HostName = '$Hostinst')"
foreach($hostinst1 in
$hostInstances |
WHERE {($_.HostName -eq
$Hostinst) -and
($_.HostName
-notlike 'BizTalkServerApplication')
-and ($_.RunningServer -eq
$ServerName)}) {
if ($hostinst1.ServiceState -eq
4) # 4 = host instance
is running
{
$hostinst1.Stop()
#Wait
for few seconds
Start-Sleep
-s 3
$hostinst1.Start()
#Wait
for few seconds
Start-Sleep
-s 2
$resultToWrite=$resultToWrite+ 'Restart host
instance: '+$Hostinst+ ' at datetime :'+(Get-Date).ToString()
Add-Content
$OutResultLog $resultToWrite
$resultToWrite=""
}
if ($hostinst1.ServiceState -eq
1) # 1 = host instance
is stoped
{
$hostinst1.Start()
#Wait
for few seconds
Start-Sleep
-s 2
$resultToWrite=$resultToWrite+ 'Restart host
instance: '+$Hostinst+ ' at datetime :'+(Get-Date).ToString()
Add-Content
$OutResultLog $resultToWrite
$resultToWrite=""
}
}
#Restart
host instance
#
Restart-Service -Displayname "BizTalk Service BizTalk Group :
${Hostinst}"
}
catch
[Exception]
{
$resultToWrite=$resultToWrite+ 'Getting Exception:
'+ $_.Exception.Message
+' to restart
host instance: '+$Hostinst+ ' at datetime: '+(Get-Date).ToString()+"`r`n"
Add-Content
$OutResultLog $resultToWrite
$resultToWrite=""
}
}
}
$resultToWrite=$resultToWrite+
'Process end to restart sftp send 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 restart host instances
RestartSFTPSendHostInstance
Note: Use BizTalk Admin Group user to create Window Task
Scheduler and If BizTalkMgmtDb database run under different user then please
define Username and possword it in ConnectionString.
Comments
Post a Comment
Please write comment only that belongs to this blog