Repository for a sample module to manipulate Azure Storage Table rows/entities.
For more information, please visit the following blog post: https://blogs.technet.microsoft.com/paulomarques/2017/01/17/working-with-azure-storage-tables-from-powershell/
This module supports Azure Storage Tables. Cosmos DB support was removed and it will have its own module.
-
In a Windows 10/2016 execute the following cmdlets in order to install required modules
Install-Module AzureRm.Storage -AllowClobber -Force Install-Module AzureRM.Profile -AllowClobber -Force Install-Module AzureRM.Resources -AllowClobber -Force Install-Module Azure.Storage -AllowClobber -Force
-
Install AzureRmStorageTable
Install-Module AzureRmStorageTable
Below you will get the help content of every function that is exposed through the AzureRmStorageTable module.
Gets a Table object, it can be from Azure Storage Table or Cosmos DB in preview support.
Get-AzureStorageTableTable -resourceGroup <String> -tableName <String> -storageAccountName <String>
Get-AzureStorageTableTable -resourceGroup <String> -tableName <String> -databaseName <String>
Gets a Table object, it can be from Azure Storage Table or Cosmos DB in preview support.
# Getting storage table object
$resourceGroup = "myResourceGroup"
$storageAccount = "myStorageAccountName"
$tableName = "table01"
$table = Get-AzureStorageTabletable -resourceGroup $resourceGroup -tableName $tableName -storageAccountName $storageAccount
# Getting Cosmos DB table object
$resourceGroup = "myResourceGroup"
$databaseName = "myCosmosDbName"
$tableName = "table01"
$table01 = Get-AzureStorageTabletable -resourceGroup $resourceGroup -tableName $tableName -databaseName $databaseName
Resource Group where the Azure Storage Account or Cosmos DB are located
Type: String
Parameter Sets: (All)
Aliases:
Required: True
Position: Named
Default value: None
Accept pipeline input: False
Accept wildcard characters: False
Name of the table to retrieve
Type: String
Parameter Sets: (All)
Aliases:
Required: True
Position: Named
Default value: None
Accept pipeline input: False
Accept wildcard characters: False
Storage Account name where the table lives
Type: String
Parameter Sets: AzureTableStorage
Aliases:
Required: True
Position: Named
Default value: None
Accept pipeline input: False
Accept wildcard characters: False
CosmosDB database where the table lives
Type: String
Parameter Sets: AzureCosmosDb
Aliases:
Required: True
Position: Named
Default value: None
Accept pipeline input: False
Accept wildcard characters: False
Adds a row/entity to a specified table
Add-StorageTableRow [-table] <AzureStorageTable> [-partitionKey] <String> [-rowKey] <String>
[-property] <Hashtable>
Adds a row/entity to a specified table
# Adding a row
$saContext = (Get-AzureRmStorageAccount -ResourceGroupName $resourceGroup -Name $storageAccount).Context
$table = Get-AzureStorageTable -Name $tableName -Context $saContext
Add-StorageTableRow -table $table -partitionKey $partitionKey -rowKey (\[guid\]::NewGuid().tostring()) -property @{"firstName"="Paulo";"lastName"="Costa";"role"="presenter"}
Table object of type Microsoft.WindowsAzure.Commands.Common.Storage.ResourceModel.AzureStorageTable where the entity will be added
Type: AzureStorageTable
Parameter Sets: (All)
Aliases:
Required: True
Position: 1
Default value: None
Accept pipeline input: False
Accept wildcard characters: False
Identifies the table partition
Type: String
Parameter Sets: (All)
Aliases:
Required: True
Position: 2
Default value: None
Accept pipeline input: False
Accept wildcard characters: False
Identifies a row within a partition
Type: String
Parameter Sets: (All)
Aliases:
Required: True
Position: 3
Default value: None
Accept pipeline input: False
Accept wildcard characters: False
Hashtable with the columns that will be part of the entity. e.g. @{"firstName"="Paulo";"lastName"="Marques"}
Type: Hashtable
Parameter Sets: (All)
Aliases:
Required: True
Position: 4
Default value: None
Accept pipeline input: False
Accept wildcard characters: False
Returns all rows/entities from a storage table - no filtering
Get-AzureStorageTableRowAll [-table] <AzureStorageTable>
Returns all rows/entities from a storage table - no filtering
# Getting all rows
$saContext = (Get-AzureRmStorageAccount -ResourceGroupName $resourceGroup -Name $storageAccount).Context
$table = Get-AzureStorageTable -Name $tableName -Context $saContext
Get-AzureStorageTableRowAll -table $table
Table object of type Microsoft.WindowsAzure.Commands.Common.Storage.ResourceModel.AzureStorageTable to retrieve entities
Type: AzureStorageTable
Parameter Sets: (All)
Aliases:
Required: True
Position: 1
Default value: None
Accept pipeline input: False
Accept wildcard characters: False
Returns one or more rows/entities based on a specified column and its value
Get-AzureStorageTableRowByColumnName [-table] <AzureStorageTable> [-columnName] <String> [-value] <String>
[-operator] <String>
Returns one or more rows/entities based on a specified column and its value
# Getting row by firstname
$saContext = (Get-AzureRmStorageAccount -ResourceGroupName $resourceGroup -Name $storageAccount).Context
$table = Get-AzureStorageTable -Name $tableName -Context $saContext
Get-AzureStorageTableRowByColumnName -table $table -columnName "firstName" -value "Paulo" -operator Equal
Table object of type Microsoft.WindowsAzure.Commands.Common.Storage.ResourceModel.AzureStorageTable to retrieve entities
Type: AzureStorageTable
Parameter Sets: (All)
Aliases:
Required: True
Position: 1
Default value: None
Accept pipeline input: False
Accept wildcard characters: False
Column name to compare the value to
Type: String
Parameter Sets: (All)
Aliases:
Required: True
Position: 2
Default value: None
Accept pipeline input: False
Accept wildcard characters: False
Value that will be looked for in the defined column
Type: String
Parameter Sets: (All)
Aliases:
Required: True
Position: 3
Default value: None
Accept pipeline input: False
Accept wildcard characters: False
Supported comparison operator. Valid values are "Equal","GreaterThan","GreaterThanOrEqual","LessThan" ,"LessThanOrEqual" ,"NotEqual"
Type: String
Parameter Sets: (All)
Aliases:
Required: True
Position: 4
Default value: None
Accept pipeline input: False
Accept wildcard characters: False
Returns one or more rows/entities based on custom filter.
Get-AzureStorageTableRowByCustomFilter [-table] <AzureStorageTable> [-customFilter] <String>
Returns one or more rows/entities based on custom filter. This custom filter can be built using the Microsoft.WindowsAzure.Storage.Table.TableQuery class or direct text.
# Getting row by firstname by using the class Microsoft.WindowsAzure.Storage.Table.TableQuery
$saContext = (Get-AzureRmStorageAccount -ResourceGroupName $resourceGroup -Name $storageAccount).Context
$table = Get-AzureStorageTable -Name $tableName -Context $saContext
Get-AzureStorageTableRowByCustomFilter -table $table -customFilter $finalFilter
# Getting row by firstname by using text filter directly (oData filter format)
$saContext = (Get-AzureRmStorageAccount -ResourceGroupName $resourceGroup -Name $storageAccount).Context
$table = Get-AzureStorageTable -Name $tableName -Context $saContext
Get-AzureStorageTableRowByCustomFilter -table $table -customFilter "(firstName eq 'User1') and (lastName eq 'LastName1')"
Table object of type Microsoft.WindowsAzure.Commands.Common.Storage.ResourceModel.AzureStorageTable to retrieve entities
Type: AzureStorageTable
Parameter Sets: (All)
Aliases:
Required: True
Position: 1
Default value: None
Accept pipeline input: False
Accept wildcard characters: False
Custom filter string.
Type: String
Parameter Sets: (All)
Aliases:
Required: True
Position: 2
Default value: None
Accept pipeline input: False
Accept wildcard characters: False
Returns one or more rows/entities based on Partition Key
Get-AzureStorageTableRowByPartitionKey [-table] <AzureStorageTable> [-partitionKey] <String>
Returns one or more rows/entities based on Partition Key
# Getting rows by partition Key
$saContext = (Get-AzureRmStorageAccount -ResourceGroupName $resourceGroup -Name $storageAccount).Context
$table = Get-AzureStorageTable -Name $tableName -Context $saContext
Get-AzureStorageTableRowByPartitionKey -table $table -partitionKey $newPartitionKey
Table object of type Microsoft.WindowsAzure.Commands.Common.Storage.ResourceModel.AzureStorageTable to retrieve entities
Type: AzureStorageTable
Parameter Sets: (All)
Aliases:
Required: True
Position: 1
Default value: None
Accept pipeline input: False
Accept wildcard characters: False
Identifies the table partition
Type: String
Parameter Sets: (All)
Aliases:
Required: True
Position: 2
Default value: None
Accept pipeline input: False
Accept wildcard characters: False
Remove-AzureStorageTableRow - Removes a specified table row
Remove-AzureStorageTableRow -table <AzureStorageTable> -entity <Object>
Remove-AzureStorageTableRow -table <AzureStorageTable> -partitionKey <String> -rowKey <String>
Remove-AzureStorageTableRow - Removes a specified table row. It accepts multiple deletions through the Pipeline when passing entities returned from the Get-AzureStorageTableRow available cmdlets. It also can delete a row/entity using Partition and Row Key properties directly.
# Deleting an entry by entity PS Object
$saContext = (Get-AzureRmStorageAccount -ResourceGroupName $resourceGroup -Name $storageAccount).Context
$table = Get-AzureStorageTable -Name $tableName -Context $saContext
\[string\]$filter1 = \[Microsoft.WindowsAzure.Storage.Table.TableQuery\]::GenerateFilterCondition("firstName",\[Microsoft.WindowsAzure.Storage.Table.QueryComparisons\]::Equal,"Paulo")
\[string\]$filter2 = \[Microsoft.WindowsAzure.Storage.Table.TableQuery\]::GenerateFilterCondition("lastName",\[Microsoft.WindowsAzure.Storage.Table.QueryComparisons\]::Equal,"Marques")
\[string\]$finalFilter = \[Microsoft.WindowsAzure.Storage.Table.TableQuery\]::CombineFilters($filter1,"and",$filter2)
$personToDelete = Get-AzureStorageTableRowByCustomFilter -table $table -customFilter $finalFilter
$personToDelete | Remove-AzureStorageTableRow -table $table
# Deleting an entry by using partitionkey and row key directly
$saContext = (Get-AzureRmStorageAccount -ResourceGroupName $resourceGroup -Name $storageAccount).Context
$table = Get-AzureStorageTable -Name $tableName -Context $saContext
Remove-AzureStorageTableRow -table $table -partitionKey "TableEntityDemoFullList" -rowKey "399b58af-4f26-48b4-9b40-e28a8b03e867"
# Deleting everything
$saContext = (Get-AzureRmStorageAccount -ResourceGroupName $resourceGroup -Name $storageAccount).Context
$table = Get-AzureStorageTable -Name $tableName -Context $saContext
Get-AzureStorageTableRowAll -table $table | Remove-AzureStorageTableRow -table $table
Table object of type Microsoft.WindowsAzure.Commands.Common.Storage.ResourceModel.AzureStorageTable where the entity exists
Type: AzureStorageTable
Parameter Sets: (All)
Aliases:
Required: True
Position: Named
Default value: None
Accept pipeline input: False
Accept wildcard characters: False
{{Fill entity Description}}
Type: Object
Parameter Sets: byEntityPSObjectObject
Aliases:
Required: True
Position: Named
Default value: None
Accept pipeline input: True (ByValue)
Accept wildcard characters: False
{{Fill partitionKey Description}}
Type: String
Parameter Sets: byPartitionandRowKeys
Aliases:
Required: True
Position: Named
Default value: None
Accept pipeline input: False
Accept wildcard characters: False
{{Fill rowKey Description}}
Type: String
Parameter Sets: byPartitionandRowKeys
Aliases:
Required: True
Position: Named
Default value: None
Accept pipeline input: False
Accept wildcard characters: False
Updates a table entity
Update-AzureStorageTableRow [-table] <AzureStorageTable> [-entity] <Object>
Updates a table entity. To work with this cmdlet, you need first retrieve an entity with one of the Get-AzureStorageTableRow cmdlets available and store in an object, change the necessary properties and then perform the update passing this modified entity back, through Pipeline or as argument. Notice that this cmdlet accepts only one entity per execution.
# Updating an entity
$saContext = (Get-AzureRmStorageAccount -ResourceGroupName $resourceGroup -Name $storageAccount).Context
$table = Get-AzureStorageTable -Name $tableName -Context $saContext
\[string\]$filter = \[Microsoft.WindowsAzure.Storage.Table.TableQuery\]::GenerateFilterCondition("firstName",\[Microsoft.WindowsAzure.Storage.Table.QueryComparisons\]::Equal,"User1")
$person = Get-AzureStorageTableRowByCustomFilter -table $table -customFilter $filter
$person.lastName = "New Last Name"
$person | Update-AzureStorageTableRow -table $table
Table object of type Microsoft.WindowsAzure.Commands.Common.Storage.ResourceModel.AzureStorageTable where the entity exists
Type: AzureStorageTable
Parameter Sets: (All)
Aliases:
Required: True
Position: 1
Default value: None
Accept pipeline input: False
Accept wildcard characters: False
The entity/row with new values to perform the update.
Type: Object
Parameter Sets: (All)
Aliases:
Required: True
Position: 2
Default value: None
Accept pipeline input: True (ByValue)
Accept wildcard characters: False
- Pester - PowerShell BDD style testing framework
- Azure Storage Emulator or Azure Subscription
- Azure Power Shell
Please make sure that your Azure Storage Emulator is up and running if you want to run all tests agains it.
PS> Invoke-Pester