none
Unable to terminate suspended based on particular Biztalk application Name by Powershell script. RRS feed

  • Question

  • Hi All,

    I am using below script,

    This below powershell script working fine for terminating the suspended messages based on service name but when i modified this script based on application name ,it is not working.Could you guys have a look and suggest .I am using Biztalk 2016 and powershell ISE.

            

    # Description: This script terminates suspended messages, matching a certain filter.

    # Instructions: 1) Open Powershell console and run this script on a BizTalk 2016 
    #                  server that currently has suspended messages.
    #               2) Enter the name of a BizTalk service which caused the suspended messages.
    #               3) Enter key part(s) of the error description for suspended messages. 
    #                  That were caused by the BizTalk service you entered the name of 
    #                  in previous step nr2. You may use wildcards (*) for the remainder of 
    #                  the error description.
    #               4) The script will now look for suspended messages matching the 
    #                  criteria set in step 2-3. If any found then those will be terminated.


    #
    ######################################################################################

    function terminate-suspmsg ([string]$ServiceName, [string]$ErrorDesc) {
    try {
    # retrive biztalk database server name
    $global:mgmtDbServer = get-wmiobject MSBTS_GroupSetting -namespace root\MicrosoftBizTalkServer | select-object -expand MgmtDbServerName
    # retrive biztalk managment database name
    $global:mgmtDbName = get-wmiobject MSBTS_GroupSetting -namespace root\MicrosoftBizTalkServer | select-object -expand MgmtDbName

    # load Microsoft.BizTalk.Operations
    [void] [System.reflection.Assembly]::LoadWithPartialName("Microsoft.BizTalk.Operations")

    # create instanse of BizTalkOperations
    $GetSuspendedMessages = New-Object Microsoft.BizTalk.Operations.BizTalkOperations($mgmtDbServer, $mgmtDbName)

    # write confirmation message to console
    Write-Host "Start looking for suspended messages caused by servicename $ServiceName with error description:`r`n`r`n$ErrorDesc`r`n`r`n" -foregroundcolor "Green"

    # look for suspended messages, store result in $MsgBody
    $SuspendedMessages = $GetSuspendedMessages.GetMessages() | where-object {$_.ErrorDescription -like $ErrorDesc -and $_.ServiceType -eq $ServiceName} | Select InstanceID

    # dispose connection to mgmtDbServer
    $GetSuspendedMessages.Dispose()

    # if any suspended message were found
    If ($SuspendedMessages) {
    # reset counter to 0
    $i = 0

    # for each suspended message found
    $SuspendedMessages | ForEach-Object {
    # store current instid
    $instid = $_.InstanceID

    # increase counter
    $i++

    # create new connection
    $TerminateOperation = New-Object Microsoft.BizTalk.Operations.BizTalkOperations($mgmtDbServer, $mgmtDbName)

    Write-Host "Terminating message"$i" with instance id: "$instid -foregroundcolor "Yellow"

    # terminate suspended message
    $TerminateOperation.TerminateInstance($instid) | Out-null

    # dispose connection to mgmtDbServer
    $TerminateOperation.Dispose()
    }
    # if no messages were found
    } else {
    # write confirmation to console
    Write-Host "No suspended messages body found for BizTalk Service $ServiceName and error description:`r`n`r`n$ErrorDesc`r`n`r`n" -foregroundcolor "Yellow"
    }

    # final confirmation message written to console
    Write-Host "Done looking for suspended messages caused by servicename $ServiceName with error description:`r`n`r`n$ErrorDesc`r`n`r`n" -foregroundcolor "Green"
    }
    # simple fault handler
    catch {
    $ErrorMsg = "Unable to retrive msgbody for BizTalk Service $ServiceName and error description:`r`n$ErrorDesc"
    $ErrorMsg += "Script report the following reason(s):`r`n"
    $ErrorMsg += [string]$error[0] + "`r`n`r`n"
    $ErrorMsg += [string]$error[0].InvocationInfo.PositionMessage + "`r`n`r`n"
    Write-Host $ErrorMsg -foregroundcolor "Red"
    }
    }

    # set biztalk service name to look for
    $ServiceName = Read-host "Enter a name of a biztalk service, that caused suspended messages"
    # set error description to look for
    $ErrorDesc = Read-host "Enter part (wildcards ""*"" allowed) of the error description which ""$ServiceName"" caused"

    # trigger function terminate-suspmsg
    terminate-suspmsg $ServiceName $ErrorDesc

    ->But when I modified the above script with Application Name ,Its not hitting the application name and not terminating suspended messages based on Application ,It is going to the else part.

    Friday, December 6, 2019 9:10 AM

All replies

  • >But when I modified the above script with Application Name ,Its not hitting the application name and not terminating suspended messages based on Application Eventhough 20K suspended messages are present for that particular application,It is going to the else part.

    function terminate-suspmsg ([string]$ApplicationName, [string]$ErrorDesc) {
    try {
            
    # retrive biztalk database server name
    $global:mgmtDbServer = get-wmiobject MSBTS_GroupSetting -namespace root\MicrosoftBizTalkServer | select-object -expand MgmtDbServerName
    # retrive biztalk managment database name
    $global:mgmtDbName = get-wmiobject MSBTS_GroupSetting -namespace root\MicrosoftBizTalkServer | select-object -expand MgmtDbName
     
    # load Microsoft.BizTalk.Operations
    [void] [System.reflection.Assembly]::LoadWithPartialName("Microsoft.BizTalk.Operations")
     
    # create instanse of BizTalkOperations
    $GetSuspendedMessages = New-Object Microsoft.BizTalk.Operations.BizTalkOperations($mgmtDbServer, $mgmtDbName)

    # write confirmation message to console
    Write-Host "Start looking for suspended messages caused by ApplicationName $ApplicationName with error description:`r`n`r`n$ErrorDesc`r`n`r`n" -foregroundcolor "Green"
     
    # look for suspended messages, store result in $MsgBody
    $SuspendedMessages = $GetSuspendedMessages.GetMessages() | where-object {$_.ErrorDescription -like $ErrorDesc -and $_.ApplicationName -eq $ApplicationName} | Select InstanceID

    # dispose connection to mgmtDbServer
    $GetSuspendedMessages.Dispose()

    # if any suspended message were found
    If ($SuspendedMessages) {
    # reset counter to 0
    $i = 0

    # for each suspended message found
    $SuspendedMessages | ForEach-Object {
    # store current instid
    $instid = $_.InstanceID

    # increase counter
    $i++
     
    # create new connection
    $TerminateOperation = New-Object Microsoft.BizTalk.Operations.BizTalkOperations($mgmtDbServer, $mgmtDbName)

    Write-Host "Terminating message"$i" with instance id: "$instid -foregroundcolor "Yellow"

    # terminate suspended message
    $TerminateOperation.TerminateInstance($instid) | Out-null

    # dispose connection to mgmtDbServer
    $TerminateOperation.Dispose()
    }
    # if no messages were found
    } else {
    # write confirmation to console
    Write-Host "No suspended messages body found for BizTalk Application $ApplicaionName and error description:`r`n`r`n$ErrorDesc`r`n`r`n" -foregroundcolor "Yellow"
    }
     
    # final confirmation message written to console
    Write-Host "Done looking for suspended messages caused by Application $ApplicaionName with error description:`r`n`r`n$ErrorDesc`r`n`r`n" -foregroundcolor "Green"
    }
    # simple fault handler
    catch {
    $ErrorMsg = "Unable to retrive msgbody for BizTalk Application $ApplicaionName and error description:`r`n$ErrorDesc"
    $ErrorMsg += "Script report the following reason(s):`r`n"
    $ErrorMsg += [string]$error[0] + "`r`n`r`n"
    $ErrorMsg += [string]$error[0].InvocationInfo.PositionMessage + "`r`n`r`n"
    Write-Host $ErrorMsg -foregroundcolor "Red"
    }
    }
     
    # set biztalk service name to look for
    $ApplicationName = Read-host "Enter a name of a biztalk Application, that caused suspended messages"
    # set error description to look for
    $ErrorDesc = Read-host "Enter part (wildcards ""*"" allowed) of the error description which ""$ApplicationName"" caused"
     
    # trigger function terminate-suspmsg
    terminate-suspmsg $ApplicationName $ErrorDesc

    Friday, December 6, 2019 9:14 AM