如何在PowerShell中处理Azure Storage TableOperation错误?

如何在PowerShell中处理Azure Storage TableOperation错误?,powershell,error-handling,azure-storage,Powershell,Error Handling,Azure Storage,当我尝试在Azure表存储中插入数据时,偶尔会出现以下错误 Exception calling "Execute" with "1" argument(s): "The remote server returned an error: (409) Conflict." 理论上,当我插入具有已存在的分区键和行键的数据时,可能会发生这种情况 因为我对分区键和行键都使用记号(一个记号是一千万分之一秒),所以我认为情况不应该是这样。下面的例子 但无论如何,如何防止CloudTable.Execute上

当我尝试在Azure表存储中插入数据时,偶尔会出现以下错误

Exception calling "Execute" with "1" argument(s): "The remote server returned an error: (409) Conflict."
理论上,当我插入具有已存在的分区键和行键的数据时,可能会发生这种情况

因为我对分区键和行键都使用记号(一个记号是一千万分之一秒),所以我认为情况不应该是这样。下面的例子

但无论如何,如何防止
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"

你试过了吗

函数Corax存储插入消息($table,[String]$partitionKey,[String]$rowKey,[String]$category,[String]$level,[String]$Message)
{
$ErrorActionPreference=“停止”
试一试{
$entity=新对象“Microsoft.WindowsAzure.Storage.Table.DynamicTableEntity”$partitionKey,$rowKey
$entity.Properties.Add(“Category”,$Category)
$entity.Properties.Add(“级别”,$Level)
$entity.Properties.Add(“Message”,$Message)
$result=$table.CloudTable.Execute([Microsoft.WindowsAzure.Storage.table.TableOperation]::Insert($entity))
}
捕获{
##错误处理代码
}
}
在catch中,您可以记录错误或使用递增键再次执行调用:

Corax存储插入消息$table$partitionKey[String]([int]$rowKey+1)$category$level$Message

是否可以使用Fiddler查看到Azure Storage Table服务的实际请求,并确认您正在使用的PK/RK组合?好主意,但这种情况在客户站点偶尔发生一次,因此这不是一个可行的选项。然后是否可以启用Azure Storage Analytics日志记录()并通过分析这些日志来查看失败的请求?谢谢,我已经启用了日志记录。但是,回到最初的问题:是否有方法捕获
CloudTable.Execute
异常?