首页 文章

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

提问于
浏览
2

我使用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:队列'myqueue'已经存在 . 在行:1 char:12 $ myQueue = New-AzureStorageQueue -Name'myqueue'-Context $ storeAuthContext ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~分类信息:ResourceExists :( :) [New-AzureStorageQueue],ResourceAlreadyExistException FullyQualifiedErrorId:ResourceAlreadyExistException,Microsoft.WindowsAzure.Commands.Storage.Queue.NewAzureStorageQueueCommand

在.NET Azure Storage API中有cloudqueue.createifnotexists (MSDN),但我找不到Azure PowerShell中的等效项 .

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

2 回答

  • 1

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

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

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

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

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

  • 4

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

    Resolution

    我不得不过滤结果,只创建队列(如果它不存在) . 这是如何解决它:

    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
    }
    

相关问题