0

I have an Azure DevOps Deployment Pipeline with an Azure PowerShell Script Task.

The script task populates an Azure Storage Table. It gets the data from a json file.

Unfortunately, my client keeps on changing their mind with what they want in the table.

So, I thought the best thing to do would be to delete the table, recreate it and repopulate it with the new data.

But, I get a conflict error if I run the Remove and Create back-to-back. Which makes sense since it seems so much of Azure uses a messaging architecture.

Anyway, does anyone have a solution to this issue? I have a few, but they seem jerry-rigged.

Here a subset of the codde:

$storageAccount = Get-AzStorageAccount -ResourceGroupName $ResourceGroupName -AccountName $StorageAccountName

$storageTable = Get-AzStorageTable `
    -Name $tableName `
    -Context $storageAccount.Context `
    -ErrorVariable ev `
    -ErrorAction SilentlyContinue

if($storageTable) {
    Remove-AzStorageTable -Name $tableName -Context $storageAccount.Context -Force     
}

New-AzStorageTable -Name $tableName -Context $storageAccount.Context

$storageTable = Get-AzStorageTable -Name $tableName -Context $storageAccount.Context

Any suggestions would be appreciated.

Richard
  • 1,054
  • 1
  • 19
  • 36

1 Answers1

2

I have done a few modifications to your code. Please follow the below snippet

$storageAccount = Get-AzStorageAccount -ResourceGroupName $ResourceGroupName -AccountName $StorageAccountName

$storageTable = Get-AzStorageTable `
    -Name $tableName `
    -Context $storageAccount.Context `
    -ErrorVariable ev `
    -ErrorAction SilentlyContinue
# if you dont have the table it will gives the $ev so you have to capture this and if table is not there we have to create
if ($ev) {
    New-AzStorageTable -Name $tableName -Context $storageAccount.Context
    $isDeleted = $false
    write-host("Table is not there it is created")
}
# if table exist you can remove the table
elseif($storageTable.Name -eq $tableName) {
    Remove-AzStorageTable -Name $tableName -Context $storageAccount.Context
    $isDeleted = $true
    write-host("Table is there so it is Removed")
    # After deleting table we have to wait for 40 sec to completely delete the table from Garbage. Mean while we cannot access the table storage.
    Start-Sleep -Seconds 40 
}


if($isDeleted -eq $true)
{
    
    New-AzStorageTable -Name $tableName -Context $storageAccount.Context
    write-host("Table is Removed so it is created")
}

$storageTable = Get-AzStorageTable -Name $tableName -Context $storageAccount.Context

After Deleting/Removing a table it is immediately marked for deletion and is no longer accessible to clients is likely to take at least 40 seconds to complete on that time it will be in a Garbage collection. Refer here

Results:

enter image description here

Delliganesh Sevanesan
  • 4,146
  • 1
  • 5
  • 15