Skip to content

Commit 74660cc

Browse files
committed
Added pipe to Get-FabricSQLDatabase
1 parent 96f67ef commit 74660cc

File tree

1 file changed

+37
-15
lines changed

1 file changed

+37
-15
lines changed

source/Public/SQL Database/Get-FabricSQLDatabase.ps1

Lines changed: 37 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -48,31 +48,53 @@ function Get-FabricSQLDatabase {
4848

4949
[CmdletBinding()]
5050
param (
51-
[Parameter(Mandatory = $true)]
51+
[Parameter(Mandatory = $true, ParameterSetName = 'WorkspaceId')]
5252
[string]$WorkspaceId,
5353

54+
[Parameter(Mandatory = $true, ParameterSetName = 'WorkspaceObject', ValueFromPipeline = $true )]
55+
$Workspace,
56+
5457
[Alias("Name", "DisplayName")]
5558
[string]$SQLDatabaseName,
5659

5760
[Alias("Id")]
5861
[string]$SQLDatabaseId
5962
)
6063

61-
Test-TokenExpired
62-
63-
Write-Verbose "You can either use SQLDatabaseName or SQLDatabaseID not both. If both are used throw error"
64-
if ($PSBoundParameters.ContainsKey("SQLDatabaseName") -and $PSBoundParameters.ContainsKey("SQLDatabaseId")) {
65-
throw "Parameters SQLDatabaseName and SQLDatabaseId cannot be used together"
64+
begin {
65+
Test-TokenExpired
66+
if ($PSBoundParameters.ContainsKey("SQLDatabaseName") -and $PSBoundParameters.ContainsKey("SQLDatabaseId")) {
67+
throw "Parameters SQLDatabaseName and SQLDatabaseId cannot be used together"
68+
}
6669
}
6770

68-
# Create SQLDatabase API
69-
$uri = "$($FabricSession.BaseApiUrl)/workspaces/$workspaceId/SqlDatabases"
70-
if ($SQLDatabaseId) {
71-
$uri = "$uri/$SQLDatabaseId"
71+
process {
72+
if ($PSCmdlet.ParameterSetName -eq 'WorkspaceObject') {
73+
$WorkspaceId = $Workspace.id
74+
}
75+
76+
# Create SQLDatabase API
77+
$uri = "$($FabricSession.BaseApiUrl)/workspaces/$WorkspaceId/SqlDatabases"
78+
if ($SQLDatabaseId) {
79+
$uri = "$uri/$SQLDatabaseId"
80+
}
81+
$response = Invoke-FabricRestMethod -Uri $uri
82+
##$databases.Where({$_.displayName -eq $body.displayName}).id
83+
84+
# Step: Validate the response code
85+
if ($statusCode -ne 200) {
86+
Write-Message -Message "Unexpected response code: $statusCode from the API." -Level Error
87+
Write-Message -Message "Error: $($response.message)" -Level Error
88+
Write-Message -Message "Error Details: $($response.moreDetails)" -Level Error
89+
Write-Message "Error Code: $($response.errorCode)" -Level Error
90+
return $null
91+
}
92+
93+
$response = $response.value
94+
if ($SQLDatabaseName) {
95+
# Filter the SQLDatabase by name
96+
$response = $response | Where-Object { $_.displayName -eq $SQLDatabaseName }
97+
}
98+
return $response
7299
}
73-
$result = Invoke-RestMethod -Headers $FabricSession.HeaderParams -Uri $uri
74-
##$databases.Where({$_.displayName -eq $body.displayName}).id
75-
76-
return $result.value
77-
78100
}

0 commit comments

Comments
 (0)