2014-10-02 126 views
0

偶爾當我嘗試在Azure表存儲中插入數據時,出現以下錯誤。如何在PowerShell中處理Azure存儲表操作錯誤?

Exception calling "Execute" with "1" argument(s): "The remote server returned an error: (409) Conflict." 

理論上,當我使用已存在的分區鍵和行鍵插入數據時,可能會發生這種情況。

因爲我使用ticks(一個tick是一個十萬分之一秒)的分區鍵和行鍵我不認爲這應該是這種情況。下面的例子。

但無論如何,我該如何防止CloudTable.Execute的異常?因爲我設置了$ErrorActionPreference = "Stop"它會停止我的安裝腳本。我不認爲我可以使用-ErrorAction Ignore

function Corax-Create-Table($tablename) 
{ 
    $context = New-AzureStorageContext -StorageAccountName $g_storage_account_name -StorageAccountKey $g_storage_account_key 
    $table = Get-AzureStorageTable $tablename -Context $context 
    if ($table -eq $null) 
    { 
     New-AzureStorageTable $tablename -Context $context 
    } 
    return $table 
} 

function Corax-Storage-Insert-Message($table, [String]$partitionKey, [String]$rowKey, [String]$category, [String]$level, [String]$message) 
{ 
    $entity = New-Object "Microsoft.WindowsAzure.Storage.Table.DynamicTableEntity" $partitionKey, $rowKey 
    $entity.Properties.Add("Category", $category) 
    $entity.Properties.Add("Level", $level) 
    $entity.Properties.Add("Message", $message) 
    $result = $table.CloudTable.Execute([Microsoft.WindowsAzure.Storage.Table.TableOperation]::Insert($entity)) 
} 

$session = $(Get-Date).Ticks 

$t = Corax-Create-Table('test') 
Corax-Storage-Insert-Message $t $session $(Get-Date).Ticks "Database" "Progress" "Database table 1 is updated" 
Corax-Storage-Insert-Message $t $session $(Get-Date).Ticks "Database" "Progress" "Database table 2 is updated" 
Corax-Storage-Insert-Message $t $session $(Get-Date).Ticks "Database" "Progress" "Database table 3 is updated" 
Corax-Storage-Insert-Message $t $session $(Get-Date).Ticks "Database" "Progress" "Database table 4 is updated" 
Corax-Storage-Insert-Message $t $session $(Get-Date).Ticks "Database" "Progress" "Database table 5 is updated" 
+0

是否可以使用菲德勒看到要Azure存儲表服務的實際要求和確認你正在使用的PK/RK組合? – 2014-10-02 14:03:29

+0

好主意,但這隻會在客戶現場發生一次,所以這不是一個可行的選擇。 – 2014-10-02 14:27:45

+0

那麼可以通過分析這些日誌來啓用Azure存儲分析日誌記錄(http://azure.microsoft.com/zh-CN/documentation/articles/storage-monitor-storage-account/)並查看失敗的請求? – 2014-10-02 16:09:35

回答

0

你有沒有嘗試try/catch

function Corax-Storage-Insert-Message($table, [String]$partitionKey, [String]$rowKey, [String]$category, [String]$level, [String]$message) 
{ 
    $ErrorActionPreference = "Stop" 
    try{ 
     $entity = New-Object "Microsoft.WindowsAzure.Storage.Table.DynamicTableEntity" $partitionKey, $rowKey 
     $entity.Properties.Add("Category", $category) 
     $entity.Properties.Add("Level", $level) 
     $entity.Properties.Add("Message", $message) 
     $result = $table.CloudTable.Execute([Microsoft.WindowsAzure.Storage.Table.TableOperation]::Insert($entity)) 
    } 
    catch{  
     ## error handling code  
    } 
} 

在catch你可以記錄錯誤或用遞增的鑰匙再次執行您的來電:

Corax-Storage-Insert-Message $table $partitionKey [String]([int]$rowKey + 1) $category $level $message