Menu

Part 1 - Querying for Suspended Instances using PowerShell in a C# application

Sometimes you just want to get all the messages associated with a suspended BizTalk instance...say they get suspended at a point where an exception occurred and resuming only results in that same exception occurring again. My early BizTalk projects need some work :)

In any case, I looked at a few options for doing so:

you could go with querying the Tracking database directly using the built in bts_GetTrackedMessageParts stored procedure. But then you need to decompress the stream produced.

Then there is WMI (Windows Management Instrumentation) which to me, looked way too complicated and possibly slow

finally you can use a little combination of PowerShell and BizTalk.Operations

It sounds funny PowerShell? inside a C# application? Yup. In this post, we'll go over the PowerShell portion of the code.

All you need are the following dlls included in your project to use PowerShell:

using System.Management;
using System.Management.Automation;
using System.Management.Automation.Runspaces;

Note

Here is how to add the System.Managment.Automation dll to your project:

In short

Another, and I think better, way to do it is to open your project file and in the first “<ItemGroup>” section, add:
<Reference Include="System.Management.Automation" />
This worked great for me.

The funny thing is, this PowerShell script is actually executing a WMI command, but it's actually much easier let PowerShell + the WMI command do the work rather than rolling your own WMI stuff. Here's the PowerShell part of it:

After this runs, you'll have a Collection of PSObjects to do with what you want. In my case, I wanted to take those objects and pass them to the BizTalkOperations side of things to pull out suspended messages.