2016-02-26 33 views
1

我使用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存儲隊列,如果它不存在,否則得到參照現有的隊列?

回答

3

據我所知沒有通過PowerShell的模塊沒有CreateIfNotExist標誌。

您可以輕鬆地執行以下操作來實現相同的:

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

如果你想surpress錯誤,並總是試圖創建它(不管它存在與否);你應該能夠在創建隊列時使用-ErrorAction SilentlyContinue。

我會建議,雖然,因爲它是一個更好的做法,第一種方法。

+0

它看起來像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

+0

@MikeWo - 你描述的是我期望它的工作方式,但是當我嘗試它時,我得到了ResourceAlreadyExistsException,因此我的問題。 CmdrTchort公佈的答案雖然工作。 – Edward

+0

@愛德華啊,是的,因爲如果我多看了一段代碼,我會發現它明確地拋出了ResourceAlreadyExistException異常。 DOH! – MikeWo

0

從2017.03.14起,接受的答案在Powershell Azure Function中不起作用,如果指定的隊列不存在,則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 
} 
+0

謝謝,過濾是我失蹤的部分 – Thomas