【问题标题】:Cannot find an overload for "ExecuteBatch" error in powershell for batch insert在用于批量插入的 powershell 中找不到“ExecuteBatch”错误的重载
【发布时间】:2020-02-28 09:30:11
【问题描述】:

我正在使用 powershell 对 Azure 表进行批量插入。使用最新的 Az 模块,而不是 AzureRm。

$context = New-AzStorageContext $storageAccountName -StorageAccountKey $storageAccountKey
$table = (Get-AzStorageTable –Name myTable –Context $context)

foreach($item in $items){
    [Microsoft.WindowsAzure.Storage.Table.TableBatchOperation]$batchOperation = New-Object -TypeName Microsoft.WindowsAzure.Storage.Table.TableBatchOperation
    $entity = New-Object -TypeName Microsoft.WindowsAzure.Storage.Table.DynamicTableEntity -ArgumentList $partitionKey, $rowKey
    $entity.Properties.Add("ID", $id)
    $batchOperation.InsertOrReplace($entity)
}
if ($batchOperation.Count -ne 0) {
    $table.CloudTable.ExecuteBatch($batchOperation)
}

但我得到的错误是:

Cannot find an overload for "ExecuteBatch" and the argument count: "1"

“ExecuteBatch”这个方法只能在旧的 AzureRm 模块中使用吗?

【问题讨论】:

    标签: powershell azure-table-storage


    【解决方案1】:

    ExecuteBatch 操作绝对可用。

    我相信您收到此错误是因为您使用了不正确的命名空间。您应该使用Microsoft.Azure.Cosmos.Table 而不是Microsoft.WindowsAzure.Storage.Table

    请尝试以下代码。我试过了,效果很好:

    $storageAccountName = "account-name";
    $storageAccountKey = "account-key=="
    
    $context = New-AzStorageContext $storageAccountName -StorageAccountKey $storageAccountKey
    $table = (Get-AzStorageTable –Name myTable –Context $context)
    
    foreach($item in $items){
        [Microsoft.Azure.Cosmos.Table.TableBatchOperation]$batchOperation = New-Object -TypeName Microsoft.Azure.Cosmos.Table.TableBatchOperation
        $entity = New-Object -TypeName Microsoft.Azure.Cosmos.Table.DynamicTableEntity -ArgumentList $partitionKey, $rowKey
        $entity.Properties.Add("ID", $id)
        $batchOperation.InsertOrReplace($entity)
    }
    if ($batchOperation.Count -ne 0) {
        $table.CloudTable.ExecuteBatch($batchOperation)
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-11-30
      • 2012-10-26
      • 2016-05-23
      • 1970-01-01
      • 1970-01-01
      • 2021-03-03
      • 1970-01-01
      相关资源
      最近更新 更多