Skip to content

Commit 2a7adde

Browse files
authored
Merge pull request #76 from dataplat/connectFabricSP
Add Service Principal parameters for authentication
2 parents 725b9f6 + bdc0836 commit 2a7adde

File tree

2 files changed

+71
-22
lines changed

2 files changed

+71
-22
lines changed

source/Public/Connect-FabricAccount.ps1

Lines changed: 65 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,51 +5,96 @@ function Connect-FabricAccount {
55
Connects to the Fabric WebAPI.
66
77
.DESCRIPTION
8-
Connects to the Fabric WebAPI by using the cmdlet Connect-AzAccount.
9-
This function retrieves the authentication token for the Fabric API and sets up the headers for API calls.
8+
Connects to the Fabric WebAPI by using the cmdlet Connect-AzAccount. This function retrieves the authentication token for the Fabric API and sets up the headers for API calls.
109
1110
.PARAMETER TenantId
12-
The TenantId of the Azure Active Directory tenant you want to connect to
13-
and in which your Fabric Capacity is.
11+
The TenantId of the Azure Active Directory tenant you want to connect to and in which your Fabric Capacity is.
12+
13+
.PARAMETER ServicePrincipalId
14+
The Client ID (AppId) of the service principal used for authentication.
15+
16+
.PARAMETER ServicePrincipalSecret
17+
The **secure string** representing the service principal secret.
18+
19+
.PARAMETER Credential
20+
A PSCredential object representing a user credential (username and secure password).
21+
22+
.EXAMPLE
23+
Connect-FabricAccount -TenantId '12345678-1234-1234-1234-123456789012'
24+
25+
Connects to the stated Tenant with exisitng credentials
26+
27+
.EXAMPLE
28+
$credential = Get-Credential
29+
Connect-FabricAccount -TenantId 'xxx' -credential $credential
30+
31+
Prompts for Service Principal id and secret and connects as that Service Principal
1432
1533
.EXAMPLE
16-
Connect-FabricAccount `
17-
-TenantID '12345678-1234-1234-1234-123456789012'
34+
Connect-FabricAccount -TenantId 'xxx' -ServicePrincipalId 'appId' -ServicePrincipalSecret $secret
35+
36+
Connects as Service Principal using id and secret
1837
1938
.NOTES
2039
2140
Revsion History:
2241
2342
- 2024-12-22 - FGE: Added Verbose Output
43+
- 2025-05-26 - Jojobit: Added Service Principal support, with secure string handling and parameter descriptions, as supported by the original FabTools module
2444
2545
.LINK
2646
Connect-AzAccount https://learn.microsoft.com/de-de/powershell/module/az.accounts/connect-azaccount?view=azps-12.4.0
27-
2847
#>
2948

30-
[CmdletBinding()]
49+
[CmdletBinding(SupportsShouldProcess)]
3150
param (
32-
[Parameter(Mandatory = $true)]
33-
[string]$TenantId
51+
[Parameter(Mandatory = $false, HelpMessage = "Azure AD Tenant ID.")]
52+
[string]$tenantId,
53+
54+
[Parameter(Mandatory = $false, HelpMessage = "AppId of the service principal.")]
55+
[string]$servicePrincipalId,
56+
57+
[Parameter(Mandatory = $false, HelpMessage = "Secure secret of the service principal.")]
58+
[SecureString]$servicePrincipalSecret,
59+
60+
[Parameter(Mandatory = $false, HelpMessage = "User credential.")]
61+
[PSCredential]$credential
3462
)
3563

3664
begin {
3765
}
3866

3967
process {
4068
Write-Verbose "Connect to Azure Account"
41-
Connect-AzAccount -TenantId $TenantId | Out-Null
42-
43-
Write-Verbose "Get authentication token"
44-
$FabricSession.FabricToken = (Get-AzAccessToken -ResourceUrl $FabricSession.ResourceUrl).Token
45-
Write-Verbose "Token: $($FabricSession.FabricToken)"
4669

47-
Write-Verbose "Setup headers for API calls"
48-
$FabricSession.HeaderParams = @{'Authorization' = "Bearer {0}" -f $FabricSession.FabricToken }
49-
Write-Verbose "HeaderParams: $($FabricSession.HeaderParams)"
70+
if ($servicePrincipalId) {
71+
$credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $servicePrincipalId, $servicePrincipalSecret
72+
$null = Connect-AzAccount -ServicePrincipal -TenantId $tenantId -Credential $credential
73+
}
74+
elseif ($null -ne $credential) {
75+
$null = Connect-AzAccount -Credential $credential -Tenant $tenantId
76+
}
77+
else {
78+
$null = Connect-AzAccount
79+
}
80+
81+
$azContext = Get-AzContext
82+
83+
Write-Verbose "Connected: $($azContext.Account)"
84+
85+
if ($PSCmdlet.ShouldProcess("Setting Fabric authentication token for $($azContext.Account)")) {
86+
87+
Write-Verbose "Get authentication token"
88+
$FabricSession.FabricToken = (Get-AzAccessToken -ResourceUrl $FabricSession.ResourceUrl).Token
89+
Write-Verbose "Token: $($FabricSession.FabricToken)"
90+
}
91+
92+
if ($PSCmdlet.ShouldProcess("Setting Fabric headers for $($azContext.Account)")) {
93+
Write-Verbose "Setup headers for API calls"
94+
$FabricSession.HeaderParams = @{'Authorization' = "Bearer {0}" -f $FabricSession.FabricToken }
95+
Write-Verbose "HeaderParams: $($FabricSession.HeaderParams)"
96+
}
5097
}
51-
5298
end {
5399
}
54-
55100
}

tests/Unit/Connect-FabricAccount.Tests.ps1

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ param(
33
$ModuleName = "FabricTools",
44
$expectedParams = @(
55
"TenantId"
6+
"ServicePrincipalId"
7+
"ServicePrincipalSecret"
8+
"Credential"
69
"Verbose"
710
"Debug"
811
"ErrorAction"
@@ -15,7 +18,9 @@ param(
1518
"OutVariable"
1619
"OutBuffer"
1720
"PipelineVariable"
18-
21+
"WhatIf"
22+
"Confirm"
23+
1924
)
2025
)
2126

@@ -43,4 +48,3 @@ Describe "Connect-FabricAccount" -Tag "UnitTests" {
4348
}
4449
}
4550
}
46-

0 commit comments

Comments
 (0)