powershell - Azure PowerShell - 如何创建队列(如果不存在)

标签 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' 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/

相关文章:

.net - System.IO.Compression.DeflateStream 的数据格式

powershell - 如何为使用动态参数的高级函数 cmdlet 创建包装器

wordpress - 容器 Web 应用程序 - Wordpress Https

AzureDevOps : Pass task variable to subsequent powershell task type as Object

python - 如何将依赖项安装到 Azure Pipeline Python

powershell - 为什么用单个字符而不是单个字符串填充PSCustomObject?

excel - 使用 powershell 刷新 Excel 工作表

c# - 在 azure blob 存储中设置 Ionic Zip 密码,并为 zip 文件夹而不是文件设置最佳压缩

Azure OpenAI 自带数据 - 语义搜索不起作用

azure - 新-AzResourceLock : A positional parameter cannot be found that accepts argument