Get-AzTableItem.ps1

Function Get-AzTableItem
{
    <#
    .SYNOPSIS
        Get items from Azure Table.
 
    .PARAMETER odataVersion
        Specifies the OData version to use in the request. Valid values are "nometadata", "minimalmetadata" and "fullmetadata". The default value is "minimalmetadata".
 
    .PARAMETER StorageAccountName
        Name of the storage account containing the table.
 
    .PARAMETER TableName
        Name of the table to query.
 
    .PARAMETER AccessToken
        Access token for authenticating with Azure Table Storage.
 
    .PARAMETER PartitionKey
        Partition key of the item to retrieve. If specified, RowKey must also be specified
 
    .PARAMETER RowKey
        Row key of the item to retrieve. If specified, PartitionKey must also be specified
 
    .PARAMETER Filter
        OData filter expression to filter the items in the table.
 
    .PARAMETER Select
        OData select expression to specify which properties to return.
 
    .PARAMETER Top
        Maximum number of items to return.
 
    .EXAMPLE
        #Require RBAC app permissions on storage account: Storage Table Data Reader or Storage Table Data Contributor
        $Params = @{
            Scope = @("https://storage.azure.com/.default")
            ClientId = $Connection.ApplicationId
            TenantId = $Connection.TenantId
            RedirectUri = "https://login.microsoftonline.com/common/oauth2/nativeclient"
            Certificate = $Certificate
        }
        $Token = Get-PSMSALToken @Params
 
        #Get single item from Azure Table
        $Item = Get-AzTableItem -StorageAccountName "<StorageAccountName>" -TableName "<TableName>" -AccessToken $Token.AccessToken -PartitionKey "<PartitionKey>" -RowKey "<RowKey>"
 
    .EXAMPLE
        #Get all items from Azure Table with specific PartitionKey
        $Items = Get-AzTableItem -StorageAccountName "<StorageAccountName>" -TableName "<TableName>" -AccessToken $Token.AccessToken -PartitionKey "<PartitionKey>" -Verbose
 
    .EXAMPLE
        #Get all items from Azure Table
        $Items = Get-AzTableItem -StorageAccountName "<StorageAccountName>" -TableName "<TableName>" -AccessToken $Token.AccessToken -Verbose
 
    .NOTES
        Author: Michal Gajda
 
    .LINK
        https://learn.microsoft.com/en-us/rest/api/storageservices/query-entities
#>


    [CmdletBinding(SupportsShouldProcess=$True,ConfirmImpact="Low")]
    Param(
        [ValidateSet("nometadata","minimalmetadata","fullmetadata")]
        [Parameter()]
        [String]$OdataVersion = "minimalmetadata",
        [Parameter(Mandatory = $true)]
        [String]$StorageAccountName,
        [Parameter(Mandatory = $true)]
        [String]$TableName,
        [Parameter(Mandatory = $true)]
        [String]$AccessToken,
        [Parameter()]
        [String]$PartitionKey,
        [Parameter()]
        [String]$RowKey,
        [Parameter()]
        [String]$Filter,
        [Parameter()]
        [String]$Select,
        [ValidateRange(1,1000)]
        [Parameter()]
        [Int]$Top
    )

    Begin
    {
        #Build headers
        $Date = [DateTime]::UtcNow.ToString('R')
        $Headers = @{
            "Accept"        = "application/json;odata=$OdataVersion"
            "x-ms-version"  = "2026-06-06"
            "x-ms-date"     = $Date
            "Authorization" = "Bearer $($AccessToken)"
        }
    }

    Process
    {
        if($PartitionKey -and $RowKey)
        {
            #Get single unique item if have PartitionKey and RowKey
            $Uri = "https://$($StorageAccountName).table.core.windows.net/$($TableName)(PartitionKey='$($PartitionKey)',RowKey='$($RowKey)')"

            Write-Verbose $Uri
            Write-Verbose "Headers: $($Headers | Select-Object -exclude "Authorization" | Convertto-Json -Compress)"

            If ($PSCmdlet.ShouldProcess($StorageAccountName,"Get item with PartitionKey:'$($PartitionKey)' and RowKey:'$($RowKey)' from table $TableName"))
            {
                $Result = Invoke-RestMethod -Uri $Uri -Headers $Headers -Method Get -ResponseHeadersVariable ResponseHeaders
                Write-Verbose "Item retrieved: $($Result.Count)"
            }

            Return $Result
        } else {
            #Search for items
            $BaseUri = "https://$($StorageAccountName).table.core.windows.net/$($TableName)()"
            $Uri = $BaseUri

            #Set Filters - PartitionKey
            if($PartitionKey)
            {
                if($Uri -match "\?") { $Uri += "&" } else { $Uri += "?" }
                $Uri += "`$filter=PartitionKey eq '$($PartitionKey)'"
            }

            #Set Filters
            if($Filter)
            {
                if($Uri -match "\?") { $Uri += "&" } else { $Uri += "?" }
                $Uri += "`$filter=$($Filter)"
            }

            #Set Select
            if($Select)
            {
                if($Uri -match "\?") { $Uri += "&" } else { $Uri += "?" }
                $Uri += "`$select=$($Select -join ",")"
            }

            #Set Top
            if($Top)
            {
                if($Uri -match "\?") { $Uri += "&" } else { $Uri += "?" }
                $Uri += "`$top=$($Top)"
            }

            $ResponseHeaders = $null
            $Result = @()
            do {
                if($null -ne $ResponseHeaders)
                {
                    if($ResponseHeaders["x-ms-continuation-NextPartitionKey"] -and $ResponseHeaders["x-ms-continuation-NextRowKey"])
                    {
                        $Uri = "$($BaseUri)?NextPartitionKey=$($ResponseHeaders["x-ms-continuation-NextPartitionKey"])&NextRowKey=$($ResponseHeaders["x-ms-continuation-NextRowKey"])"
                    }
                }

                Write-Verbose $Uri
                Write-Verbose "Headers: $($Headers | Select-Object -exclude "Authorization" | Convertto-Json -Compress)"

                If ($PSCmdlet.ShouldProcess($StorageAccountName,"Get items from table $TableName"))
                {
                    $Response = Invoke-RestMethod -Uri $Uri -Headers $Headers -Method Get -ResponseHeadersVariable ResponseHeaders
                    $Result += $Response.value
                    Write-Verbose "ResponseHeaders: $($ResponseHeaders | Convertto-Json -Compress)"
                }

                if($null -eq $ResponseHeaders -or $Top)
                {
                    $ResponseHeaders = @{
                        "x-ms-continuation-NextPartitionKey" = $null
                        "x-ms-continuation-NextRowKey" = $null
                    }
                }
            } while($ResponseHeaders["x-ms-continuation-NextPartitionKey"] -and $ResponseHeaders["x-ms-continuation-NextRowKey"])

            Write-Verbose "Items retrieved: $($Result.Count)"
            Return $Result
        }
    }

    End{}
}