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!

Super cool! Using it all the time now! Bye bye WMI (finally)!
Thanks!
I need a script that will resubmit all resumable messages failed during maintenance windows. How can I do this adding to your script?
I need bts-terminate-message()! please.
Its done:
#
# list suspended instances
#
function bts-list-suspended()
{
get-wmiobject MSBTS_ServiceInstance `
-namespace ‘root\MicrosoftBizTalkServer’ `
-filter ‘ServiceClass=4 and (ServiceStatus = 4 or ServiceStatus = 16)’
}
#
# terminate 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”
}
}
Call:
bts-list-suspended | %{ bts-terminate-instance($_.InstanceID) }
Hello Dear Sir,
I am new to power shell. How do I get count ( number) for suspended instaces?
Regards,
James
Can you point me to a ps script to resume all suspended instances, please?
Cheers,
Bill =B-)
Ok, I found out that replacing E’s bts-terminate-instance() function with a bts-resume-instance() function, basically replacing the word ‘terminate’ with the word ‘resume’… and it worked!
#
# 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”
}
}
Note that I also simplified the bts-list-suspended() function to filter just for ServiceStatus=4:
#
# list suspended instances
#
function bts-list-suspended()
{
get-wmiobject MSBTS_ServiceInstance -namespace ‘root\MicrosoftBizTalkServer’ -filter ‘ServiceStatus=4′
}
This served my purposes.
Thanks to all of you for sharing your scripts!
Cheers,
Bill =B-)
Hi,
Is there a script that can be used to get the ‘Routing Failure Report’ from BizTalk admin console?
Basically I would like the context properties associated with this message to be saved to a file.
Any suggestions is greately appriciated.
Regards
Biranchi
Nice Post
Thanks for the sharing