2014-10-02 199 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