Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automated MSMQ Setup with Powershell

I am in the process of configuring a new test server for an application I support. It uses around 35 different MSMQ queues and creating these manually is obviously not loads of fun. Especially since the production version of the application is also moving servers so I'll get to do this all over again. What I'm looking for is an automated way to create these queues and Powershell (based on my limited knowledge of it) seems like the way to go.

Does anyone have any tips on how I might go about accomplishing this?

like image 240
Scott Salyer Avatar asked Aug 20 '09 23:08

Scott Salyer


3 Answers

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( $_ ) }
like image 168
Scott Saad Avatar answered Sep 19 '22 13:09

Scott Saad


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
like image 35
Keith Hill Avatar answered Sep 19 '22 13:09

Keith Hill


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....

like image 24
BFree Avatar answered Sep 22 '22 13:09

BFree