Azure Message Queue Manipulation with PowerShell

New to Message queues in Azure? Here’s some samples for working with them.


#Azure Message Queue Manipulation

$StorageAccountName = "storage001"
$StorageAccountKey = Get-AzureRMStorageAccountKey -StorageAccountName $StorageAccountName -ResourceGroupName "Storage"
$storeAuthContext = New-AzureStorageContext $StorageAccountName -StorageAccountKey $StorageAccountKey[0].value

# Create Queue
$myQueue = New-AzureStorageQueue -Name 'myqueue' -Context $storeAuthContext

# Create Queue Message	
$queueMessage = New-Object -TypeName Microsoft.WindowsAzure.Storage.Queue.CloudQueueMessage -ArgumentList 'Hello'
#Can run next line a few times in a row to fill queue with data
$myQueue.CloudQueue.AddMessage($queueMessage)


# Get Queue Message  (gets oldest message in queue and takes it out of queue)
$myQueue.CloudQueue.GetMessage()

#Get-AzureStorageQueue will retrieve general information about the storage queue including approximate message count. 


##PartitionKey, RowKey and TimeStamp required properties.  TimeStamp is created automatically.

## Some C# Commands
// Retrieve a reference to a queue
CloudQueue queue = queueClient.GetQueueReference("myqueue");

// Peek at the next message
CloudQueueMessage peekedMessage = queue.PeekMessage();

// Display message.
Console.WriteLine(peekedMessage.AsString);

// Create a message and add it to the queue.
CloudQueueMessage message = new CloudQueueMessage("Hello, World");
queue.AddMessage(message);

// Fetch the queue attributes.
queue.FetchAttributes();

// Retrieve the cached approximate message count.
int? cachedMessageCount = queue.ApproximateMessageCount;

// Display number of messages.
Console.WriteLine("Number of messages in queue: " + cachedMessageCount);

// Delete the queue.
queue.Delete();



Leave a Reply

Your email address will not be published. Required fields are marked *