RabbitMQ in Powershell

I have been trying to work out how to interact with RabbitMQ via Powershell.

I have seen PoshRabbit but that is not quite flexible enough for my purposes. I am looking for simple admin tasks.

The obvious starting point is the excellent RabbitMQ.Client assembly.

The only flaw is that the ConnectionFactory is not common type system safe due to a pair of properties that are not case unique. However with a bit of reflection even this can be overcome. Here is my first draft of the Send example in powershell:

[Reflection.Assembly]::LoadFile(($pwd).Path + “RabbitMq.Client.Dll”)
$factory = new-object RabbitMQ.Client.ConnectionFactory
$hostNameProp = [RabbitMQ.Client.ConnectionFactory].GetField(“HostName”)
$hostNameProp.SetValue($factory, “localhost”)
$createConnectionMethod = [RabbitMQ.Client.ConnectionFactory].GetMethod(“CreateConnection”, [Type]::EmptyTypes)
$connection = $createConnectionMethod.Invoke($factory, “instance,public”, $null, $null, $null)
$channel = $connection.CreateModel()
$channel.QueueDeclare(“hello”, $false, $false, $false, $null)
$message = “Hello, World!”
$body = [System.Text.Encoding]::UTF8.GetBytes($message)
$channel.BasicPublish(“”, “hello”, $null, $body)
Write-Host ” [x] Sent $message”

Now that I can create the connection the rest of the examples should be trivial to port.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s