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

Azure PowerShell-如果队列不存在,如何创建队列,powershell,azure,azure-powershell,azure-queues,Powershell,Azure,Azure Powershell,Azure Queues,我正在使用Azure PowerShell创建条目并将其添加到Azure队列,如下示例: 以下是我的PowerShell脚本: $storeAuthContext = New-AzureStorageContext -StorageAccountName '[my storage account name]' -StorageAccountKey '[my storage account key' $myQueue = New-AzureStorageQueue –Name 'myqueue'

我正在使用Azure PowerShell创建条目并将其添加到Azure队列,如下示例:

以下是我的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)
这在我第一次运行它时效果很好

第二次,我明白了:

新AzureStorageQueue:队列“myqueue”已存在。第1行 字符:12 +$myQueue=New AzureStorageQueue–名称'myQueue'-Context$storeAuthContext + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +CategoryInfo:ResourceExists:(:)[New AzureStorageQueue],ResourceReadyExistException +FullyQualifiedErrorId:ResourceAlreadyExistException,Microsoft.WindowsAzure.Commands.Storage.Queue.NewAzureStorageQueueCommand

在.NET Azure存储API中有,但我在Azure PowerShell中找不到等效的API


如果Azure存储队列不存在,那么在PowerShell中创建该队列的最佳方法是什么?否则,请获取对现有队列的引用。

Afaik PowerShell模块中没有CreateIfNotExist标志

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

$queue = Get-AzureStorageQueue -name 'myName' -Context $storeAuthContext
if(-not $queue){ 
    # your code to create the queue
}
如果您想要超越错误并始终尝试创建它(无论它是否存在);您应该能够在创建队列时使用-ErrorAction SilentlyContinue


我会推荐第一种方法,因为它是一种更好的实践

截至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
}

看起来新的AzureStorageQueue实际上是在幕后进行这项工作的。如果在调用CmdLet时查看GitHub中的代码,它会在调用中执行CreateIfNotExists。()请注意,在脚本代码中始终使用新的AzureStorageQueue可能会显得有点奇怪,而且CmdrTchort指示的代码更具可读性。从理论上讲,你会比没有排队的时候更容易得到排队方式,因此需要创建。@MikeWo-你描述的是我期望它如何工作,但当我尝试它时,我得到了ResourceReadyExistsException,因此我的问题是。CmdrTchort发布的答案仍然有效。@Edward啊,是的,因为如果我再多看一点代码,我就会看到它显式抛出ResourceReadyExistException。啊!谢谢,过滤是我遗漏的部分。另外,只需将Get-AzStorageQueue放入try/catch和catch调用New-AzStorageQueue即可