我使用Azure的PowerShell創建並添加條目到Azure的隊列,下面的例子在這裏:MSDN: Using Azure PowerShell with Azure Storage - How to manage Azure queues .Azure中的PowerShell - 如何創建隊列如果NOT EXISTS
這裏是我的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 char:12 + $ myQueue = New-AzureStorageQueue -Name'myqueue'-Context $ storeAuthContext + ~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo:ResourceExists:(:) [新-AzureStorageQueue],ResourceAlreadyExistException + FullyQualifiedErrorId:ResourceAlreadyExistException,Microsoft.WindowsAzure.Commands.Storage.Queue.NewAzureStorageQueueCommand
在.NET Azure存儲API有cloudqueue.createifnotexists (MSDN),但我找不到在PowerShell中天青相當於。
什麼是PowerShell中的最好的方式來創建Azure存儲隊列,如果它不存在,否則得到參照現有的隊列?
它看起來像New-AzureStorageQueue實際上是在引擎蓋下。如果您在調用CmdLet時查看GitHub中的代碼,則會在調用時執行CreateIfNotExists。 (https://github.com/Azure/azure-powershell/blob/dev/src/Common/Storage/Commands.Storage/Queue/Cmdlet/NewAzureStorageQueue。cs) 請注意,在您的腳本代碼中,這可能看起來有點奇怪,因此始終使用New-AzureStorageQueue,CmdrTchort指示的代碼更具可讀性。從理論上講,你會比其他人更頻繁地排隊,需要創建隊列。 – MikeWo
@MikeWo - 你描述的是我期望它的工作方式,但是當我嘗試它時,我得到了ResourceAlreadyExistsException,因此我的問題。 CmdrTchort公佈的答案雖然工作。 – Edward
@愛德華啊,是的,因爲如果我多看了一段代碼,我會發現它明確地拋出了ResourceAlreadyExistException異常。 DOH! – MikeWo