使用 Powershell 脚本将 Azure 存储表从一个存储复制到另一个存储表时出错

问题描述 投票:0回答:1

使用 Powershell 脚本将 Azure 存储表从一个存储复制到另一个存储表时遇到错误“InvalidOperation:方法调用失败,因为 [System.String] 不包含名为“ExecuteQuerySegmentedAsync”的方法

#Set Variables to define source and destination values
$sourcestorageaccountname = "stlpttoragetst"
$sourceresourcegroupname = "RG_SUPPLY__ADF_TEST_AUE"
$destinationstorageaccountname = "stlpestorage"
$destinationresourcegroupname = "RG_SUPPLY_DEV_AUE"
$sourcetablename = "DataConfig" 
$desttablename =  "TestTable"

#Get storage account keys
$sourcestorageaccount = Get-AzStorageAccount -ResourceGroupName $sourceresourcegroupname -Name $sourcestorageaccountname
$destinationstorageaccount = Get-AzStorageAccount -ResourceGroupName $destinationresourcegroupname -Name $destinationstorageaccountname
 
$sourcekey = "$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$g=="
$destinationkey = "$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$"  

#Function to create CloudTable refernce
function Get-CloudTable {
    param (
        [string]$accountName,
        [string]$accountKey,
        [string]$tableName
    )
    $storageContext = New-AzStorageContext -StorageAccountName $accountName -StorageAccountKey $accountKey

    #Get Cloud Table Refernce
    $table = Get-AzStorageTable -Name $tableName -Context $storageContext

    return $table
}

#Get Source table and Destination table refernce 
$sourceTable = Get-CloudTable -accountName $sourcestorageaccountname -accountKey $sourcekey -tableName $sourceTablename
$destinationTable = Get-CloudTable -accountName $destinationstorageaccountname -accountKey $destinationkey -tableName $desttablename





#Copy Entity from Source Table to Destination table 
$entities = Get-AzTableRowAll -table $sourcetablename

foreach ($entity in $entities){
#     #Insert or replace entity in destination table
       $insertOperation = Add-AzTableRow -Table $desttablename -partitionKey $entity.PartitionKey -RowKey $entity.RowKey -property $entity.Properties

    }
 Write-Output "Table Copied Completed"
azure powershell azure-storage
1个回答
0
投票

使用 Powershell 脚本将 Azure 存储表从一个存储复制到另一个存储表时出错。

您可以使用以下脚本将表实体从源复制到另一个存储帐户表(使用

PowerShell 7
)。

您可以看到我存储在源表存储帐户中的以下实体。

传送门: enter image description here

脚本:

function Copy-AzureTable ($SourceResourceGroupName, $SourceStorageAccountName, $SourceTable, $DestinationResourceGroupName, $DestinationStorageAccountName, $ExistingDestinationTable, $SourceAccountKey, $DestinationAccountKey) {
   
    $SourceCtx = New-AzStorageContext -StorageAccountName $SourceStorageAccountName -StorageAccountKey $SourceAccountKey
    $SourceCloudTable = (Get-AzStorageTable -Name $SourceTable -Context $SourceCtx).CloudTable

    $DestinationCtx = New-AzStorageContext -StorageAccountName $DestinationStorageAccountName -StorageAccountKey $DestinationAccountKey

    # Check if the destination table exists
    try {
        $DestinationCloudTable = (Get-AzStorageTable -Name $ExistingDestinationTable -Context $DestinationCtx).CloudTable
    } catch {
        Write-Error "Destination table '$ExistingDestinationTable' does not exist in the destination storage account."
        return
    }
    
    $SourceCloudTableContent = Get-AzTableRow -Table $SourceCloudTable
    $SourceCloudTableContent | ForEach-Object -ThrottleLimit 500 -Parallel {
        $DestinationCloudTableUpdate = $using:DestinationCloudTable
        $_ | Update-AzTableRow -Table $DestinationCloudTableUpdate
    }
}

Copy-AzureTable "SourceResourceGroup" "SourceStorageAccountName" "SourceTable" "DestinationResourceGroup" "DestinationStorageAccountName" "ExistingDestinationTable" "SourceAccountKey" "DestinationAccountKey"

输出:

Result         : Microsoft.Azure.Cosmos.Table.DynamicTableEntity
HttpStatusCode : 204
Etag           : W/"datetime'2024-10-17T07%3A29%3A59.4901773Z'"
SessionToken   :
RequestCharge  :
ActivityId     :

Result         : Microsoft.Azure.Cosmos.Table.DynamicTableEntity
HttpStatusCode : 204
Etag           : W/"datetime'2024-10-17T07%3A29%3A59.7052318Z'"
SessionToken   :
RequestCharge  :
ActivityId     :

enter image description here

传送门: enter image description here

上述脚本将实体从源表存储复制到目标表存储帐户。

© www.soinside.com 2019 - 2024. All rights reserved.