Reputation: 529
I'm trying to get the count of messages present in outgoing queue. I'm using C# with Powershell to achieve this. I'm using below command
Get-MsmqOutgoingQueue | Format-Table -Property MessageCount
This command is getting executed successfully on powershell command prompt. But when I try this from C#, it is giving following exception:
The type initializer for 'Microsoft.Msmq.PowerShell.Commands.Utilities' threw an exception.
Below is the C# code that I'm using to execute this command:
string scriptTest = "Get-MsmqOutgoingQueue | Format-Table -Property MessageCount";
Runspace runspace = RunspaceFactory.CreateRunspace();
runspace.Open();
Pipeline pipeline = runspace.CreatePipeline();
pipeline.Commands.AddScript(scriptText);
pipeline.Commands.Add("Out-String");
Collection<PSObject> results = pipeline.Invoke();
runspace.Close();
string s = "";
foreach (PSObject obj in results)
{
s = obj.ToString();
}
Console.WriteLine(s);
I tried this code by giving all permission but it's giving same exception.
Upvotes: 1
Views: 129
Reputation: 529
Fixed the issue by adding following useLegacyV2RuntimeActivationPolicy in Startup tag.
<configuration>
<startup useLegacyV2RuntimeActivationPolicy="true">
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6"/>
</startup>
</configuration>
Upvotes: 1