Yo! I’m back again. This time I have been playing around with Azure Table Storage.
Azure Table storage is a service that stores structured NoSQL data in the cloud, providing a key/attribute store with a schema less design. Because table storage is schema less, it’s easy to adapt your data as the needs of your application evolve. Access to Table storage data is fast and cost-effective for many types of applications, and is typically lower in cost than traditional SQL for similar volumes of data.
You need to create a storage account in an Azure subscription and generate a shared access signature, prior before using the code below.
I created some lightweight functions that allows you to insert and get data from a table with PowerShell, using REST API.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
Function Insert-AzureTableData { [CmdletBinding()] Param ( [parameter(Mandatory=$true)][string]$Endpoint, [parameter(Mandatory=$true)][string]$SharedAccessSignature, [parameter(Mandatory=$true)][string]$Table, [parameter(Mandatory=$true)][hashtable]$TableData ) #Create request header. $Headers = @{ "x-ms-date"=(Get-Date -Format r); "x-ms-version"="2016-05-31"; "Accept-Charset"="UTF-8"; "DataServiceVersion"="3.0;NetFx"; "MaxDataServiceVersion"="3.0;NetFx"; "Accept"="application/json;odata=nometadata" }; #Construct URI. $URI = ($Endpoint + "/" + $Table + "/" + $SharedAccessSignature); #Convert table data to JSON and encode to UTF8. $Body = [System.Text.Encoding]::UTF8.GetBytes((ConvertTo-Json -InputObject $TableData)); #Insert data to Azure storage table. Invoke-WebRequest -Method Post -Uri $URI -Headers $Headers -Body $Body -ContentType "application/json"; } Function Get-AzureTableData { [CmdletBinding()] Param ( [parameter(Mandatory=$true)][string]$Endpoint, [parameter(Mandatory=$true)][string]$SharedAccessSignature, [parameter(Mandatory=$true)][string]$Table ) #Create request header. $Headers = @{ "x-ms-date"=(Get-Date -Format r); "x-ms-version"="2016-05-31"; "Accept-Charset"="UTF-8"; "DataServiceVersion"="3.0;NetFx"; "MaxDataServiceVersion"="3.0;NetFx"; "Accept"="application/json;odata=nometadata" }; #Construct URI. $URI = ($Endpoint + "/" + $Table + "/" + $SharedAccessSignature); #Insert data to Azure storage table. $Response = Invoke-WebRequest -Method Get -Uri $URI -Headers $Headers; #Return table data. Return ,($Response.Content | ConvertFrom-Json).Value; } #Storage account URL. $Endpoint = 'https://<storage account>.table.core.windows.net'; #Generated shared access signature. $SharedAccessSignature = '<insert shared access signature here>'; #Table name in the storage account. $Table = '<Table name>'; ############################## #Query all rows from a table. Get-AzureTableData -Endpoint $Endpoint -SharedAccessSignature $SharedAccessSignature -Table $Table; ############################## ############################## #Data you want to add to the table. $TableData = @{ "PartitionKey" = "PartitionKeyData1"; "RowKey" = "RowKeyTestData1"; "<Column Name>" = "<Column Data>"; }; #Insert data. Insert-AzureTableData -Endpoint $Endpoint -SharedAccessSignature $SharedAccessSignature -Table $Table -TableData $TableData; ############################## |
Resources:
- Table Storage with PowerShell.
- Create a storage account.
- What is Shared Access Signatures?
- Azure Storage Services REST API
Hello,
this functions work great. Thank you.
Could you also create a function to Delete and to Update? So that it would work with PowerShell 7.
That would be wonderfull. Thanks.