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'
 
    .STATUS CODES
    601 - An error occurred during the retrieval process.
    602 - The remote server returned a 404 error (Table Not Found).
    603 - The remote server name could not be resolved (Storage Account Not Found).
    604 - An unspecified WebException occurred.
    605 - Missing [value] parameter when requesting filtered data.
    606 - Missing [key] parameter when requesting filtered data.
 
    .NOTES
    Author : owen.heaume
    Version : 1.1
 
    #>


    [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)) {
            write-warning "You have requested to return filtered data but the [value] parameter is missing"
            return 605
        }

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

        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.") {
                Write-Warning "An error occurred: The remote server returned a 404 error."
                Write-Warning "Table [$TableName] not found. Please check that the table exists and that you have not made a typo."
                return 602
            } elseif ($_.Exception.Message -match "The remote name could not be resolved") {
                write-warning $_.Exception.Message
                write-warning "Storage account [$StorageAccount] not found. Please check that the storage account exists and that you have not made a typo."
                return 603
            } else {
                # Handle other WebExceptions
                Write-Output "A WebException occurred: $_.Exception.Message)"
                return 604
            }
        }

        catch {
            write-error "An error occurred: $_"
            return 601
        }
    }
}