Public/Get-SpecAzTableDataUsingSAS.ps1

function Get-SpecAzTableDataUsingSAS {
    <#
    .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.
 
    .EXAMPLE
    # Retrieve all data from the specified Azure Table
    Get-SpecAzTableDataUsingSAS -SAS $SAS -StorageAccount "myaccount" -TableName "mytable"
 
    .EXAMPLE
    # Retrieve filtered data from the specified Azure Table
    $params = @{
        storageAccount = "stem4prdeud01"
        tableName = "NLRetailDeviceInfo"
        SAS = "<SASKey>"
        Key = 'Last_Logged_On'
        Value = 'owen.heaume'
    }
 
    Get-SpecAzTableDataUsingSAS @params
 
    .NOTES
    Author : owen.heaume
    Version : 1.0
 
    The following values could be returned if there is an error:
    601 - An error occurred during the retrieval process.
    602 - The [value] parameter is missing when requesting filtered data.
    603 - The [key] parameter is missing when requesting filtered data.
    #>


    [cmdletbinding()]
    param(
        [parameter (mandatory = $true)]
        [string]
        $SAS,

        [parameter (mandatory = $true)]
        [string]
        $StorageAccount,

        [parameter (mandatory = $true)]
        [string]
        $TableName,

        [parameter (mandatory = $false)]
        [string]
        $Key,

        [parameter (mandatory = $false)]
        [string]
        $Value
    )

    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 602
        }

        # $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 603
        }

        if ($key -and $value) {
            write-Verbose "Requesting filtered data"
            # using a filter
            $filter = "`$filter=($key eq '$value')"
            $tableUri = Get-SpecazTableUri -SAS $SAS -StorageAccount $StorageAccount -TableName $TableName -Key $key -Value $Value
        } else {
            write-Verbose "Requesting all data"
            # all table items
            $tableUri = Get-SpecAzTableUri -SAS $SAS -StorageAccount $StorageAccount -TableName $TableName
        }

        try {
            $item = Invoke-RestMethod -Method Get -Uri $tableUri -Headers $headers -ContentType application/json -ea stop -ev x
            $item.value
        } catch {
            write-error "An error occured: $_"
            return 601
        }
    }
}