Quantcast
Channel: Integration Experts - Dipesh Avlani » BizTalk 2010 R2
Viewing all articles
Browse latest Browse all 8

Powershell script to administer messages in BizTalk

$
0
0

The below script does everything from resume to terminate the suspended messages. It also allows you to get counts and save suspended messages into dedicated folders.

# Script to save, resume and list suspended messages. To be used by BizTalk admin ONLY!
# Created on 30/01/2014 by Dipesh A.
# Commands:
   # Note: CD to script path first.

    # To get count of all suspended messages
    # PS > .\GetSuspended.ps1 -action counts

    # To list all suspended messages
    # PS > .\GetSuspended.ps1 -action list

    # To save all suspended messages
    # PS> .\GetSuspended.ps1 -action save -path 'X:\[Path]'

    # To resume resumable suspended messages
    # PS> .\GetSuspended.ps1 -action resume

    # To terminate non-resumable suspended messages
    # PS> .\GetSuspended.ps1 -action terminate

# -----------------------------------------------------------------

# declare params: the action to take, and an optional
# path to save messages to
#
param(
    [string] $action=$(throw 'need action'),
    [string] $path=$(if ($action -eq 'save') { throw 'need path' })
)

#
# get all suspended messaging service instances,
# both resumable and not-resumable
#
function bts-get-messaging-svc-instances()
{
    get-wmiobject MSBTS_ServiceInstance `
    -namespace 'root\MicrosoftBizTalkServer' `
    -filter '(ServiceClass=1 or ServiceClass=4) and ServiceStatus = 4'
}

#

# save the message associated to the
# specified messaging Service Instance
#
function bts-save-message([string]$msgid, [string]$sname, $homepath)
{

    #save each message within respective folders.

    "msgid is $msgid"
    "ServiceName is $sname"
    "msg_counter is $counter"
    $msg = get-wmiobject MSBTS_MessageInstance `
    -namespace 'root\MicrosoftBizTalkServer' `
    -filter "ServiceInstanceID = '$msgid'"

    $newpath = (Join-Path $homepath $sname)
    "new path is $newpath"

    $r_code= (test-path $newpath)
    "return code is $r_code"

    if ($r_code -eq $False)
       {
       md $newpath
       "new directory created"
    }

    foreach($m in $msg)
    {
       $m.psbase.invokemethod('SaveToFile', ($newpath))
       "Message from ServiceInstanceID=$msgid saved to $newpath."
   }

}

#
# save the message associated to the
# specified messaging Service Instance
#
function bts-save-message1([string]$msgid)
{

    $msg = get-wmiobject MSBTS_MessageInstance `
    -namespace 'root\MicrosoftBizTalkServer' `
    -filter "ServiceInstanceID = '$msgid'"
    $msg.psbase.InvokeMethod('SaveToFile', ($path))
    "Message from ServiceInstanceID=$msgid saved."
}

#
# list resumable suspended instances
#
function bts-get-resumable-suspended()
{
    get-wmiobject MSBTS_ServiceInstance -namespace 'root\MicrosoftBizTalkServer' -filter 'ServiceStatus=4'
}

#
# list resumable suspended instances
#
function bts-get-nonresumable-suspended()
{
    get-wmiobject MSBTS_ServiceInstance -namespace 'root\MicrosoftBizTalkServer' -filter 'ServiceStatus=32'
}

#
# resume instance
#
function bts-resume-instance([string]$msgId)
{
    if(!($msgId -eq “”))
    {
       "Resume {0}" -f $msgId
       $msg = get-wmiobject MSBTS_ServiceInstance -namespace 'root\MicrosoftBizTalkServer' -filter “InstanceID = '$msgId'”
       $msg.Resume() | Out-Null
    "- Done"
    }
    else
    {
       "MessageId missing"
    }
}

#
# terminate non resumable instance
#
function bts-terminate-instance([string]$msgId)
{
    if(!($msgId -eq “”))
    {
       "Terminate {0}" -f $msgId
       $msg = get-wmiobject MSBTS_ServiceInstance -namespace 'root\MicrosoftBizTalkServer' -filter “InstanceID = '$msgId'”
       $msg.Terminate() | Out-Null
       "- Done"
   }
   else
    {
       "MessageId missing"
   }
}

#
# main script
#
switch ( $action )
{
    'counts' {
       bts-get-messaging-svc-instances |
       %{ $counter++; }

       "Total: $counter"

       $counterrr = 0
       bts-get-resumable-suspended | %{ $counterrr++; }

       "Resumable: $counterrr"

       $counternr = 0
       bts-get-nonresumable-suspended | %{ $counternr++; }

       "Non resumable: $counternr"
    }

   'list' {
        bts-get-messaging-svc-instances |
       fl InstanceId, ServiceName, SuspendTime, HostName,
       ServiceStatus, ErrorId, ErrorDescription
    }
    'save'
    {

       #Set home path
       $timestamp = Get-Date -UFormat "%Y%m%d%H%M%S"
       $newpath = (Join-Path $path $timestamp)
       "Home path is $newpath"

       $r_code= (test-path $newpath)
       "return code is $r_code"

       if ($r_code -eq $False)
       {
          md $newpath
          "new directory created"
       }

        #save summary
       $wmi = get-wmiobject MSBTS_ServiceInstance `
       -namespace 'root\MicrosoftBizTalkServer' `
       -filter '(ServiceClass=1 or ServiceClass=4) and ServiceStatus = 4' | out-string

       $file = New-Item -Type File -Name "Summary.txt" -Path $newpath
       Add-Content $file -Value $wmi

       bts-get-messaging-svc-instances |
       %{ $counter++; bts-save-message $_.InstanceID $_.ServiceName $newpath }

    }
    'resume'
    {
      bts-get-resumable-suspended | %{ bts-resume-instance($_.InstanceID) }
    }
    'terminate'
    {
      bts-get-nonresumable-suspended | %{ bts-terminate-instance($_.InstanceID) }
    }
}



Viewing all articles
Browse latest Browse all 8

Latest Images

Trending Articles





Latest Images