Continuing with my series on managing BizTalk Server with PowerShell, here's a new script that deals with suspended messaging service instances:

#

# declare our parameters: 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=4 and (ServiceStatus = 4 or ServiceStatus = 16)'

}



#

# save the message associated to the

# specified messaging Service Instance

#

function bts-save-message([string]$msgid)

{

   $msg = get-wmiobject MSBTS_MessageInstance `

      -namespace 'root\MicrosoftBizTalkServer' `

      -filter "ServiceInstanceID = '$msgid'"

   $msg.psbase.invokemethod('SaveToFile', ($path))

   "Message from ServiceInstanceID=$msgid saved."

}





#

# main script

#

switch ( $action )

{

   'list' {

      bts-get-messaging-svc-instances |

         fl InstanceId, ServiceName, SuspendTime, HostName,

            ServiceStatus, ErrorId, ErrorDescription

   }

   'save' {

      bts-get-messaging-svc-instances |

         %{ bts-save-message($_.InstanceID) }

   }

}

If you run it with the 'list' action, you can get a nicely formatted list with the more significant properties of any suspended messaging service instance:


PS E:\Projects\BizTalk\PSScripts> .\bts-get-suspended-msgs -action list
InstanceId : {24FF0CAC-58F8-424C-BC49-6E1BD5045463}
ServiceName : AttachmentRequestRP
SuspendTime : 20060902091027.000000-300
HostName : BizTalkServerApplication
ServiceStatus : 4
ErrorId : 0xc0c01680
ErrorDescription : The published message could not be routed because no subscri
bers were found. This error occurs if the subscribing orches
tration or send port has not been enlisted, or if some of th
e message properties necessary for subscription evaluation h
ave not been promoted. Please use the Biztalk Administration
console to troubleshoot this failure.
InstanceId : {0661221D-3C2F-4AD6-B952-099E02BD0E21}
ServiceName : AttachmentRequestRP
SuspendTime : 20060902091045.000000-300
HostName : BizTalkServerApplication
ServiceStatus : 4
ErrorId : 0xc0c01680
ErrorDescription : The published message could not be routed because no subscri
bers were found. This error occurs if the subscribing orches
tration or send port has not been enlisted, or if some of th
e message properties necessary for subscription evaluation h
ave not been promoted. Please use the Biztalk Administration
console to troubleshoot this failure.

Do notice the script filters out so that we only consider messaging service instances and not others like a suspended orchestration, you can however very easily tweak the script to show those as well as present a different set of properties for each instance.

You can also run the script with the 'save' action and provide a secondary 'path' argument to the script. In this case, the script will save any tracked messages associated with the suspended messaging service instances into the specified folder:

PS E:\Projects\BizTalk\PSScripts> .\bts-get-suspended-msgs -action save 'c:\temp
\ctt'
Message from ServiceInstanceID={24FF0CAC-58F8-424C-BC49-6E1BD5045463} saved.
Message from ServiceInstanceID={0661221D-3C2F-4AD6-B952-099E02BD0E21} saved.
Message from ServiceInstanceID={9E661EE6-4599-4458-B370-179220D66CF1} saved.

Enjoy!


Tomas Restrepo

Software developer located in Colombia.