我正在使用 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' 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
在 .NET Azure 存储 API 中,有 cloudqueue.createifnotexists (MSDN) ,但我在 Azure PowerShell 中找不到等效项。
如果 Azure 存储队列尚不存在,则在 PowerShell 中创建创建队列的最佳方法是什么,否则获取对现有队列的引用?
最佳答案
据我所知,PowerShell 模块没有 CreateIfNotExist 标志。
您可以轻松地执行以下操作来实现相同的目的:
$queue = Get-AzureStorageQueue -name 'myName' -Context $storeAuthContext
if(-not $queue){
# your code to create the queue
}
如果你想抑制错误并总是尝试创建它(无论它是否存在);创建队列时,您应该能够使用 -ErrorAction SilentlyContinue 。
我会推荐第一种方法,因为这是一种更好的做法。
关于powershell - Azure PowerShell - 如何创建队列(如果不存在),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35650453/