To access Azure Storage Tables, mainly for use in Azure Functions (PowerShell), I have been using the AzTable module. However, recently I ran into the issue that this does not appear to support signing in with Azure AD Credentials. This means that using a Managed Identity to access the Azure Storage Table from a function app is not possible.
It appears this module is depending upon Microsoft.Azure.Cosmos.Table cloudTable. As far as my understanding goes, this does not support the DefaultAzureCredential Class, though alternatively Azure.Data.Tables does appear to support this (in preview).
Since I have a requirement to be able to use Managed Identity for authorizing access to an Azure Storage Table I needed to find a solution. Because my .Net 'Fu' is basically non-existent, I decided to instead look into using the REST API provided to gain access instead.
I created this module to allow relatively simple manipulation of Azure Storage Tables. Bear in mind that I have currently only spot-tested the module and while it appears to do what is required, I do not give any guarantee on functionality. Use for your own risk!
If you want to give it a try, you can get it on github
When imported, this module will provide the following functions:
- Get-StorageTableRow
- Get-StorageTableNextRow
- Remove-StorageTableRow
- Add-StorageTableRow
- Update-StorageTableRow
It will allow you to use Table storage in:
- Azure Storage Account, using:
- Shared Key
- Current Signed-in Credentials
- Azurite Development Storage, using:
- Shared Key only
Every function requires a 'table' parameter. This parameter is an object of type Microsoft.WindowsAzure.Commands.Common.Storage.ResourceModel.AzureStorageTable. An object of this type can be obtained by using [Get-AzStorageTablehttps://docs.microsoft.com/en-us/powershell/module/az.storage/get-azstoragetable?view=azps-7.3.0) Every function requires a 'table' parameter. This parameter is an object of type Microsoft.WindowsAzure.Commands.Common.Storage.ResourceModel.AzureStorageTable. An object of this type can be obtained by using Get-AzStorageTable
For instance:
$ctx = New-AzStorageContext -StorageAccountName MyStorageAccount -UseConnectedAccount
$table = Get-AzStorageTable -Name MyTable -Context $ctx
In this example $table can be used in all the imported commands.
- table: object containing reference to the table and how to authorize (using shared key or using signed in credentials)
- partitionKey: string used for the partition used by this entity
- rowKey: string used for the Rowkey, which is a unique identifier for the row
- property: hashtable containing additional columns to be inserted. See the Microsoft Documentation for more information
- returnContent: switch, if selected the command will return the inserted row
Used to insert rows into an Azure Storage Table. It will fail if you attempt to overwrite an existing row.
Example
Add-StorageTableRow -table $table -partitionKey 'MyPartitionKey' -rowKey 'MyRowKey' -property @{"[email protected]" = "Edm.Guid"; "CustomerCode" = "c9da6455-213d-42c9-9a79-3e9149a57833"}
- table: object containing reference to the table and how to authorize (using shared key or using signed in credentials)
- partitionKey: string used for the partition used by this entity
- rowKey: Rowkey is a unique identifier for the row
- property: hashtable containing additional columns to be inserted. See the Microsoft Documentation for more information
Used to insert or update a row into an Azure Storage Table.
Example
Update-StorageTableRow -table $table -partitionKey 'MyPartitionKey' -rowKey 'MyRowKey' -property @{"[email protected]" = "Edm.Guid"; "CustomerCode" = "c9da6455-213d-42c9-9a79-3e9149a57833"}
- table: object containing reference to the table and how to authorize (using shared key or using signed in credentials)
- partitionKey: string used for the partition used by this entity
- rowKey: Rowkey is a unique identifier for the row. Can only be used in combination with partionKey
- customFilter: custom odata filter to select records from the Azure Storage Table. Cannot be used together with partitionKey or rowKey parameters
- selectColumn: comma separated list of columns that should be returned
- top: integer, maximum number of rows to be returned at once. If more records are found, a paginationQuery property will be added, which can be used by Get-StorageTableNextRow to retrieve the next set of records
Used to retrieve records from the Azure Storage Table
Example: Retrieve all records
Get-StorageTableRow -table $table
Example: Retrieve records with partitionKey
Get-StorageTableRow -table $table -partitionKey "MyPartition"
Example: Retrieve records with partitionKey and rowKey
Get-StorageTableRow -table $table -rowKey "MyRowKey1" -partitionKey "MyPartition"
Example: Retrieve records with a custom odata filter
Get-StorageTableRow -table $table -customFilter "RowKey eq 'MyRowKey1'"
Example: Retrieve the first 10 records (can be combined with all the other options)
Get-StorageTableRow -table $table -top 10
Note: top just limits the resultset to the first x records returned and can be used to paginate results
Example: Select columns which are returned (can be combined with all the other options)
Get-StorageTableRow -table $table -partitionKey "MyPartition" -selectColumn RowKey
Note: selectColumn is case sensitive. rowKey(wrong) and RowKey(correct) are not the same!
- table: object containing reference to the table and how to authorize (using shared key or using signed in credentials)
- paginationQuery: property of a paginated resultset, which contains the original filter and RowKey and PartitionKey for the next resultset
Example: Retrieve the first 10 records (can be combined with all the other options)
# Retrieve the first set of results
$result = Get-StorageTableRow -table $table -top 10
# Retrieve the next set of results
$result = Get-StorageTableNextRow -table $table -paginationQuery $result.paginationQuery
As long as the result includes a paginationQuery property, more results can be retrieved
- table: object containing reference to the table and how to authorize (using shared key or using signed in credentials)
- partitionKey: string used for the partition used by this entity
- rowKey: Rowkey is a unique identifier for the row
Used to remove a row from the Azure Storage Table
Example
Remove-StorageTableRow -table $table -partitionKey 'MyPartitionKey' -rowKey 'MyRowKey'