Public/Get-SpecAzTableByFilter.ps1
function Get-SpecAzTableByFilter { <# .SYNOPSIS Retrieves rows from an Azure table based on a custom filter. .DESCRIPTION The Get-SpecAzTableByFilter function retrieves rows from an Azure table using a custom filter expression. It requires the Azure table name, resource group, storage account, and a custom filter string. .PARAMETER TableName The name of the Azure table to query. .PARAMETER TableResourceGroup The resource group name of the Azure storage account containing the table. .PARAMETER TableStorageAccount The name of the Azure storage account containing the table. .PARAMETER CustomFilter The custom filter expression to apply to the table rows. This should follow the syntax and conventions of Azure Storage Table query operations. .EXAMPLE $filt = 'fred.bloggs' $customFilter = "(Last_Logged_On eq '$filt')" Get-SpecAzTableByFilter -TableName 'myTable' -TableResourceGroup 'myResourceGroup' -TableStorageAccount 'myStorageAccount' -CustomFilter $customFilter Retrieves rows from the 'myTable' Azure table in the 'myResourceGroup' resource group and 'myStorageAccount' storage account, using the custom filter expression "(Last_Logged_On eq 'fred.bloggs')". .INPUTS System.String .OUTPUTS System.Object It will return various error numbers depending on the error: 501 - Storage account not found 502 - Table Not found .NOTES Author: owen.heaume Version: - 1.0 Initial release - 1.1 Remove Exit codes and replace with return numbers #> [cmdletbinding()] param ( [Parameter(Mandatory = $true)] [string]$TableName, [Parameter(Mandatory = $true)] [string]$tableResourceGroup, [Parameter(Mandatory = $true)] [string]$tableStorageAccount, [Parameter(Mandatory = $true)] [string]$CustomFilter ) Begin { # clean up leading and trailing whitespace in vars $TableName = $TableName.Trim() $tableResourceGroup = $tableResourceGroup.Trim() $tableStorageAccount = $tableStorageAccount.Trim() $CustomFilter = $CustomFilter.Trim() # Get Storage account context try { $storageAccount = Get-AzStorageAccount -ResourceGroupName $tableresourceGroup -Name $tablestorageAccount -ErrorAction Stop $ctx = $storageAccount.Context } catch { Write-Warning "Unable to locate storage account: $tableStorageAccount. Are you sure it exists?" return 501 } try { $storageTable = Get-AzStorageTable –Name $TableName –Context $ctx -ErrorAction Stop $cloudTable = $storageTable.CloudTable } catch { Write-Warning "Unable to locate table: $TableName. Are you sure it exists?" return 502 } } process { Write-verbose "Attempting to get the row(s) based on your filter: $CustomFilter" $tableresult = Get-AzTableRow -table $cloudTable -CustomFilter $CustomFilter -ErrorAction Stop if ($null -eq $tableresult) { Write-Warning "No record found using the filter: $CustomFilter" } Return $tableResult } } |