Automated MSMQ Setup with Powershell Automated MSMQ Setup with Powershell powershell powershell

Automated MSMQ Setup with Powershell


Maybe something like. It'a a little verbose but it's to help demonstrate that PowerShell can do this without a CmdLet.

# Loads the assembly into PowerShell (because it's not a pre-loaded one)[Reflection.Assembly]::LoadWithPartialName( "System.Messaging" ) | Out-Null# This is just an array which could also just be a file$queueList = ( ".\q1", ".\q2", ".\q3", ".\q4" )# Create the queues by piping the list into the creation function# $_ refers to the current obect that the ForEach-Object is on$queueList | ForEach-Object { [System.Messaging.MessageQueue]::Create( $_ ) }


If you are using the PowerShell Community Extensions (PSCX), it has cmdlets for creating and managing MSMQ:

  • Clear-MSMQueue
  • Get-MSMQueue
  • New-MSMQueue
  • Test-MSMQueue


I think the approach you should take is to create your own Powershell cmdlet (Commandlet). Basically, you inherit from a base class, override a method, and that's the method that gets called when you call that cmdlet from Powershell. This way you can do what you need to do in C# and just call it from Powershell. Figure something like this:

EDIT: Forgot to link to MSDN for creating cmdlets: http://msdn.microsoft.com/en-us/library/dd878294(VS.85).aspx

[Cmdlet(VerbsCommunications.Get, "MyCmdlet")]public class MyCmdlet : Cmdlet{    [Parameter(Mandatory=true)]    public string SomeParam {get; set;}    protected override void ProcessRecord()    {         WriteObject("The param you passed in was: " + SomeParam);    }}

You would then call this cmdlet from Powershell something like this:

PS>Get-MyCmdlet -SomeParam 'whatever you want'

Then, to use MSMQ, there are many samples online on how to accomplish this from within C#:

Here's just one of them....