views:

640

answers:

4

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?

+1  A: 

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

BFree
Perfect. I think I'm going to attempt this route since I have no familiarity with Powershell yet and it seems like it would be a good introduction. Thanks!
digitall
A Cmdlet seems a bit overkill for this particular task. A few lines of script and there is no reason to compile anything or install anything (which you have to do with a Cmdlet).
Scott Saad
A: 

There's no reason you can't use System.Messaging from within PowerShell. Just load the right assembly and you can create/delete/hook your queues to your heart's content. Creating a custom cmdlet is certainly an interesting option, but might be overkill depending on what you want (a simple script might just do the trick).

tomasr
+2  A: 

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( $_ ) }
Scott Saad
+1  A: 

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
Keith Hill