Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PowerShell module for Microsoft Fabric #23

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
215 changes: 215 additions & 0 deletions powershell/FabricACEToolkit/FabricACEToolkit.psd1
Original file line number Diff line number Diff line change
@@ -0,0 +1,215 @@
#
# Module manifest for module 'FabricACEToolkit'
#
# Generated by: FabricACEToolkit
#
# Generated on: 1/20/2025
#

@{

# Script module or binary module file associated with this manifest.
RootModule = 'FabricACEToolkit.psm1'

# Version number of this module.
ModuleVersion = '1.0'

# Supported PSEditions
# CompatiblePSEditions = @()

# ID used to uniquely identify this module
GUID = 'd0110b5c-cfcc-4bcc-8049-468880cf66c8'

# Author of this module
Author = 'FabricACEToolkit'

# Company or vendor of this module
CompanyName = 'FabricACEToolkit'

# Copyright statement for this module
Copyright = '2025 FabricACEToolkit by Tiago Balabuch'

# Description of the functionality provided by this module
Description = 'FabricACEToolkit'

# Minimum version of the PowerShell engine required by this module
# PowerShellVersion = ''

# Name of the PowerShell host required by this module
# PowerShellHostName = ''

# Minimum version of the PowerShell host required by this module
# PowerShellHostVersion = ''

# Minimum version of Microsoft .NET Framework required by this module. This prerequisite is valid for the PowerShell Desktop edition only.
# DotNetFrameworkVersion = ''

# Minimum version of the common language runtime (CLR) required by this module. This prerequisite is valid for the PowerShell Desktop edition only.
# ClrVersion = ''

# Processor architecture (None, X86, Amd64) required by this module
# ProcessorArchitecture = ''

# Modules that must be imported into the global environment prior to importing this module
# RequiredModules = @()

# Assemblies that must be loaded prior to importing this module
# RequiredAssemblies = @()

# Script files (.ps1) that are run in the caller's environment prior to importing this module.
# ScriptsToProcess = @()

# Type files (.ps1xml) to be loaded when importing this module
# TypesToProcess = @()

# Format files (.ps1xml) to be loaded when importing this module
# FormatsToProcess = @()

# Modules to import as nested modules of the module specified in RootModule/ModuleToProcess
# NestedModules = @()

# Functions 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 functions to export.
FunctionsToExport = 'Get-FabricCapacity', 'Get-FabricDashboard', 'Get-FabricDataPipeline',
'New-FabricDataPipeline', 'Remove-FabricDataPipeline',
'Update-FabricDataPipeline', 'Get-FabricDatamart',
'Assign-FabricDomainWorkspaceByCapacity',
'Assign-FabricDomainWorkspaceById',
'Assign-FabricDomainWorkspaceByPrincipal',
'Assign-FabricDomainWorkspaceRoleAssignment', 'Get-FabricDomain',
'Get-FabricDomainWorkspace', 'New-FabricDomain',
'Remove-FabricDomain', 'Unassign-FabricDomainWorkspace',
'Unassign-FabricDomainWorkspaceRoleAssignment',
'Update-FabricDomain', 'Get-FabricEnvironment',
'Get-FabricEnvironmentLibrary', 'Get-FabricEnvironmentSparkCompute',
'Get-FabricEnvironmentStagingLibrary',
'Get-FabricEnvironmentStagingSparkCompute', 'New-FabricEnvironment',
'Publish-FabricEnvironment', 'Remove-FabricEnvironment',
'Remove-FabricEnvironmentStagingLibrary',
'Stop-FabricEnvironmentPublish', 'Update-FabricEnvironment',
'Update-FabricEnvironmentStagingSparkCompute',
'Upload-FabricEnvironmentStagingLibrary', 'Get-FabricEventhouse',
'Get-FabricEventhouseDefinition', 'New-FabricEventhouse',
'Remove-FabricEventhouse', 'Update-FabricEventhouse',
'Update-FabricEventhouseDefinition', 'Get-FabricEventstream',
'Get-FabricEventstreamDefinition', 'New-FabricEventstream',
'Remove-FabricEventstream', 'Update-FabricEventstream',
'Update-FabricEventstreamDefinition', 'Get-FabricKQLDashboard',
'Get-FabricKQLDashboardDefinition', 'New-FabricKQLDashboard',
'Remove-FabricKQLDashboard', 'Update-FabricKQLDashboard',
'Update-FabricKQLDashboardDefinition', 'Get-FabricKQLDatabase',
'Get-FabricKQLDatabaseDefinition', 'New-FabricKQLDatabase',
'Remove-FabricKQLDatabase', 'Update-FabricKQLDatabase',
'Update-FabricKQLDatabaseDefinition', 'Get-FabricKQLQueryset',
'Get-FabricKQLQuerysetDefinition', 'New-FabricKQLQueryset',
'Remove-FabricKQLQueryset', 'Update-FabricKQLQueryset',
'Update-FabricKQLQuerysetDefinition', 'Get-FabricLakehouse',
'Get-FabricLakehouseTable', 'Load-FabricLakehouseTable',
'New-FabricLakehouse', 'Remove-FabricLakehouse',
'Start-FabricLakehouseTableMaintenance', 'Update-FabricLakehouse',
'Get-FabricMirroredDatabase',
'Get-FabricMirroredDatabaseDefinition',
'Get-FabricMirroredDatabaseStatus',
'Get-FabricMirroredDatabaseTableStatus',
'New-FabricMirroredDatabase', 'Remove-FabricMirroredDatabase',
'Start-FabricMirroredDatabaseMirroring',
'Stop-FabricMirroredDatabaseMirroring',
'Update-FabricMirroredDatabase',
'Update-FabricMirroredDatabaseDefinition',
'Get-FabricMirroredWarehouse', 'Get-FabricMLExperiment',
'New-FabricMLExperiment', 'Remove-FabricMLExperiment',
'Update-FabricMLExperiment', 'Get-FabricMLModel', 'New-FabricMLModel',
'Remove-FabricMLModel', 'Update-FabricMLModel', 'Get-FabricNotebook',
'Get-FabricNotebookDefinition', 'New-FabricNotebook',
'New-FabricNotebookNEW', 'Remove-FabricNotebook',
'Update-FabricNotebook', 'Update-FabricNotebookDefinition',
'Get-FabricPaginatedReport', 'Update-FabricPaginatedReport',
'Get-FabricReflex', 'Get-FabricReflexDefinition', 'New-FabricReflex',
'Remove-FabricReflex', 'Update-FabricReflex',
'Update-FabricReflexDefinition', 'Get-FabricReport',
'Get-FabricReportDefinition', 'New-FabricReport',
'Remove-FabricReport', 'Update-FabricReport',
'Update-FabricReportDefinition', 'Get-FabricSemanticModel',
'Get-FabricSemanticModelDefinition', 'New-FabricSemanticModel',
'Remove-FabricSemanticModel', 'Update-FabricSemanticModel',
'Update-FabricSemanticModelDefinition', 'Get-FabricSparkCustomPool',
'Get-FabricSparkSettings', 'New-FabricSparkCustomPool',
'Remove-FabricSparkCustomPool', 'Update-FabricSparkCustomPool',
'Update-FabricSparkSettings', 'Get-FabricSparkJobDefinition',
'Get-FabricSparkJobDefinitionDefinition',
'New-FabricSparkJobDefinition', 'Remove-FabricSparkJobDefinition',
'Start-FabricSparkJobDefinitionOnDemand',
'Update-FabricSparkJobDefinition',
'Update-FabricSparkJobDefinitionDefinition',
'Get-FabricSQLEndpoint', 'Get-FabricTenantSetting',
'Get-FabricTenantSettingOverridesCapacity', 'Convert-FromBase64',
'Convert-ToBase64', 'Get-FabricLongRunningOperation',
'Get-FabricLongRunningOperationResult', 'Set-FabricApiHeaders',
'Get-FabricWarehouse', 'New-FabricWarehouse',
'Remove-FabricWarehouse', 'Update-FabricWarehouse',
'Add-FabricWorkspaceIdentity', 'Add-FabricWorkspaceRoleAssignment',
'Assign-FabricWorkspaceCapacity', 'Get-FabricWorkspace',
'Get-FabricWorkspaceRoleAssignment', 'New-FabricWorkspace',
'Remove-FabricWorkspace', 'Remove-FabricWorkspaceIdentity',
'Remove-FabricWorkspaceRoleAssignment',
'Unassign-FabricWorkspaceCapacity', 'Update-FabricWorkspace',
'Update-FabricWorkspaceRoleAssignment'

# 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.
CmdletsToExport = @()

# Variables to export from this module
VariablesToExport = 'FabricConfig'

# 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.
AliasesToExport = @()

# DSC resources to export from this module
# DscResourcesToExport = @()

# List of all modules packaged with this module
# ModuleList = @()

# List of all files packaged with this module
# FileList = @()

# Private data to pass to the module specified in RootModule/ModuleToProcess. This may also contain a PSData hashtable with additional module metadata used by PowerShell.
PrivateData = @{

PSData = @{

# Tags applied to this module. These help with module discovery in online galleries.
# Tags = @()

# A URL to the license for this module.
# LicenseUri = ''

# A URL to the main website for this project.
# ProjectUri = ''

# A URL to an icon representing this module.
# IconUri = ''

# ReleaseNotes of this module
# ReleaseNotes = ''

# Prerelease string of this module
# Prerelease = ''

# Flag to indicate whether the module requires explicit user acceptance for install/update/save
# RequireLicenseAcceptance = $false

# External dependent modules of this module
# ExternalModuleDependencies = @()

} # End of PSData hashtable

} # End of PrivateData hashtable

# HelpInfo URI of this module
# HelpInfoURI = ''

# Default prefix for commands exported from this module. Override the default prefix using Import-Module -Prefix.
# DefaultCommandPrefix = ''

}

65 changes: 65 additions & 0 deletions powershell/FabricACEToolkit/FabricACEToolkit.psm1
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
$directorySeparator = [System.IO.Path]::DirectorySeparatorChar
$moduleName = $PSScriptRoot.Split($directorySeparator)[-1]
$moduleManifest = $PSScriptRoot + $directorySeparator + $moduleName + '.psd1'
$publicFunctionsPath = $PSScriptRoot + $directorySeparator + 'Public' + $directorySeparator #+ 'ps1'
$privateFunctionsPath = $PSScriptRoot + $directorySeparator + 'Private' + $directorySeparator #+ 'ps1'
$currentManifest = Test-ModuleManifest $moduleManifest

$aliases = @()
$publicFunctions = Get-ChildItem -Path $publicFunctionsPath -Recurse | Where-Object {$_.Extension -eq '.ps1'}
$privateFunctions = Get-ChildItem -Path $privateFunctionsPath -Recurse | Where-Object {$_.Extension -eq '.ps1'}
$publicFunctions | ForEach-Object { . $_.FullName }
$privateFunctions | ForEach-Object { . $_.FullName }
# Configuration object for module-wide settings

$FabricConfig = @{
BaseUrl = "https://api.fabric.microsoft.com/v1"
ResourceUrl = "https://api.fabric.microsoft.com"
FabricHeaders = @{}
TenantIdGlobal = ""
TokenExpiresOn = ""

}

Export-ModuleMember -Variable FabricConfig

$publicFunctions | ForEach-Object { # Export all of the public functions from this module

# The command has already been sourced in above. Query any defined aliases.
$alias = Get-Alias -Definition $_.BaseName -ErrorAction SilentlyContinue
if ($alias) {
$aliases += $alias
Export-ModuleMember -Function $_.BaseName -Alias $alias
#Export-ModuleMember -Variable FabricConfig, TenantIdGlobal, TokenExpiresOn
}
else {
Export-ModuleMember -Function $_.BaseName
#Export-ModuleMember -Variable FabricConfig, TenantIdGlobal, TokenExpiresOn
}

}

$functionsAdded = $publicFunctions | Where-Object {$_.BaseName -notin $currentManifest.ExportedFunctions.Keys}
$functionsRemoved = $currentManifest.ExportedFunctions.Keys | Where-Object {$_ -notin $publicFunctions.BaseName}
$aliasesAdded = $aliases | Where-Object {$_ -notin $currentManifest.ExportedAliases.Keys}
$aliasesRemoved = $currentManifest.ExportedAliases.Keys | Where-Object {$_ -notin $aliases}

if ($functionsAdded -or $functionsRemoved -or $aliasesAdded -or $aliasesRemoved) {
try {

$updateModuleManifestParams = @{}
$updateModuleManifestParams.Add('Path', $moduleManifest)
$updateModuleManifestParams.Add('ErrorAction', 'Stop')
if ($aliases.Count -gt 0) { $updateModuleManifestParams.Add('AliasesToExport', $aliases) }
if ($publicFunctions.Count -gt 0) { $updateModuleManifestParams.Add('FunctionsToExport', $publicFunctions.BaseName) }

Update-ModuleManifest @updateModuleManifestParams

}
catch {

$_ | Write-Error

}

}
57 changes: 57 additions & 0 deletions powershell/FabricACEToolkit/Private/Get-FileDefinitionParts.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
function Get-FileDefinitionParts {
[CmdletBinding()]
param (
[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[string]$sourceDirectory
)
try {
if (-Not (Test-Path $sourceDirectory)) {
Write-Message -Message "The specified source directory does not exist: $sourceDirectory" -Level Error
throw
}

# Get all files from the directory recursively
Write-Message -Message "Get all files from the directory recursively" -Level Debug
$fileList = Get-ChildItem -Path $sourceDirectory -File -Recurse

# Initialize the output JSON object
$jsonObject = @{ parts = @() }

# Loop through the files to create parts dynamically
Write-Message -Message "Loop through the files to create parts dynamically" -Level Debug
foreach ($file in $fileList) {

$relativePath = $file.FullName.Substring($sourceDirectory.Length + 1) -replace "\\", "/"
Write-Message -Message "File found: $relativePath" -Level Debug
Write-Message -Message "Starting encode to base64" -Level Debug

$base64Content = Convert-ToBase64 -filePath $file.FullName
Write-Message -Message "Adding part to json object" -Level Debug

$jsonObject.parts += @{
path = $relativePath
payload = $base64Content
payloadType = "InlineBase64"
}
}
Write-Message -Message "Loop through the files finished" -Level Debug

return $jsonObject
Write-Message -Message "Parts returned" -Level Debug
}

catch {
# Step 4: Handle and log errors
$errorDetails = $_.Exception.Message
Write-Message -Message "An error occurred while getting file definition parts: $errorDetails" -Level Error
throw "An error occurred while encoding to Base64: $_"
}
}


# Example usage
#$sourceDirectory = "C:\temp\API\Notebook"
#Get-FileParts -sourceDirectory $sourceDirectory
#$fileParts = Get-FileParts -sourceDirectory $sourceDirectory
#$fileParts | ConvertTo-Json -Depth 10
57 changes: 57 additions & 0 deletions powershell/FabricACEToolkit/Private/Test-TokenExpired.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<#
.SYNOPSIS
Checks if the Fabric token is expired and logs appropriate messages.

.DESCRIPTION
The `Test-TokenExpired` function checks the expiration status of the Fabric token stored in the `$FabricConfig.TokenExpiresOn` variable.
If the token is expired, it logs an error message and provides guidance for refreshing the token.
Otherwise, it logs that the token is still valid.

.PARAMETER FabricConfig
The configuration object containing the token expiration details.

.EXAMPLE
Test-TokenExpired -FabricConfig $config

Checks the token expiration status using the provided `$config` object.

.NOTES
- Ensure the `FabricConfig` object includes a valid `TokenExpiresOn` property of type `DateTimeOffset`.
- Requires the `Write-Message` function for logging.

.AUTHOR
Tiago Balabuch
#>
function Test-TokenExpired {
[CmdletBinding()]
param ()

try {
# Ensure required properties have valid values
if ([string]::IsNullOrWhiteSpace($FabricConfig.TenantIdGlobal) -or
[string]::IsNullOrWhiteSpace($FabricConfig.TokenExpiresOn)) {
Write-Message -Message "Token details are missing. Please run 'Set-FabricApiHeaders' to configure them." -Level Error
throw "MissingTokenDetailsException: Token details are missing."
}

# Convert the TokenExpiresOn value to a DateTime object
$tokenExpiryDate = [datetimeoffset]::Parse($FabricConfig.TokenExpiresOn)

# Check if the token is expired
if ($tokenExpiryDate -le [datetimeoffset]::Now) {
Write-Message -Message "Your authentication token has expired. Please sign in again to refresh your session." -Level Warning
#throw "TokenExpiredException: Token has expired."
#Set-FabricApiHeaders -tenantId $FabricConfig.TenantIdGlobal
}

# Log valid token status
Write-Message -Message "Token is still valid. Expiry time: $($tokenExpiryDate.ToString("u"))" -Level Debug
} catch [System.FormatException] {
Write-Message -Message "Invalid 'TokenExpiresOn' format in the FabricConfig object. Ensure it is a valid datetime string." -Level Error
throw "FormatException: Invalid TokenExpiresOn value."
} catch {
# Log unexpected errors with details
Write-Message -Message "An unexpected error occurred: $_" -Level Error
throw $_
}
}
Loading