how to add user assigned managed identify in azure sql database using powershell?

Brian D H ZHANG 40 Reputation points
2024-07-12T08:21:12.2733333+00:00

how to add user assigned managed identify in azure sql database using powershell?

I tried two methods as follow and they didn't work.

1.add parameter userassignedidentityid when create az sql database .


# Create Azure SQL Database  
$databaseName = "sql-db-ase-ermes-dev-02"  
$edition = "GeneralPurpose"  
$computeModel = "Serverless"  
$collation = "SQL_Latin1_General_CP1_CI_AS"  
$vcore =  2 
$ComputeGeneration = 'Gen5'
$MinimumCapacity = 2
$BackupStorageRedundancy ='Local'

$UserAssignedIdentityId = (Get-AzUserAssignedIdentity).Id

$databaseParams = @{  
    ResourceGroupName = $resourceGroupName
    ServerName = $serverName  
    DatabaseName = $databaseName  
    Edition = $edition  
    ComputeModel = $computeModel  
    Collation = $collation 
    VCore = $vcore
    ComputeGeneration = $ComputeGeneration
    MinimumCapacity = $MinimumCapacity
    BackupStorageRedundancy = $BackupStorageRedundancy 
    UserAssignedIdentityId = $UserAssignedIdentityId
}  
  
New-AzSqlDatabase @databaseParams  

2.add parameter userassignedidentityid after creating az sql database.

$UserAssignedIdentityId = (Get-AzUserAssignedIdentity).Id
Set-AzSqlDatabase -ResourceGroupName $resourceGroupName -ServerName $serverName -DatabaseName $databaseName   -UserAssignedIdentityId  $UserAssignedIdentityId

User's image

Azure SQL Database
PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,579 questions
{count} votes

Accepted answer
  1. ShaktiSingh-MSFT 15,341 Reputation points
    2024-07-12T09:08:36.7+00:00

    Hi Brian D H ZHANG •,

    Welcome to Microsoft Q&A forum.

    As I understand, you want to provision Azure SQL Database logical server using user assigned managed identity.

    Thanks for sharing details and screenshots.

    Could you please try to use commands for Logical Server and not on Database as shown below:

    $server = @{

    ResourceGroupName = "<ResourceGroupName>"

    Location = "<Location>"

    ServerName = "<ServerName>"

    ServerVersion = "12.0"

    AssignIdentity = $true

    IdentityType = "UserAssigned"

    UserAssignedIdentityId = "/subscriptions/<subscriptionId>/resourceGroups/<ResourceGroupName>/providers/Microsoft.ManagedIdentity/userAssignedIdentities/<managedIdentity>"

    PrimaryUserAssignedIdentityId = "/subscriptions/<subscriptionId>/resourceGroups/<ResourceGroupName>/providers/Microsoft.ManagedIdentity/userAssignedIdentities/<primaryIdentity>"

    ExternalAdminName = "<AzureADAccount>"

    EnableActiveDirectoryOnlyAuthentication = $true }

    New-AzSqlServer @server

    Note

    The above example provisions a server with only a user-assigned managed identity. You could set the -IdentityType to be "UserAssigned,SystemAssigned" if you wanted both types of managed identities to be created with the server.

    To check the server status after creation, see the following command:

    Get-AzSqlServer -ResourceGroupName "<ResourceGroupName>" -ServerName "<ServerName>" -ExpandActiveDirectoryAdministrator

    Prerequisites

    Refer https://learn.microsoft.com/en-us/azure/azure-sql/database/authentication-azure-ad-user-assigned-managed-identity-create-server?view=azuresql&tabs=azure-powershell more details.

    Hope this helps.

    Let us know if you have further queries.

    Thanks

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.