DevOps | Scripts | Automation

AzurePowershell

Azure Table Storage with PowerShell

Azure Table Storage: Overview

In this article, we’ll be discussing how to manage tables using PowerShell in Azure table storage. Although we won’t be providing a detailed explanation of what Azure table storage is or how it works. It’s worth noting that this storage solution is capable of storing significant amounts of structured data.

You can refer to the link below to learn more about storage tables.

https://learn.microsoft.com/en-us/azure/storage/tables/table-storage-overview

Table Reference:

  • Create a Storage Account table with PowerShell
  • Add table entry with PowerShell
  • Retrieve table entries with PowerShell
  • Update Table entry with PowerShell
  • Delete table entry with PowerShell.

a. Create a table Storage account with PowerShell

  • To create a table storage account using PowerShell, you must first have an existing Azure storage account. We won’t be covering how to create a storage account here, but rather the steps for creating a table within the account.

    You can refer to this link: Create a storage account with PowerShell.
  • Assuming, you have the storage account ready. Now open the PowerShell terminal and authenticate. Please read this article to know more about it.
  • Install AzTables module. It doesn’t install default with Az Module.
Install-Module AzTable -verbose
  • Set your Azure context (Subscription) to work with.
Set-AzContext -Subscription (Subscription Name or ID) -ErrorAction Stop
  • Create Azure Storage context.
$strgcontext = (Get-AzStorageAccount -StorageAccountName StorageAccountName -ResourceGroupName StorageAccountRG).Context
  • Now create a new table storage table (Here CarsTable which will store the car name and its Owner).
New-AzStorageTable -Name CarsTable -Context $strgcontext
  • To check whether the table is created,
get-azStorageTable -Context $strgcontext

Great job! You have successfully created the CarsTable. In order to carry out any operations with this table, we must refer to the cloud property of the table. This is a necessary step when working with the table data using AzTable.

To reference the cloud property use the CloudTable property of the context.

$cloudtable = (Get-AzStorageTable -Name CarsTable -Context $strgcontext).CloudTable


b. Add Entries to Azure table storage

The table has been successfully created and we now need to add entries to the Azure storage table. To accomplish this, let’s look into using PowerShell.

In order to add a new entry to the table, we must utilize the Add-AzTableRow cmdlet which mandates the provision of the partition key and row key to create. Let us proceed to add a single entry to the table.

Add-AzTableRow -Table $cloudTable `
      -PartitionKey "Honda" `
      -RowKey ("BG") `
      -property @{"Owner"="Bill Gates";"Model"="City";"Year"=2021}

Output:

To ensure a successful entry, the HttpsStatusCode should be 204. You can find more information on the status and error codes in this article.

Let’s add another entry with a different partition key.

Add-AzTableRow -Table $cloudTable `
      -PartitionKey "Ford" `
      -RowKey ("EM") `
      -property @{"Owner"="Elon Musk";"Model"="Figo";"Year"=2018}


c. Retrieve table entries with PowerShell

We have added new entries to the Azure table storage. To view these entries using PowerShell, use the following command which will provide access to all of them.

Get-AzTableRow -table $cloudtable | ft

To get entries based on the Partition Key.

 Get-AzTableRow -table $cloudtable -PartitionKey Ford | ft

Please note that the values are case-sensitive. You have the option to filter entries as well. If you require only one specific entry, please ensure that you filter it using a unique property such as RowKey. This will assist us in modifying or deleting a single entry that has been discussed in this article.

Get-AzTableRow -table $cloudtable | where{$_.Model -eq 'City'}


d. Update Table entry with PowerShell

If you need to modify an existing entry, you can utilize the Update-AzTableRow command.

In order to modify the year of a Ford Figo car property, we must first locate the row corresponding to that specific vehicle.

$data = Get-AzTableRow -Table $cloudtable | where{$_.RowKey -eq 'EM'}

Now update the entry.

$data.Year = 2021
$data | Update-AzTableRow -Table $cloudtable

Once you are done with the changes, check if the entry is updated.



e. Delete Table entry with PowerShell

To delete the entry with Powershell, you can use Remove-AzTableRow. This process is similar to updating the table entry. First, get the entry to delete and remove it.

$data = Get-AzTableRow -Table $cloudtable | where{$_.RowKey -eq 'EM'}
$data | Remove-AzTableRow -Table $cloudtable

Now, let’s get all the table entries and see if the value is deleted.


Conclusion

If you need to handle large amounts of structured data and prefer not to use offline files such as Excel or CSV, Azure table storage is a faster and more advantageous option.

Loading