Public/Get-AzTableAPITable.ps1
|
function Get-AzTableAPITable { <# .SYNOPSIS Lists tables in an Azure Table Storage account, or retrieves a specific table. .PARAMETER Context Connection context created by New-AzTableAPIContext. .PARAMETER TableName Optional. When specified, retrieves only the named table. .OUTPUTS One or more PSCustomObject representing table entries. .EXAMPLE $tables = Get-AzTableAPITable -Context $ctx .EXAMPLE $table = Get-AzTableAPITable -Context $ctx -TableName 'Orders' #> [CmdletBinding()] [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, [string]$TableName ) if ($TableName) { # Use an OData $filter query instead of Tables('name') to retrieve a specific table. # Azurite redirects GET /Tables('name') to the entity-query handler, so the filter # approach is the only form that works consistently with both Azurite and Azure. # Azure Table Storage table names are restricted to alphanumeric characters only # (enforced by New-AzTableAPITable), so single-quote escaping is a conservative # safeguard that covers all possible input without risking OData injection. $escapedName = $TableName.Replace("'", "''") $queryString = "`$filter=TableName eq '$escapedName'" $response = Invoke-AzTableRestMethod -Context $Context -Method 'GET' -Resource 'Tables' -QueryString $queryString if ($response.Content -and $response.Content.value -and $response.Content.value.Count -gt 0) { return $response.Content.value[0] } return $null } # List all tables, following NextTableName continuation tokens $allTables = [System.Collections.Generic.List[PSCustomObject]]::new() $nextTableName = $null do { $query = if ($null -ne $nextTableName) { "NextTableName=$([Uri]::EscapeDataString($nextTableName))" } else { '' } $response = Invoke-AzTableRestMethod -Context $Context -Method 'GET' -Resource 'Tables' -QueryString $query if ($response.Content -and $response.Content.value) { foreach ($table in $response.Content.value) { $allTables.Add($table) } } $nextTableName = $response.Headers['x-ms-continuation-NextTableName'] if ($nextTableName -is [array]) { $nextTableName = $nextTableName[0] } } until ([string]::IsNullOrEmpty($nextTableName)) return $allTables.ToArray() } |