Public/Get-AzTableAPIEntity.ps1
|
function Get-AzTableAPIEntity { <# .SYNOPSIS Queries entities from an Azure Table Storage table. .DESCRIPTION Retrieves a single entity by PartitionKey and RowKey, or queries multiple entities using OData filter, select, and top parameters. Automatically follows continuation tokens to return all matching entities. .PARAMETER Context Connection context created by New-AzTableAPIContext. .PARAMETER TableName Name of the table to query. .PARAMETER PartitionKey PartitionKey of the entity to retrieve. Must be combined with -RowKey. .PARAMETER RowKey RowKey of the entity to retrieve. Must be combined with -PartitionKey. .PARAMETER Filter OData filter expression, e.g. "PartitionKey eq 'Sales' and RowKey eq '001'". .PARAMETER Select Column names to project (select). Returns only the specified properties. .PARAMETER Top Maximum number of entities to return. Defaults to all entities. .OUTPUTS One or more PSCustomObject representing the matched entities. .EXAMPLE Get-AzTableAPIEntity -Context $ctx -TableName 'Orders' .EXAMPLE Get-AzTableAPIEntity -Context $ctx -TableName 'Orders' -PartitionKey 'Sales' -RowKey '001' .EXAMPLE Get-AzTableAPIEntity -Context $ctx -TableName 'Orders' -Filter "Status eq 'Pending'" -Top 100 #> [CmdletBinding(DefaultParameterSetName = 'Query')] [OutputType([PSCustomObject])] param ( [Parameter(Mandatory)] [ValidateScript({ ($_.PSObject.Properties.Name -contains 'Endpoint') -and ($_.PSObject.Properties.Name -contains 'AuthType') }, ErrorMessage = 'The -Context parameter requires a context object created by New-AzTableAPIContext.')] [PSCustomObject]$Context, [Parameter(Mandatory)] [ValidateNotNullOrEmpty()] [string]$TableName, [Parameter(Mandatory, ParameterSetName = 'SingleEntity')] [ValidateNotNullOrEmpty()] [string]$PartitionKey, [Parameter(Mandatory, ParameterSetName = 'SingleEntity')] [ValidateNotNullOrEmpty()] [string]$RowKey, [Parameter(ParameterSetName = 'Query')] [string]$Filter, [Parameter(ParameterSetName = 'Query')] [string[]]$Select, [Parameter(ParameterSetName = 'Query')] [ValidateRange(1, [int]::MaxValue)] [int]$Top ) if ($PSCmdlet.ParameterSetName -eq 'SingleEntity') { $resource = Format-AzTableEntityResource -TableName $TableName -PartitionKey $PartitionKey -RowKey $RowKey $response = Invoke-AzTableRestMethod -Context $Context -Method 'GET' -Resource $resource return $response.Content } # Build OData query string parts $queryParts = [System.Collections.Generic.List[string]]::new() if ($Filter) { $queryParts.Add("`$filter=$([Uri]::EscapeDataString($Filter))") } if ($Select) { $queryParts.Add("`$select=$([Uri]::EscapeDataString($Select -join ','))") } if ($PSBoundParameters.ContainsKey('Top')) { $queryParts.Add("`$top=$Top") } $baseQuery = $queryParts -join '&' $resource = "$TableName()" # Retrieve all pages, following continuation tokens $allEntities = [System.Collections.Generic.List[PSCustomObject]]::new() $nextPartitionKey = $null $nextRowKey = $null do { $continuationParts = [System.Collections.Generic.List[string]]::new() if ($null -ne $nextPartitionKey) { $continuationParts.Add("NextPartitionKey=$([Uri]::EscapeDataString($nextPartitionKey))") $continuationParts.Add("NextRowKey=$([Uri]::EscapeDataString($nextRowKey ?? ''))") } $continuationQuery = $continuationParts -join '&' $query = @($baseQuery, $continuationQuery) | Where-Object { $_ } | Join-String -Separator '&' $response = Invoke-AzTableRestMethod -Context $Context -Method 'GET' -Resource $resource -QueryString $query if ($response.Content -and $response.Content.value) { foreach ($entity in $response.Content.value) { $allEntities.Add($entity) } } # Check for continuation token in response headers $nextPartitionKey = $response.Headers['x-ms-continuation-NextPartitionKey'] $nextRowKey = $response.Headers['x-ms-continuation-NextRowKey'] # Normalize - some PS versions return arrays; take the first element if ($nextPartitionKey -is [array]) { $nextPartitionKey = $nextPartitionKey[0] } if ($nextRowKey -is [array]) { $nextRowKey = $nextRowKey[0] } } until ([string]::IsNullOrEmpty($nextPartitionKey)) return $allEntities.ToArray() } |