Public/Get-SpecAzTableRow.ps1
function Get-SpecAzTableRow { <# .SYNOPSIS Gets a specific row from an Azure table based on the supplied key and value. .DESCRIPTION The Get-SpecAzTableRow function retrieves a specific row from an Azure table by matching the specified key and value. It accepts pipeline input and can return multiple rows based on the $Value variable received through the pipeline. .PARAMETER TableName The name of the Azure table. .PARAMETER TableResourceGroup The resource group of the Azure table storage account. .PARAMETER TableStorageAccount The name of the Azure table storage account. .PARAMETER Key The key (Column header) to match against in the Azure table. .PARAMETER Value The value to match against the Key in the Azure table. Accepts pipeline input. .EXAMPLE Get-SpecAzTableRow -TableName "MyTable" -TableResourceGroup "MyResourceGroup" -TableStorageAccount "MyStorageAccount" -Key "Name" -Value "John" This example retrieves a row from the Azure table "MyTable" in the "MyResourceGroup" resource group and "MyStorageAccount" storage account, where the "Name" key matches the value "John". .EXAMPLE "John", "Alice" | Get-SpecAzTableRow -TableName "MyTable" -TableResourceGroup "MyResourceGroup" -TableStorageAccount "MyStorageAccount" -Key "Name" This example uses pipeline input to retrieve rows from the Azure table "MyTable" in the "MyResourceGroup" resource group and "MyStorageAccount" storage account, where the "Name" key matches the values "John" and "Alice". .INPUTS [System.String] You can pipe one or more values to the `Value` parameter. .OUTPUTS [PSCustomObject] 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]$Key, [Parameter(Mandatory = $true, ValueFromPipeline = $true)] [string[]]$Value ) Begin { # clean up leading and trailing whitespace in vars $TableName = $TableName.Trim() $tableResourceGroup = $tableResourceGroup.Trim() $tableStorageAccount = $tableStorageAccount.Trim() $key = $key.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 { foreach ($val in $value) { $Val = $Val.Trim() Write-verbose "Getting row based on column header: $Key containing value: $Value" $tableresult = Get-AzTableRow -table $cloudTable -columnName $key -value $val -operator Equal -ErrorAction Stop if ($null -eq $tableresult) { Write-Warning "No record found for key: $key using value: $val" } Return $tableResult } } } |