Is there a PowerShell statement that can Update a server with User Managed Identies?

Brian D H ZHANG 60 Reputation points
2025-01-16T12:03:59.64+00:00

Hi

Is there a PowerShell statement that can Update a server with User Managed Identies?

like this :

az sql server update -g myResourceGroup -n myServer -i \ --user-assigned-identity-id /subscriptions/xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/myResourceGroup/providers/Microsoft.ManagedIdentity/userAssignedIdentities/testumi \ --identity-type UserAssigned --pid /subscriptions/xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/myResourceGroup/providers/Microsoft.ManagedIdentity/userAssignedIdentities/testumi

https://learn.microsoft.com/en-us/cli/azure/sql/server?view=azure-cli-latest#az-sql-server-update,

the az cli cann't work in my env.I need to use powershell .

i use the powershell as following ,but it doesn't work.

Set-AzSqlServer -ResourceGroupName $(resourceGroupNameBCP) -ServerName $(serverNamebcp) -IdentityType UserAssigned -UserAssignedIdentityId '/subscriptions/xxxxxxxxxxxxx/resourceGroups/xxxxxxxxxx/providers/Microsoft.ManagedIdentity/userAssignedIdentities/xxxxxxxxx' -PrimaryUserAssignedIdentityId  '/subscriptions/xxxxxxxxxxxxx/resourceGroups/xxxxxxxxxx/providers/Microsoft.ManagedIdentity/userAssignedIdentities/xxxxxxxxx'

thanks for your help.

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,880 questions
0 comments No comments
{count} votes

Accepted answer
  1. Vijayalaxmi Kattimani 1,885 Reputation points Microsoft External Staff
    2025-01-21T10:49:28.07+00:00

    Hi @Brian D H ZHANG,

    I'm glad that you were able to resolve your issue and thank you for posting your solution so that others experiencing the same thing can easily reference this! Since the Microsoft Q&A community has a policy that "The question author cannot accept their own answer. They can only accept answers by others ", I'll repost your solution in case you'd like to "Accept " the answer.

    Issue: Is there a PowerShell statement that can Update a server with User Managed Identies? the az cli cann't work in my env. I need to use powershell.

    Solution:

    As you stated that, you have resolved the issue using azure CLI.

    The solution is as follows.

    1. The following command clears the UMI.
         az sql server update -g resourceGroupName -n serverName -i --user-assigned-identity-id /subscriptions/xxxxxxxxxxxxx/resourceGroups/xxxxxxxxxx/providers/Microsoft.ManagedIdentity/userAssignedIdentities/xxxxxxxxx
      

    2.set sql server UMI

    az sql server update -g resourceGroupName -n serverName -i --user-assigned-identity-id /subscriptions/xxxxxxxxxxxxx/resourceGroups/xxxxxxxxxx/providers/Microsoft.ManagedIdentity/userAssignedIdentities/xxxxxxxxx 
     --identity-type UserAssigned --pid /subscriptions/xxxxxxxxxxxxx/resourceGroups/xxxxxxxxxx/providers/Microsoft.ManagedIdentity/userAssignedIdentities/xxxxxxxxx
    

    3.set database UMI

    az sql db update -g resourceGroupName -s serverName 
    -n databaseName  -i --user-assigned-identity-id 
    /subscriptions/xxxxxxxxxxxxx/resourceGroups/xxxxxxxxxx/providers/Microsoft.ManagedIdentity/userAssignedIdentities/xxxxxxxxx
    

    If I missed anything please let me know and I'd be happy to add it to answer, or feel free to comment below with any additional information.

    Please remember to "Accept Answer" so that others in the community facing similar issues can easily find the solution.

    Thank you,

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Marcin Policht 40,310 Reputation points MVP
    2025-01-16T12:50:24.35+00:00

    Set-AzSqlServer cmdlet should work

    # Set variables
    $resourceGroupName = "myResourceGroup"
    $serverName = "myServer"
    $userAssignedIdentityId = "/subscriptions/xxxxxxxxxxxxx/resourceGroups/xxxxxxxxxx/providers/Microsoft.ManagedIdentity/userAssignedIdentities/xxxxxxxxx"
    
    # Update SQL server with User Assigned Identity
    Set-AzSqlServer `
        -ResourceGroupName $resourceGroupName `
        -ServerName $serverName `
        -AssignIdentity `
        -IdentityType UserAssigned `
        -UserAssignedIdentityId @{Key = $userAssignedIdentityId}
    

    -AssignIdentity: Specifies that an identity should be assigned to the server. -IdentityType: Specifies the type of identity (UserAssigned in this case). -UserAssignedIdentityId: This needs to be passed as a hashtable where the key is the identity ID.

    • Ensure the PowerShell Az module is up to date by running Update-Module -Name Az.
    • Verify that the user or service principal executing the command has the necessary permissions (e.g., Contributor role on the SQL server and Managed Identity resources).
    • Confirm the User Assigned Identity exists and the provided ID is accurate.
    • Ensure PowerShell is using the appropriate subscription by running Set-AzContext.

    If the above response helps answer your question, remember to "Accept Answer" so that others in the community facing similar issues can easily find the solution. Your contribution is highly appreciated.

    hth

    Marcin


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.