在批量插入的 powershell 中找不到 "ExecuteBatch" 错误的重载

Cannot find an overload for "ExecuteBatch" error in powershell for batch insert

我正在使用 powershell 批量插入到 Azure table。使用最新的 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模块中使用吗?

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)
}