I have been working on implementing ServiceBus using powershell. There is to my knowledge no modules/scripts available to communicate with ServiceBus from powershell. There is a Azure powershell module on Git that enables you to create/manage Queues/namespaces, however not to send/receive messages. Let’s just jump right in and test it.
- Create a Queue on Azure and recceive the connection Information (connectionstring) (http://azure.microsoft.com/en-us/documentation/articles/service-bus-dotnet-how-to-use-queues/)
- Download/clone the PowerBus repository on bitbucket (https://bitbucket.org/torgro/powerbus/wiki/Home)
- Unblock the downloaded zip file (unblock-file –path c:\temp\PowerBus.zip)
- Copy the module to one of your module paths
- Import-Module –Name PowerBus
How to use
After importing these commands will be available for you:
Get-BusMessage and Send-BusMessage are used for receiving and sending messages to the Queue. These functions need to know your connection string (ConnectionString) and the name of the Queue (QueueNameString.
You can provide those as parameters to the functions or you can add those 2 parameters with the Set-BusDefaults:
So if I run this:
Set-BusDefaults -BusConnectionstring "Endpoint=sb://yourqueue.servicebus.windows.net/;SharedAccessKeyName=rootKey;SharedAccessKey=eLOdFasdaKJoiOIJhiuhO98hoihjLKH” -QueueName "testqueue"
This will add a couple of defaults to the PSDefaultParameterValues automatic variable at the module scope which will add the connection string and queuename as parameters when you call Get-BusMessage or Send-BusMessage. You can retrieve the current connection string and queue name with the Get-BusDefaults function.
So with the connection string set and the queue name added, we can start to send messages to the queue with Send-BusMessage:
We can retrieve the message with the Get-BusMessage command
The ServiceBus queue implements the FIFO principle (first in, first out). To de-queue messages, use the switch parameter dequeue. Default is peek mode when you run Get-BusMessage without the switch parameter. As you can see in the screenshot above, I first run Get-BusMessage to peek at the message, run it with the dequeue switch and finally try to get the message again with Get-BusMessage which returns $null (no messages in the queue).