Last active
October 18, 2023 14:24
-
-
Save JKerens/047b4cde77ceecaf93fe88aaa34ce051 to your computer and use it in GitHub Desktop.
Addons to Az.Sql to query the table APIs from the portal
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <# | |
| .EXAMPLE | |
| $db = Get-AzSqlDatabase -ResourceGroupName <ResourceGroupName> -ServerName <ServerName> -DatabaseName <DatabaseName> | |
| Get-AzSqlDatabaseSchema -ResourceId $db.ResourceId | |
| #> | |
| function Get-AzSqlDatabaseSchema { | |
| [CmdletBinding()] | |
| param ( | |
| [Parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true)] | |
| [string]$ResourceId | |
| ) | |
| $schemasApi = $ResourceId + "/schemas" + "?api-version=2018-06-01-preview" | |
| $schemasResult = Invoke-AzRest -Method GET -Path $schemasApi | |
| $schemasInfo = $schemasResult.Content | ConvertFrom-Json -Depth 100 | |
| $schemasInfo.value | ForEach-Object { Write-Output $_ } | |
| } | |
| function Get-AzSqlDatabaseTable { | |
| [CmdletBinding()] | |
| param ( | |
| [Parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true)] | |
| [string]$ResourceId, | |
| [Parameter(Mandatory = $false)] | |
| [string[]]$SchemaName, | |
| [Parameter(Mandatory = $false)] | |
| [string[]]$TableName | |
| ) | |
| $schemas = $SchemaName ?? (Get-AzSqlDatabaseSchema -ResourceId $ResourceId).name | |
| foreach ($schema in $schemas) { | |
| $tableApi = $ResourceId + "/schemas/" + $schema + "/tables" + "?api-version=2018-06-01-preview" | |
| $tableResult = Invoke-AzRest -Method GET -Path $tableApi | |
| $tableInfo = $tableResult.Content | ConvertFrom-Json -Depth 100 | |
| $result = $tableInfo.value | |
| if ($TableName) { | |
| $result = $result | Where-Object { $_ -in $TableName } | |
| } | |
| $result | ForEach-Object { Write-Output $_ } | |
| } | |
| } | |
| function Get-AzSqlDatabaseColumn { | |
| [CmdletBinding()] | |
| param ( | |
| [Parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true)] | |
| [string]$ResourceId, | |
| [Parameter(Mandatory = $false)] | |
| [string[]]$SchemaName, | |
| [Parameter(Mandatory = $false)] | |
| [string[]]$TableName, | |
| [Parameter(Mandatory = $false)] | |
| [string[]]$ColumnName | |
| ) | |
| # Get all the schemas and tables to work on if not specified | |
| $schemas = $SchemaName ?? (Get-AzSqlDatabaseSchema -ResourceId $ResourceId).name | |
| $tables = $TableName ?? (Get-AzSqlDatabaseTable -ResourceId $ResourceId).name | |
| # UI progress rules | |
| $totalSchemas = $schemas.Count | |
| $totalTables = $tables.Count | |
| $shouldUseProgress = $totalSchemas * $totalTables -ge 5 | |
| # Loop the schemas | |
| for ($schemaIndex = 0; $schemaIndex -lt $totalSchemas; $schemaIndex++) { | |
| # Set the loop value and update progress if it is a large operation | |
| $schema = $schemas[$schemaIndex] | |
| if ($shouldUseProgress) { | |
| Write-Progress -Id 1 -Activity "Schema" -PercentComplete (($schemaIndex / $totalSchemas) * 100) -Status $schema | |
| } | |
| # Loop the tables | |
| for ($tableIndex = 0; $tableIndex -lt $totalTables; $tableIndex++) { | |
| # Set the loop value and update progress if it is a large operation | |
| $table = $tables[$tableIndex] | |
| if ($shouldUseProgress) { | |
| Write-Progress -Id 2 -Activity "Table" -PercentComplete (($tableIndex / $totalTables) * 100) -Status $table | |
| } | |
| # Actual work | |
| $columnApi = $ResourceId + "/schemas/" + $schema + "/tables/" + $table + "/columns" + "?api-version=2018-06-01-preview" | |
| $columnResult = Invoke-AzRest -Method GET -Path $columnApi | |
| $columnInfo = $columnResult.Content | ConvertFrom-Json -Depth 100 | |
| $result = $columnInfo.value | |
| if ($ColumnName) { | |
| $result = $result | Where-Object { $_ -in $ColumnName } | |
| } | |
| $result | ForEach-Object { Write-Output $_ } | |
| } | |
| } | |
| } | |
| $schemaCompleter = { | |
| [OutputType([System.Management.Automation.CompletionResult])] | |
| param( | |
| [string] $CommandName, | |
| [string] $ParameterName, | |
| [string] $WordToComplete, | |
| [System.Management.Automation.Language.CommandAst] $CommandAst, | |
| [System.Collections.IDictionary] $FakeBoundParameters | |
| ) | |
| $CompletionResults = [System.Collections.Generic.List[System.Management.Automation.CompletionResult]]::new() | |
| $__resourceId = $FakeBoundParameters["ResourceId"] | |
| if ($__resourceId) { | |
| (Get-AzSqlDatabaseSchema -ResourceId $__resourceId).name | Where-Object { | |
| $_ -like "*$WordToComplete*" | |
| } | ForEach-Object { $CompletionResults.Add($_) } | |
| } | |
| return $CompletionResults | |
| } | |
| $tableCompleter = { | |
| [OutputType([System.Management.Automation.CompletionResult])] | |
| param( | |
| [string] $CommandName, | |
| [string] $ParameterName, | |
| [string] $WordToComplete, | |
| [System.Management.Automation.Language.CommandAst] $CommandAst, | |
| [System.Collections.IDictionary] $FakeBoundParameters | |
| ) | |
| $CompletionResults = [System.Collections.Generic.List[System.Management.Automation.CompletionResult]]::new() | |
| $__resourceId = $FakeBoundParameters["ResourceId"] | |
| $__schemaName = $FakeBoundParameters["SchemaName"] | |
| if ($__resourceId -and $__schemaName) { | |
| (Get-AzSqlDatabaseTable -ResourceId $__resourceId -SchemaName $__schemaName).name | Where-Object { | |
| $_ -like "*$WordToComplete*" | |
| } | ForEach-Object { $CompletionResults.Add($_) } | |
| } | |
| return $CompletionResults | |
| } | |
| Register-ArgumentCompleter ` | |
| -CommandName Get-AzSqlDatabaseTable, Get-AzSqlDatabaseColumn ` | |
| -ParameterName SchemaName ` | |
| -ScriptBlock $schemaCompleter | |
| Register-ArgumentCompleter ` | |
| -CommandName Get-AzSqlDatabaseTable, Get-AzSqlDatabaseColumn ` | |
| -ParameterName TableName ` | |
| -ScriptBlock $TableCompleter |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment