Skip to content

Commit 5d2213b

Browse files
committed
* Added few new functions:
'Get-FabricSQLDatabase', 'Remove-FabricSQLDatabase', 'Get-FabricCapacitySkus', 'Confirm-FabricAuthToken' * Unified objects in module scope (FabricSession, AzureSession) * Module scope holds all api urls, header params for requests * Unified function to authenticate to Fabric API with bearer token * Auto-refresh token when expires * Removed $authToken as input param in several functions * Removed usage of $env:azToken from several functions * Get-FabricItem accepts Workspaces as input-pipeline * Minor cosmetic changes
1 parent 7d1b416 commit 5d2213b

File tree

53 files changed

+433
-273
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+433
-273
lines changed

FabricTools/FabricTools.psd1

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
RootModule = 'FabricTools.psm1'
1313

1414
# Version number of this module.
15-
ModuleVersion = '0.8.0.0'
15+
ModuleVersion = '0.9.0.0'
1616

1717
# Supported PSEditions
1818
# CompatiblePSEditions = @()
@@ -112,13 +112,13 @@
112112
'Get-FabricKQLDashboard',
113113
'Get-FabricKQLDatabase',
114114
'Get-FabricKQLQueryset',
115-
'Get-FabricWorkspace',
115+
'Get-FabricWorkspace2',
116116
'Invoke-FabricKQLCommand',
117117
'New-FabricEventhouse',
118118
'New-FabricEventstream',
119119
'New-FabricKQLDashboard',
120120
'New-FabricKQLDatabase',
121-
'New-FabricWorkspace',
121+
'New-FabricWorkspace2',
122122
'Remove-FabricEventhouse',
123123
'Remove-FabricEventstream',
124124
'Remove-FabricKQLDatabase',
@@ -130,7 +130,12 @@
130130
'Add-FabricWorkspaceRoleAssignment',
131131
'Get-FabricWorkspaceRoleAssignment',
132132
'Get-FabricKQLDashboardDefinition',
133-
'Get-FabricDebugInfo'
133+
'Get-FabricDebugInfo',
134+
135+
'Get-FabricSQLDatabase',
136+
'Remove-FabricSQLDatabase',
137+
'Get-FabricCapacitySkus',
138+
'Confirm-FabricAuthToken'
134139
)
135140

136141
# Cmdlets to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no cmdlets to export.

FabricTools/FabricTools.psm1

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,23 @@
2222
This script is part of the FabricTools module.
2323
#>
2424

25-
$FabricSession = [ordered]@{
25+
$script:FabricSession = [ordered]@{
2626
BaseFabricUrl = 'https://api.fabric.microsoft.com'
27+
ApiUrl = 'https://api.fabric.microsoft.com/v1'
2728
FabricToken = $null
2829
HeaderParams = $null
2930
ContentType = @{'Content-Type' = "application/json"}
3031
KustoURL = "https://api.kusto.windows.net"
32+
AccessToken = $null
33+
}
34+
35+
$script:AzureSession = [ordered]@{
36+
BaseUrl = "https://management.azure.com"
37+
AccessToken = $null
38+
Token = $null
39+
HeaderParams = $null
3140
}
3241

33-
$script:apiUrl = "https://api.fabric.microsoft.com/v1"
34-
$script:resourceUrl = "https://api.fabric.microsoft.com"
35-
$script:fabricToken = $null
3642
# Get all .ps1 files in the (public) Functions folder
3743
$functions = Get-ChildItem -Path "$PSScriptRoot\public" -Filter *.ps1
3844

FabricTools/public/Add-FabricWorkspaceRoleAssignment.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ begin {
5757

5858
# Check if session is established - if not throw error
5959
if ($null -eq $FabricSession.headerParams) {
60-
throw "No session established to Fabric Real-Time Intelligence. Please run Connect-RTISession"
60+
throw "No session established to Fabric Real-Time Intelligence. Please run Connect-FabricAccount"
6161
}
6262

6363
# Create body of request
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<#
2+
.SYNOPSIS
3+
Check whether the Fabric API authentication token is set and not expired and reset it if necessary.
4+
5+
.DESCRIPTION
6+
The Confirm-FabricAuthToken function retrieves the Fabric API authentication token. If the token is not already set, it calls the Set-FabricAuthToken function to set it. It then outputs the token.
7+
8+
.EXAMPLE
9+
Confirm-FabricAuthToken
10+
11+
This command retrieves the Fabric API authentication token.
12+
13+
.INPUTS
14+
None. You cannot pipe inputs to this function.
15+
16+
.OUTPUTS
17+
Returns object as Get-FabricDebugInfo function
18+
19+
.NOTES
20+
21+
#>
22+
23+
function Confirm-FabricAuthToken {
24+
[CmdletBinding()]
25+
param ( )
26+
27+
# Check if the Fabric token is already set
28+
if (!$FabricSession.FabricToken -or !$AzureSession.AccessToken) {
29+
Write-Output "Confirm-FabricAuthToken::Set-FabricAuthToken"
30+
Set-FabricAuthToken | Out-Null
31+
}
32+
33+
$now = (Get-Date)
34+
$s = Get-FabricDebugInfo
35+
if ($s.FabricSession.AccessToken.ExpiresOn -lt $now ) {
36+
Write-Output "Confirm-FabricAuthToken::Set-FabricAuthToken#1"
37+
Set-FabricAuthToken -reset | Out-Null
38+
}
39+
40+
if ($s.AzureSession.AccessToken.ExpiresOn -lt $now ) {
41+
Write-Output "Confirm-FabricAuthToken::Set-FabricAuthToken#2"
42+
Set-FabricAuthToken -reset | Out-Null
43+
}
44+
45+
$s = Get-FabricDebugInfo
46+
return $s
47+
48+
}

FabricTools/public/Connect-FabricAccount.ps1

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ function Connect-FabricAccount {
1414
and in which your Fabric Capacity is.
1515
1616
.EXAMPLE
17-
Connect-RTIAccount `
17+
Connect-FabricAccount `
1818
-TenantID '12345678-1234-1234-1234-123456789012'
1919
2020
.NOTES
@@ -44,8 +44,7 @@ process {
4444
Out-Null
4545

4646
Write-Verbose "Get authentication token"
47-
$FabricSession.FabricToken = (Get-AzAccessToken `
48-
-ResourceUrl $FabricSession.BaseFabricUrl).Token
47+
$FabricSession.FabricToken = (Get-AzAccessToken -ResourceUrl $FabricSession.BaseFabricUrl).Token
4948
Write-Verbose "Token: $($FabricSession.FabricToken)"
5049

5150
Write-Verbose "Setup headers for API calls"

FabricTools/public/Export-FabricItem.ps1

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ Function Export-FabricItem {
3939
(
4040
[string]$path = '.\pbipOutput',
4141
[string]$workspaceId = '',
42-
[scriptblock]$filter = { $_.type -in @("Report", "SemanticModel", "Notebook","SparkJobDefinitionV1") },
42+
[scriptblock]$filter = { $_.type -in @("Report", "SemanticModel", "Notebook", "SparkJobDefinitionV1") },
4343
[string]$itemID = ''
4444
)
4545
if (![string]::IsNullOrEmpty($itemID)) {
@@ -68,18 +68,21 @@ Function Export-FabricItem {
6868
$items = Invoke-FabricAPIRequest -Uri "workspaces/$workspaceId/items" -Method Get
6969

7070
if ($filter) {
71-
$items = $items | Where-Object $filter
71+
$items = $items.value | Where-Object $filter
72+
}
73+
else {
74+
$items = $items.value
7275
}
7376

74-
write-output "Existing items: $($items.Count)"
77+
Write-Output "Existing items: $($items.Count)"
7578

7679
foreach ($item in $items) {
7780
$itemId = $item.id
7881
$itemName = $item.displayName
7982
$itemType = $item.type
8083
$itemOutputPath = "$path\$workspaceId\$($itemName).$($itemType)"
8184

82-
if ($itemType -in @("report", "semanticmodel", "notebook", "SparkJobDefinitionV1")) {
85+
if ($itemType -in @("report", "semanticmodel", "notebook", "SparkJobDefinitionV1", "DataPipeline")) {
8386
write-output "Getting definition of: $itemId / $itemName / $itemType"
8487

8588
$response = Invoke-FabricAPIRequest -Uri "workspaces/$workspaceId/items/$itemId/getDefinition" -Method Post

FabricTools/public/Get-AllFabricCapacities.ps1

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -35,19 +35,12 @@ function Get-AllFabricCapacities {
3535
# Initialize an array to store the results
3636
$res = @()
3737

38+
Get-FabricAuthToken | Out-Null
39+
3840
# If a subscription ID is provided
3941
if ($subscriptionID) {
40-
# If the 'azToken' environment variable is null, connect to the Azure account and set the 'azToken' environment variable.
41-
if ($null -eq $env:azToken) {
42-
# Connect to the Azure account
43-
Connect-azaccount | Out-Null
44-
# Set the context to the provided subscription ID
45-
set-azcontext -SubscriptionId $subscriptionID | Out-Null
46-
# Set the 'azToken' environment variable
47-
$env:aztoken = "Bearer " + (get-azAccessToken).Token
48-
}
4942
# Set the context to the provided subscription ID
50-
set-azcontext -SubscriptionId $subscriptionID | Out-Null
43+
Set-AzContext -SubscriptionId $subscriptionID | Out-Null
5144

5245
# Get all resource groups in the subscription
5346
$rgs = Get-AzResourceGroup
@@ -60,19 +53,12 @@ function Get-AllFabricCapacities {
6053
}
6154
else {
6255
# If no subscription ID is provided, get all subscriptions
63-
if ($null -eq $env:azToken) {
64-
# Connect to the Azure account
65-
Connect-azaccount | Out-Null
66-
# Set the 'azToken' environment variable
67-
$env:aztoken = "Bearer " + (get-azAccessToken).Token
68-
}
69-
# Get all subscriptions
7056
$subscriptions = Get-AzSubscription
7157

7258
# For each subscription, set the context to the subscription ID
7359
foreach ($sub in $subscriptions) {
7460
# Set the context to the subscription ID
75-
set-azcontext -SubscriptionId $sub.id | Out-Null
61+
Set-AzContext -SubscriptionId $sub.id | Out-Null
7662

7763
# Get all resource groups in the subscription
7864
$rgs = Get-AzResourceGroup

FabricTools/public/Get-FabricAPIClusterURI.ps1

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,12 @@ function Get-FabricAPIclusterURI {
2222
# Define aliases for the function for flexibility.
2323
[Alias("Get-FabAPIClusterURI")]
2424
Param (
25-
[Parameter(Mandatory=$false)]
26-
[string]$authToken
2725
)
2826

29-
if ([string]::IsNullOrEmpty($authToken)) {
30-
$authToken = Get-FabricAuthToken
31-
}
27+
$s = Confirm-FabricAuthToken
3228

33-
$fabricHeaders = @{
34-
'Content-Type' = $contentType
35-
'Authorization' = "Bearer {0}" -f $authToken
36-
}
3729
# Make a GET request to the PowerBI API to retrieve the datasets.
38-
$reply = Invoke-RestMethod -uri "https://api.powerbi.com/v1.0/myorg/datasets" -Headers $fabricHeaders -Method GET
30+
$reply = Invoke-RestMethod -uri "https://api.powerbi.com/v1.0/myorg/datasets" -Headers $s.FabricSession.HeaderParams -Method GET
3931

4032
# Extract the '@odata.context' property from the response.
4133
$unaltered = $reply.'@odata.context'

FabricTools/public/Get-FabricAuthToken.ps1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,11 @@ function Get-FabricAuthToken {
2929
)
3030

3131
# Check if the Fabric token is already set
32-
if (!$script:fabricToken) {
32+
if (!$FabricSession.FabricToken) {
3333
# If not, set the Fabric token
3434
Set-FabricAuthToken
3535
}
3636

3737
# Output the Fabric token
38-
return $script:fabricToken
38+
return $FabricSession.FabricToken
3939
}

FabricTools/public/Get-FabricCapacity.ps1

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ function Get-FabricCapacity {
2626
[string]$capacity
2727
)
2828

29+
Confirm-FabricAuthToken | Out-Null
30+
2931
if ($capacity) {
3032
$result = Invoke-FabricAPIRequest -uri "capacities/$capacity" -Method GET
3133
} else {

0 commit comments

Comments
 (0)