Public/Get-SpecAzTableRowUsingSAS.ps1

function Get-SpecAzTableRowUsingSAS {
    <#
    .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-SpecAzTableRowUsingSAS -SAS $SasToken -StorageAccount "myaccount" -TableName "mytable" -GetAllRows
 
    .EXAMPLE
    Retrieve row data from the specified Azure Table using the -Key and -Value:
    Get-SpecAzTableRowUsingSAS -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-SpecAzTableRowUsingSAS -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.1
                     - 1.1.1 - Now throws on errors so that they can be trapped via try/catch
 
    #>


    [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 {
        # $key has a value but $data does not!
        if ($key -and [string]::IsNullOrEmpty($value)) {
            throw "605: You have requested to return filtered data but the [value] parameter is missing"
        }

        # $key has a value but $data does not!
        if ($value -and [string]::IsNullOrEmpty($key)) {
            throw "606: You have requested to return filtered data but the [key] parameter is missing"
        }

        if ($key -and $value) {
            Write-Verbose "Requesting filtered data"
            # using a filter
            $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"
            # all table items
            $tableUri = Get-SpecAzTableUri -SAS $SasToken -StorageAccount $StorageAccount -TableName $TableName -GetAllRows
        }

        try {
            $item = Invoke-RestMethod -Method Get -Uri $tableUri -Headers $headers -ContentType application/json -ea stop -ev x
            $item.value
        } catch [System.Net.WebException] {
            if ($_.Exception.Message -contains "The remote server returned an error: (404) Not Found.") {
                throw "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") {
                throw "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
                throw "A WebException occurred: $_.Exception.Message)"
            }
        } catch {
            throw "601: An error occurred: $_"
        }
    }
}