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"
2
Answers
We face error while assigning variable to keys so we hardcoded them for the mean while tested on powershell 7
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:
Script:
Output:
Portal:
The above script copied entities from source table storage to destination table storage account.