Azure PowerShell - 如果不存在,如何创建队列

时间:2016-02-26 11:24:32

标签: powershell azure azure-powershell azure-queues

我正在使用Azure PowerShell创建条目并将条目添加到Azure队列,如下所示:MSDN: Using Azure PowerShell with Azure Storage - How to manage Azure queues .

这是我的PowerShell脚本:

$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)

第一次运行它时效果很好。

第二次,我明白了:

  

New-AzureStorageQueue:Queue' myqueue'已经存在。在线:1   焦炭:12   + $ myQueue = New-AzureStorageQueue -Name' myqueue' -Context $ storeAuthContext   + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~       + CategoryInfo:ResourceExists:(:) [New-AzureStorageQueue],ResourceAlreadyExistException       + FullyQualifiedErrorId:ResourceAlreadyExistException,Microsoft.WindowsAzure.Commands.Storage.Queue.NewAzureStorageQueueCommand

在.NET Azure Storage API中有cloudqueue.createifnotexists (MSDN),但我在Azure PowerShell中找不到相应的内容。

PowerShell创建Azure存储队列的最佳方法是什么(如果尚不存在),否则会引用现有队列?

2 个答案:

答案 0 :(得分:5)

Afaik通过PowerShell模块没有CreateIfNotExist标志。

您可以轻松地执行以下操作来实现相同目标:

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

如果你想克服错误并总是试图创造它(无论它是否存在);您应该能够在创建队列时使用-ErrorAction SilentlyContinue。

我会推荐第一种方法,因为这是一种更好的做法。

答案 1 :(得分:1)

截至2017.03.14,接受的答案在Powershell Azure函数中不起作用,如果指定的队列不存在,Get-AzureStorageQueue会抛出异常。

示例:

这是代码

$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

这是日志

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)

解决

我必须过滤结果,只有在队列不存在时才创建队列。这是如何解决它:

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
}