4

I am using Azure PowerShell to create and add entries to an Azure queue, following example here: MSDN: Using Azure PowerShell with Azure Storage - How to manage Azure queues .

Here is my PowerShell script:

$storeAuthContext = New-AzureStorageContext -StorageAccountName '[my storage account name]' -StorageAccountKey '[my storage account key'
$myQueue = New-AzureStorageQueue –Name 'myqueue' -Context $storeAuthContext
$queueMessage = New-Object -TypeName Microsoft.WindowsAzure.Storage.Queue.CloudQueueMessage -ArgumentList 'Hello'
$myQueue.CloudQueue.AddMessage($queueMessage)

This works fine the first time I run it.

Second time, I get this:

New-AzureStorageQueue : Queue 'myqueue' already exists. At line:1 char:12 + $myQueue = New-AzureStorageQueue –Name 'myqueue' -Context $storeAuthContext + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : ResourceExists: (:) [New-AzureStorageQueue], ResourceAlreadyExistException + FullyQualifiedErrorId : ResourceAlreadyExistException,Microsoft.WindowsAzure.Commands.Storage.Queue.NewAzureStorageQueueCommand

In .NET Azure Storage API there is cloudqueue.createifnotexists (MSDN), but I cannot find the equivalent in Azure PowerShell.

What is the best way in PowerShell to create Azure storage queue if it does not already exist, otherwise get reference to the existing queue?

Harald F.
  • 4,505
  • 24
  • 29
Edward
  • 8,028
  • 2
  • 36
  • 43

2 Answers2

5

Afaik there is no CreateIfNotExist flag through the PowerShell module.

You can easily do the following to achieve the same:

$queue = Get-AzureStorageQueue -name 'myName' -Context $storeAuthContext
if(-not $queue){ 
    # your code to create the queue
}

If you want to surpress errors and always try to create it (regardless if it exists or not); you should be able to use the -ErrorAction SilentlyContinue when creating the queue.

I would recommend the first approach though as it's a better practice.

Harald F.
  • 4,505
  • 24
  • 29
  • It looks like the New-AzureStorageQueue is actually doing this under the hood. If you look at the code in GitHub when the CmdLet is called it performs a CreateIfNotExists on the call. (https://github.com/Azure/azure-powershell/blob/dev/src/Common/Storage/Commands.Storage/Queue/Cmdlet/NewAzureStorageQueue.cs) Note that this might look a little odd in your script code to always be using New-AzureStorageQueue and the code indicated by CmdrTchort is more readable. In theory you'd get the queue way more often than it not being there and need to be created. – MikeWo Feb 26 '16 at 12:08
  • @MikeWo - What you describe is how I expected it to work, but I got ResourceAlreadyExistsException when I tried it, hence my question. The answer posted by CmdrTchort works though. – Edward Feb 26 '16 at 13:23
  • @Edward Ah, yeah, because if I had looked at that code a little more I would have seen that are explicitly throwing the ResourceAlreadyExistException. DOH! – MikeWo Feb 26 '16 at 13:37
1

As of 2017.03.14, the accepted answer did not work in a Powershell Azure Function, the Get-AzureStorageQueue throws an exception if the specified queue does not exist.

Example:

Here's the code

$storageAccountName = $env:AzureStorageAccountName
Write-Output ("storageAccountName: {0}" -f $storageAccountName)
$storageAccountKey = $env:AzureStorageAccountKey
Write-Output ("storageAccountKey: {0}" -f $storageAccountKey)
$storageQueueName = $env:AzureStorageQueueName
Write-Output ("storageAccountKey: {0}" -f $storageAccountKey)

Write-Output "Creating storage context"
$azureStorageContext = New-AzureStorageContext $storageAccountName -    StorageAccountKey $storageAccountKey

Write-Output "Retrieving queue"
$azureStorageQueue = Get-AzureStorageQueue -Name $storageQueueName –Context $azureStorageContext

Here's the log

2017-03-15T04:16:57.021 Get-AzureStorageQueue : Can not find queue 'my-queue-name'.
at run.ps1: line 21
+ Get-AzureStorageQueue
+ _____________________
    + CategoryInfo          : ObjectNotFound: (:) [Get-AzureStorageQueue], ResourceNotFoundException
    + FullyQualifiedErrorId : ResourceNotFoundException,Microsoft.WindowsAzure.Commands.Storage.Queue.GetAzureStorageQueueCommand
2017-03-15T04:16:57.021 Function completed (Failure, Id=58f35998-ebe0-4820-ac88-7d6ca42833df)

Resolution

I had to filter the results and only create the queue if it didn't exist. here's how to solve it:

Write-Output "Retrieving queue"
# Get-AzureStorageQueue returns an exception if the queue does not exists when passing -Name, so instead
# we need to get them all, filter by Name, and if null, create it
$azureStorageQueue = Get-AzureStorageQueue –Context $azureStorageContext | Where-Object {$_.Name -eq $storageQueueName}
if ($azureStorageQueue -eq $null)
{
    Write-Output "Queue does not exist, creating it"
    $azureStorageQueue = New-AzureStorageQueue -Name $storageQueueName -Context $azureStorageContext
}
Rick Glos
  • 2,466
  • 2
  • 29
  • 30