Public/Get-pseAzTableRowUsingSAS.ps1
function Get-pseAzTableRowUsingSAS { <# .SYNOPSIS Retrieves data from an Azure Table Storage using a Shared Access Signature (SAS) token. .DESCRIPTION This function allows you to retrieve data from an Azure Table Storage using a SAS token for authentication. You can retrieve all data from the table or specify key-value pairs to filter the results. .PARAMETER SAS The Shared Access Signature (SAS) token for authenticating with the Azure Table Storage. .PARAMETER StorageAccount The name of the Azure Storage Account containing the table. .PARAMETER TableName The name of the Azure Table to retrieve data from. .PARAMETER Key (Optional) The key to use for filtering data. .PARAMETER Value (Optional) The value to use for filtering data. .PARAMETER CustomFilter (Optional) An optional custom filter to apply to the URI. .PARAMETER GetAllRows A switch parameter to indicate whether to retrieve all rows in the table. (All table data) .EXAMPLE Retrieve all data from the specified Azure Table using the -GetAllRows switch: Get-pseAzTableRowUsingSAS -SAS $SasToken -StorageAccount "myaccount" -TableName "mytable" -GetAllRows .EXAMPLE Retrieve row data from the specified Azure Table using the -Key and -Value: Get-pseAzTableRowUsingSAS -SAS $SasTokenReadToken -StorageAccount $storageAccount -TableName $tableName -Key 'DeviceType' -Value 'Dispense' Returns the row where 'DeviceType' = 'Dispense' .EXAMPLE Retrieve row data from the specified Azure Table using a custom filter: $customFilter = "(Last_Logged_On eq 'owen.heaume') and (Device_Name eq 'UKC-HV4DZY2')" Get-pseAzTableRowUsingSAS -SAS $SasTokenReadToken -StorageAccount $storageAccount -TableName $tableName -CustomFilter $customFilter Returns the row where 'Last_Logged_On' = 'owen.heaume' and 'Device_Name' = 'UKC-HV4DZY2' .NOTES Author : owen.heaume Version : - 1.0.0 - Initial release #> [cmdletbinding()] param( [parameter (mandatory = $true)] [string]$SasToken, [parameter (mandatory = $true)] [string]$StorageAccount, [parameter (mandatory = $true)] [string]$TableName, [parameter (mandatory = $false, ParameterSetName = 'KeyValue')] [string]$Key, [parameter (mandatory = $false, ParameterSetName = 'KeyValue')] [string]$Value, [parameter (mandatory = $false, ParameterSetName = 'Custom')] [string]$CustomFilter, [parameter (ParameterSetName = 'GetAllRows')] [Switch]$GetAllRows ) begin { $headers = @{ Accept = 'application/json;odata=nometadata' } } process { # Validate key and value parameters if ($key -and [string]::IsNullOrEmpty($value)) { throw "605: You have requested to return filtered data but the [value] parameter is missing" } if ($value -and [string]::IsNullOrEmpty($key)) { throw "606: You have requested to return filtered data but the [key] parameter is missing" } # Construct the initial URI if ($key -and $value) { Write-Verbose "Requesting filtered data" $filter = "`$filter=($key eq '$value')" $tableUri = Get-SpecAzTableUri -SAS $SasToken -StorageAccount $StorageAccount -TableName $TableName -Key $key -Value $Value } elseif ($customfilter) { $tableUri = Get-SpecAzTableUri -SAS $SasToken -StorageAccount $StorageAccount -TableName $TableName -CustomFilter $CustomFilter } else { Write-Verbose "Requesting all data" $tableUri = Get-SpecAzTableUri -SAS $SasToken -StorageAccount $StorageAccount -TableName $TableName -GetAllRows } $allItems = @() $nextPartitionKey = $null $nextRowKey = $null do { try { $uri = $tableUri + "&`$top=1000" if ($nextPartitionKey -and $nextRowKey) { $uri += "&NextPartitionKey=$nextPartitionKey&NextRowKey=$nextRowKey" } Write-Verbose "Making request to URI: $uri" $response = Invoke-WebRequest -Method Get -Uri $uri -Headers $headers -ContentType application/json -ErrorAction Stop -UseBasicParsing $content = $response.Content | ConvertFrom-Json if ($content -and $content.value) { $allItems += $content.value } else { Write-Host "No data returned in response" -ForegroundColor DarkYellow } Write-Verbose "Response Headers: $($response.Headers | Format-List)" if ($response.Headers['x-ms-continuation-NextPartitionKey'] -and $response.Headers['x-ms-continuation-NextRowKey']) { $nextPartitionKey = $response.Headers['x-ms-continuation-NextPartitionKey'] $nextRowKey = $response.Headers['x-ms-continuation-NextRowKey'] Write-Verbose "Received continuation tokens: NextPartitionKey=$nextPartitionKey, NextRowKey=$nextRowKey" } else { Write-Verbose "No continuation headers found" $nextPartitionKey = $null $nextRowKey = $null } } catch [System.Net.WebException] { if ($_.Exception.Message -contains "The remote server returned an error: (404) Not Found.") { write-error "602: Table [$TableName] not found. Please check that the table exists and that you have not made a typo. $_" } elseif ($_.Exception.Message -match "The remote name could not be resolved") { write-error "603: Storage account [$StorageAccount] not found. Please check that the storage account exists and that you have not made a typo. $_" } else { # Handle other WebExceptions write-error "A WebException occurred: $($_.Exception.Message)" } } catch { write-error "601: An error occurred: $_" } } while ($nextPartitionKey -or $nextRowKey) return $allItems } } |