Last active
February 2, 2022 16:41
-
-
Save dzimchuk/1e45174d3a63705b9171 to your computer and use it in GitHub Desktop.
PowerShell script to copy Azure Table from one account to another
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
param ( | |
[string] | |
$sourceConnectionString = $(throw "-sourceConnectionString is required."), | |
[string] | |
$sourceTableName = $(throw "-sourceConnectionString is required."), | |
[string] | |
$targetConnectionString = $(throw "-targetConnectionString is required."), | |
[string] | |
$targetTableName = $(throw "-targetTableName is required.") | |
) | |
function Insert-Records | |
{ | |
param ($table, $entities) | |
$batches = @{} | |
foreach ($entity in $entities) | |
{ | |
if ($batches.ContainsKey($entity.PartitionKey) -eq $false) | |
{ | |
$batches.Add($entity.PartitionKey, (New-Object Microsoft.WindowsAzure.Storage.Table.TableBatchOperation)) | |
} | |
$batch = $batches[$entity.PartitionKey] | |
$batch.Add([Microsoft.WindowsAzure.Storage.Table.TableOperation]::InsertOrReplace($entity)); | |
if ($batch.Count -eq 100) | |
{ | |
$table.ExecuteBatch($batch); | |
$batches[$entity.PartitionKey] = (New-Object Microsoft.WindowsAzure.Storage.Table.TableBatchOperation) | |
} | |
} | |
foreach ($batch in $batches.Values) | |
{ | |
if ($batch.Count -gt 0) | |
{ | |
$table.ExecuteBatch($batch); | |
} | |
} | |
} | |
function Copy-Records | |
{ | |
param($sourceTable, $targetTable) | |
$tableQuery = New-Object 'Microsoft.WindowsAzure.Storage.Table.TableQuery' | |
[Microsoft.WindowsAzure.Storage.Table.TableContinuationToken]$token = $null | |
do | |
{ | |
$segment = $sourceTable.ExecuteQuerySegmented($tableQuery, $token); | |
$token = $segment.ContinuationToken | |
Insert-Records $targetTable $segment.Results | |
$count = $segment.Results.Count | |
Write-Host "Copied $count records" | |
} while ($token -ne $null) | |
} | |
function Get-Table | |
{ | |
param($storageContext, $tableName, $createIfNotExists) | |
$table = Get-AzureStorageTable $tableName -Context $storageContext -ErrorAction Ignore | |
if ($table -eq $null) | |
{ | |
if($createIfNotExists -eq $false) | |
{ | |
return $null | |
} | |
$table = New-AzureStorageTable $tablename -Context $storageContext | |
} | |
return $table.CloudTable | |
} | |
$sourceContext = New-AzureStorageContext -ConnectionString $sourceConnectionString | |
$targetContext = New-AzureStorageContext -ConnectionString $targetConnectionString | |
$sourceTable = Get-Table $sourceContext $sourceTableName $false | |
if ($sourceTable -eq $null) | |
{ | |
Write-Host "Source table $sourceTableName was not found" | |
return | |
} | |
$targetTable = Get-Table $targetContext $targetTableName $true | |
Copy-Records $sourceTable $targetTable |
Thank you so much, this script saved a lot of time.
Taking to much time to copy the entities, my test result is 100 entities per 30 seconds.
How can I improve copying rate?
Beautiful! Saved lots of time. Note: for more recent version of Azure Powershell youll need to replace the Windows Table with Microsoft.Azure.Cosmos.Table
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you, great job! I was able to import it to a runbook workflow in my Azure Automation Account and it worked great.