Sometimes you just want to quickly be able to and/or extract messages from your BizTalk suspended queue for viewing and saving. This can be done in several ways, including querying the BizTalk database directly or using WMI objects in C#. Neither of which are particularly appealing sometimes.
After looking around for options, I discovered a handy Powershell command that takes advantage of querying WMI, without the overhead of having to deal with the actual WMI objects in C#. The thing is, I wanted to write a C# program to actually manipulate the results.
That's where System.Management.Automation
comes into play. This allows you to run Powershell scripts from within a .Net program.
So, here's the command itself, stored into a string variable:
// simple powershell script to extract suspended service instances.
// Because it's easier than using WMI objects in C#
string script = @"get-wmiobject MSBTS_ServiceInstance " +
"-namespace 'root\MicrosoftBizTalkServer' " +
"-filter '(ServiceClass=1 or ServiceClass=4) and ServiceStatus = 4'";
Next just create an instance of a PowerShell
object:
Runspace runspace = RunspaceFactory.CreateRunspace();
runspace.Open();
// instantiate PowerShell and run the script from above
PowerShell powerShell = PowerShell.Create();
powerShell.Runspace = runspace;
powerShell.AddScript(script);
Collection<PSObject> results = powerShell.Invoke();
Which, as you can see above, returns a Collection of PSObject
objects. From here you can do what you wish with the results. In my case I wanted to Save the messages as files to disk.
foreach (PSObject result in results)
{
Console.WriteLine(result.Properties["InstanceID"].Value);
SaveMessageToDisk(result.Properties["InstanceID"].Value.ToString(),
savePath,
ConfigurationManager.AppSettings["MessageBoxDBName"]);
}
The SaveMessageToDisk
uses BizTalk dlls to works it's magic and query the BizTalk Messagebox and write out the messages to disk. It's basically along the same lines as saving a message from the BizTalk Admin console. We'll save that for a later post.
In case you're wondering what the filter parameters in the PowerShell query mean from above:
-filter '(ServiceClass=1 or ServiceClass=4) and ServiceStatus = 4'
Here is a list of all the filters for ServiceClass and ServiceStatus:
//******** List of WMI Service Statuses ********
// ServiceClass = 1 (this gets Orchestration suspended instances)
// ServiceClass = 4 (messaging service instances).
//
// ServiceStatus = 1 - Ready To Run
// ServiceStatus = 2 - Active
// ServiceStatus = 4 - Suspended (Resumable)
// ServiceStatus = 8 - Dehydrated
// ServiceStatus = 16 - Completed With Discarded Messages' in BizTalk Server 2004
// ServiceStatus = 32 - Suspended (Not Resumable)
// ServiceStatus = 64 - In Breakpoint