Skip to content

Commit 50c0bfd

Browse files
Connect-DbaInstance - Add -AuthenticationType parameter for Entra ID support (#10271)
1 parent 1f70b62 commit 50c0bfd

2 files changed

Lines changed: 42 additions & 2 deletions

File tree

public/Connect-DbaInstance.ps1

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,15 @@ function Connect-DbaInstance {
144144
Authenticates to Azure SQL Database using an access token generated by Get-AzAccessToken or New-DbaAzAccessToken.
145145
Use this for service principal authentication or when integrating with Azure automation that provides pre-generated tokens. Tokens expire after one hour and cannot be renewed.
146146
147+
.PARAMETER AuthenticationType
148+
Specifies the authentication method for connecting to Azure SQL or Entra ID-protected SQL Server instances.
149+
Use "ActiveDirectoryInteractive" for Entra ID (Azure AD) authentication with MFA — a browser dialog will prompt you to select your Entra ID account.
150+
Use "ActiveDirectoryIntegrated" for Entra ID integrated authentication using your current Windows session.
151+
Use "ActiveDirectoryPassword" for Entra ID authentication with a username and password via SqlCredential.
152+
Use "ActiveDirectoryServicePrincipal" for service principal authentication (client ID and secret via SqlCredential).
153+
Use "ActiveDirectoryManagedIdentity" for managed identity authentication in Azure-hosted environments.
154+
Use "ActiveDirectoryDeviceCodeFlow" for device code flow authentication.
155+
147156
.PARAMETER DedicatedAdminConnection
148157
Creates a dedicated administrator connection (DAC) for emergency access to SQL Server.
149158
Use this when SQL Server is unresponsive to regular connections, allowing you to diagnose and resolve critical issues. Remember to manually disconnect the connection when finished.
@@ -343,6 +352,25 @@ function Connect-DbaInstance {
343352
If a server fails due to certificate validation, automatically retries with TrustServerCertificate enabled.
344353
This provides a secure-by-default approach for mixed environments without requiring separate connection logic.
345354
355+
.EXAMPLE
356+
PS C:\> $server = Connect-DbaInstance -SqlInstance sql01 -AuthenticationType ActiveDirectoryInteractive
357+
358+
Connects to a SQL Server instance (Azure SQL VM, Azure SQL Database, Azure SQL Managed Instance, or Fabric SQL Database)
359+
using Entra ID (Azure AD) interactive authentication with MFA. A browser dialog will appear prompting you to select
360+
your Entra ID account and complete any required MFA steps.
361+
362+
.EXAMPLE
363+
PS C:\> $server = Connect-DbaInstance -SqlInstance myserver.database.windows.net -Database mydb -AuthenticationType ActiveDirectoryInteractive
364+
365+
Connects to an Azure SQL Database using Entra ID interactive authentication with MFA.
366+
A browser dialog will appear to complete authentication.
367+
368+
.EXAMPLE
369+
PS C:\> $server = Connect-DbaInstance -SqlInstance sql01 -AuthenticationType ActiveDirectoryIntegrated
370+
371+
Connects to a SQL Server instance using Entra ID integrated authentication.
372+
Uses the currently signed-in Entra ID identity without prompting for credentials.
373+
346374
#>
347375
[CmdletBinding()]
348376
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidUsingConvertToSecureStringWithPlainText", "")]
@@ -383,6 +411,8 @@ function Connect-DbaInstance {
383411
[string]$AzureDomain = "database.windows.net",
384412
[string]$Tenant = (Get-DbatoolsConfigValue -FullName 'azure.tenantid'),
385413
[psobject]$AccessToken,
414+
[ValidateSet('ActiveDirectoryIntegrated', 'ActiveDirectoryInteractive', 'ActiveDirectoryPassword', 'ActiveDirectoryServicePrincipal', 'ActiveDirectoryManagedIdentity', 'ActiveDirectoryDeviceCodeFlow')]
415+
[string]$AuthenticationType,
386416
[switch]$DedicatedAdminConnection,
387417
[switch]$DisableException
388418
)
@@ -620,7 +650,7 @@ function Connect-DbaInstance {
620650

621651
# Check for ignored parameters
622652
# We do not check for SqlCredential as this parameter is widely used even if a server SMO is passed in and we don't want to output a message for that
623-
$ignoredParameters = 'BatchSeparator', 'ClientName', 'ConnectTimeout', 'EncryptConnection', 'LockTimeout', 'MaxPoolSize', 'MinPoolSize', 'NetworkProtocol', 'PacketSize', 'PooledConnectionLifetime', 'SqlExecutionModes', 'TrustServerCertificate', 'AllowTrustServerCertificate', 'WorkstationId', 'FailoverPartner', 'MultipleActiveResultSets', 'MultiSubnetFailover', 'AppendConnectionString', 'AccessToken'
653+
$ignoredParameters = 'BatchSeparator', 'ClientName', 'ConnectTimeout', 'EncryptConnection', 'LockTimeout', 'MaxPoolSize', 'MinPoolSize', 'NetworkProtocol', 'PacketSize', 'PooledConnectionLifetime', 'SqlExecutionModes', 'TrustServerCertificate', 'AllowTrustServerCertificate', 'WorkstationId', 'FailoverPartner', 'MultipleActiveResultSets', 'MultiSubnetFailover', 'AppendConnectionString', 'AccessToken', 'AuthenticationType'
624654
if ($inputObjectType -eq 'Server') {
625655
if (Test-Bound -ParameterName $ignoredParameters) {
626656
Write-Message -Level Warning -Message "Additional parameters are passed in, but they will be ignored"
@@ -874,7 +904,16 @@ function Connect-DbaInstance {
874904
#[Microsoft.SqlServer.Management.Common.SqlConnectionInfo+AuthenticationMethod]::ActiveDirectoryPassword
875905
#[Microsoft.SqlServer.Management.Common.SqlConnectionInfo+AuthenticationMethod]::NotSpecified
876906
#[Microsoft.SqlServer.Management.Common.SqlConnectionInfo+AuthenticationMethod]::SqlPassword
877-
if ($authType -eq 'azure integrated') {
907+
if ($AuthenticationType) {
908+
Write-Message -Level Debug -Message "Authentication will be set to '$AuthenticationType' (from AuthenticationType parameter)"
909+
$sqlConnectionInfo.Authentication = [Microsoft.SqlServer.Management.Common.SqlConnectionInfo+AuthenticationMethod]::$AuthenticationType
910+
# ActiveDirectoryInteractive and ActiveDirectoryIntegrated require UseIntegratedSecurity=False
911+
# to prevent the default Integrated Security=True from conflicting with Entra ID auth
912+
if ($AuthenticationType -in 'ActiveDirectoryInteractive', 'ActiveDirectoryIntegrated', 'ActiveDirectoryDeviceCodeFlow', 'ActiveDirectoryManagedIdentity') {
913+
Write-Message -Level Debug -Message "UseIntegratedSecurity will be set to '$false' for $AuthenticationType"
914+
$sqlConnectionInfo.UseIntegratedSecurity = $false
915+
}
916+
} elseif ($authType -eq 'azure integrated') {
878917
# Azure AD integrated security
879918
# TODO: This is not tested / How can we test that?
880919
Write-Message -Level Debug -Message "Authentication will be set to 'ActiveDirectoryIntegrated'"

tests/Connect-DbaInstance.Tests.ps1

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ Describe $CommandName -Tag UnitTests {
4242
"AzureDomain",
4343
"Tenant",
4444
"AccessToken",
45+
"AuthenticationType",
4546
"DedicatedAdminConnection",
4647
"DisableException"
4748
)

0 commit comments

Comments
 (0)