azurepowershellazure-storage

Error while copying Azure Storage table from one Storage to another storage table using Powershell script


Facing Error while copying Azure Storage table from one Storage to another storage table using Powershell script "InvalidOperation: Method invocation failed because [System.String] does not contain a method named '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"

Solution

  • Error while copying Azure Storage table from one Storage to another storage table using Powershell script.

    You can use the below script to copy table entities from source to another storage account table using PowerShell 7.

    You can see the below entities which I stored in source table storage account.

    Portal: enter image description here

    Script:

    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"
    

    Output:

    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

    Portal: enter image description here

    The above script copied entities from source table storage to destination table storage account.