Skip to content

Commit 37b8fbf

Browse files
committed
- Removed Fab aliases
- Added few functions from Tiago - Added SQL Database functions using new format - Exposed 'Test-FabricApiResponse' function
1 parent 491e4c6 commit 37b8fbf

16 files changed

+666
-103
lines changed

FabricTools/FabricTools.psd1

Lines changed: 5 additions & 30 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.10.0'
15+
ModuleVersion = '0.20.0'
1616

1717
# Supported PSEditions
1818
# CompatiblePSEditions = @()
@@ -137,7 +137,9 @@
137137
'Remove-FabricSQLDatabase',
138138
'Get-FabricCapacitySkus',
139139
'Confirm-FabricAuthToken',
140-
'Get-FabricConnection'
140+
'Get-FabricConnection',
141+
'New-FabricSQLDatabase',
142+
'Test-FabricApiResponse'
141143
)
142144

143145
# 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.
@@ -149,37 +151,10 @@
149151
# Aliases 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 aliases to export.
150152
AliasesToExport = @(
151153
"Export-FabItem",
152-
"Get-AllFabCapacities",
153-
"Get-AllFabDatasetRefreshes",
154-
"Get-FabAPIclusterURI",
155-
"Get-FabAuthToken",
156-
"Get-FabCapacity",
157-
"Get-FabCapacityRefreshables",
158-
"Get-FabCapacityState",
159-
"Get-FabCapacityTenantOverides",
160-
"Get-FabCapacityWorkload",
161-
"Get-FabDataset",
162-
"Get-FabDatasetRefreshes",
163-
"Get-FabItem",
164-
"Get-FabReport",
165154
"Get-FabricDataset",
166155
"Get-FabricReport",
167-
"Get-FabTenantSettings",
168-
"Get-FabUsagemetricsQuery",
169-
"Get-FabWorkspace",
170-
"Get-FabWorkspaceDatasetRefreshes",
171-
"Get-FabWorkspaceUsageMetricsData",
172-
"Get-FabWorkspaceUsers",
173-
"Invoke-FabDatasetRefresh",
174156
"Login-Fabric",
175-
"Logout-Fabric",
176-
"New-FabWorkspace",
177-
"New-FabWorkspaceUsageMetrics",
178-
"Register-FabWorkspaceToCapacity",
179-
"Remove-FabWorkspace",
180-
"Resume-FabCapacity",
181-
"Suspend-FabCapacity",
182-
"Unregister-FabWorkspaceToCapacity"
157+
"Logout-Fabric"
183158
)
184159

185160
# DSC resources to export from this module

FabricTools/FabricTools.psm1

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
$script:FabricSession = [ordered]@{
2626
BaseApiUrl = 'https://api.fabric.microsoft.com/v1'
27+
ResourceUrl = 'https://api.fabric.microsoft.com'
2728
FabricToken = $null
2829
HeaderParams = $null
2930
ContentType = @{'Content-Type' = "application/json"}
@@ -42,17 +43,30 @@ $script:PowerBI = [ordered]@{
4243
BaseApiUrl = "https://api.powerbi.com/v1.0/myorg"
4344
}
4445

46+
$FabricConfig = @{
47+
BaseUrl = "https://api.fabric.microsoft.com/v1"
48+
ResourceUrl = "https://api.fabric.microsoft.com"
49+
FabricHeaders = @{}
50+
TenantIdGlobal = ""
51+
TokenExpiresOn = ""
52+
}
53+
54+
Export-ModuleMember -Variable FabricConfig
55+
4556
# Get all .ps1 files in the (public) Functions folder
46-
$functions = Get-ChildItem -Path "$PSScriptRoot\public" -Filter *.ps1
57+
$publicFunctions = Get-ChildItem -Path "$PSScriptRoot\public" -Filter '*.ps1' -Recurse
4758

4859
# Loop over each function file
49-
foreach ($function in $functions) {
60+
foreach ($function in $publicFunctions) {
5061
# Dot-source the function
5162
. $function.fullname
5263
# Export the function as a module member
5364
Export-ModuleMember -Function $function.basename
5465
}
5566

67+
$privateFunctions = Get-ChildItem -Path "$PSScriptRoot\private" -Filter '*.ps1' -Recurse
68+
$privateFunctions | ForEach-Object { . $_.FullName }
69+
5670
# Set aliases for PowerBI functions
5771
Set-Alias -Name Login-Fabric -Value Login-PowerBI
5872
Export-ModuleMember -Alias Login-Fabric
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<#
2+
.SYNOPSIS
3+
Checks if the Fabric token is expired and logs appropriate messages.
4+
5+
.DESCRIPTION
6+
The `Test-TokenExpired` function checks the expiration status of the Fabric token stored in the `$FabricConfig.TokenExpiresOn` variable.
7+
If the token is expired, it logs an error message and provides guidance for refreshing the token.
8+
Otherwise, it logs that the token is still valid.
9+
10+
.PARAMETER FabricConfig
11+
The configuration object containing the token expiration details.
12+
13+
.EXAMPLE
14+
Test-TokenExpired -FabricConfig $config
15+
16+
Checks the token expiration status using the provided `$config` object.
17+
18+
.NOTES
19+
- Ensure the `FabricConfig` object includes a valid `TokenExpiresOn` property of type `DateTimeOffset`.
20+
- Requires the `Write-Message` function for logging.
21+
22+
.AUTHOR
23+
Tiago Balabuch
24+
#>
25+
function Test-TokenExpired {
26+
[CmdletBinding()]
27+
param ()
28+
29+
Confirm-FabricAuthToken | Out-Null
30+
31+
Write-Message -Message "Validating token..." -Level Debug
32+
33+
try {
34+
# Ensure required properties have valid values
35+
if ([string]::IsNullOrWhiteSpace($FabricConfig.TenantIdGlobal) -or
36+
[string]::IsNullOrWhiteSpace($FabricConfig.TokenExpiresOn)) {
37+
Write-Message -Message "Token details are missing. Please run 'Set-FabricApiHeaders' to configure them." -Level Error
38+
throw "MissingTokenDetailsException: Token details are missing."
39+
}
40+
41+
# Convert the TokenExpiresOn value to a DateTime object
42+
if ($FabricConfig.TokenExpiresOn.GetType() -eq [datetimeoffset]) {
43+
$tokenExpiryDate = $FabricConfig.TokenExpiresOn
44+
} else {
45+
$tokenExpiryDate = [datetimeoffset]::Parse($FabricConfig.TokenExpiresOn)
46+
}
47+
48+
# Check if the token is expired
49+
if ($tokenExpiryDate -le [datetimeoffset]::Now) {
50+
Write-Message -Message "Your authentication token has expired. Please sign in again to refresh your session." -Level Warning
51+
#throw "TokenExpiredException: Token has expired."
52+
#Set-FabricApiHeaders -tenantId $FabricConfig.TenantIdGlobal
53+
}
54+
55+
# Log valid token status
56+
Write-Message -Message "Token is still valid. Expiry time: $($tokenExpiryDate.ToString("u"))" -Level Debug
57+
} catch [System.FormatException] {
58+
Write-Message -Message "Invalid 'TokenExpiresOn' format in the FabricConfig object. Ensure it is a valid datetime string." -Level Error
59+
throw "FormatException: Invalid TokenExpiresOn value."
60+
} catch {
61+
# Log unexpected errors with details
62+
Write-Message -Message "An unexpected error occurred: $_" -Level Error
63+
throw $_
64+
}
65+
Write-Message -Message "Token validation completed." -Level Debug
66+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<#
2+
.SYNOPSIS
3+
Logs messages with different severity levels to the console and optionally to a file.
4+
5+
.DESCRIPTION
6+
The `Write-Message` function provides a unified way to log messages with levels such as Info, Error, Alert, Verbose, and Debug.
7+
It supports logging to the console with color-coded messages and optionally writing logs to a file with timestamps.
8+
9+
.PARAMETER Message
10+
The message to log. Supports pipeline input.
11+
12+
.PARAMETER Level
13+
Specifies the log level. Supported values are Info, Error, Alert, Verbose, and Debug.
14+
15+
.PARAMETER LogFile
16+
(Optional) Specifies a file path to write the log messages to. If not provided, messages are only written to the console.
17+
18+
.EXAMPLE
19+
Write-Message -Message "This is an info message." -Level Info
20+
21+
Logs an informational message to the console.
22+
23+
.EXAMPLE
24+
Write-Message -Message "Logging to file." -Level Info -LogFile "C:\Logs\MyLog.txt"
25+
26+
Logs an informational message to the console and writes it to a file.
27+
28+
.EXAMPLE
29+
"Pipeline message" | Write-Message -Level Alert
30+
31+
Logs a message from the pipeline with an Alert level.
32+
33+
.NOTES
34+
Author: Tiago Balabuch
35+
#>
36+
37+
function Write-Message {
38+
[CmdletBinding()]
39+
param (
40+
[Parameter(Mandatory, ValueFromPipeline)]
41+
[string]$Message,
42+
43+
[Parameter()]
44+
[ValidateSet("Message","Info", "Error", "Warning","Critical", "Verbose", "Debug", IgnoreCase = $true)]
45+
[string]$Level = "Info",
46+
47+
[Parameter()]
48+
[string]$LogFile
49+
)
50+
51+
process {
52+
try {
53+
# Format timestamp
54+
$timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
55+
56+
# Construct log message
57+
$logMessage = "[$timestamp] [$Level] $Message"
58+
59+
# Write log message to console with colors
60+
switch ($Level) {
61+
"Message" { Write-Host $logMessage -ForegroundColor White }
62+
"Info" { Write-Host $logMessage -ForegroundColor Green }
63+
"Error" { Write-Host $logMessage -ForegroundColor Red }
64+
"Warning" { Write-Host $logMessage -ForegroundColor Yellow }
65+
"Critical" { Write-Host $logMessage -ForegroundColor Red }
66+
"Verbose" { Write-Verbose $logMessage }
67+
"Debug" { Write-Debug $logMessage }
68+
69+
}
70+
71+
# Optionally write log message to a file
72+
if ($LogFile) {
73+
try {
74+
Add-Content -Path $LogFile -Value $logMessage -Encoding UTF8
75+
} catch {
76+
# Catch and log any errors when writing to file
77+
Write-Host "[ERROR] Failed to write to log file '$LogFile': $_" -ForegroundColor Red
78+
}
79+
}
80+
} catch {
81+
Write-Host "[ERROR] An unexpected error occurred: $_" -ForegroundColor Red
82+
}
83+
}
84+
}

FabricTools/public/Get-AllFabricCapacities.ps1

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
#>
2424
function Get-AllFabricCapacities {
2525
# Define aliases for the function for flexibility.
26-
[Alias("Get-AllFabCapacities")]
2726

2827
# Define parameters for the function
2928
Param (

FabricTools/public/Get-AllFabricDatasetRefreshes.ps1

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ The function makes a GET request to the PowerBI API to retrieve the refreshes. I
1717
# This function retrieves all refreshes for all datasets in all PowerBI workspaces.
1818
function Get-AllFabricDatasetRefreshes {
1919
# Define aliases for the function for flexibility.
20-
[Alias("Get-AllFabDatasetRefreshes")]
2120

2221
Confirm-FabricAuthToken | Out-Null
2322

FabricTools/public/Get-FabricDebugInfo.ps1

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ process {
3535

3636
return @{
3737
FabricSession = $script:FabricSession
38-
AzureSession = $script:AzureSession
38+
AzureSession = $script:AzureSession
39+
FabricConfig = $script:FabricConfig
3940
}
4041

4142
}

FabricTools/public/Remove-FabricSQLDatabase.ps1

Lines changed: 0 additions & 66 deletions
This file was deleted.

0 commit comments

Comments
 (0)