In below following complete PowerShell script is to start and stop all BizTalk host instances. There is a filter condition with '-notlike' powershell command where if you want to avoid host instance by the host name. There are two functions one is for start host instance and second one is for stop host instance. You go ahead and use accordingly 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
Nice blog keep sharing more blogs
ReplyDeleteAngular course
Angular training
angular certification
angularjs online training
angularjs online course
Angular Online Training
Angularjs Online Training Hyderabad
Angularjs Online Training India
Needed to compose you a very little word to thank you yet again regarding the nice suggestions you’ve contributed here.
ReplyDeleteOutsourced Staffing Services
facility scheduling software