{"id":9090,"date":"2024-02-09T15:08:53","date_gmt":"2024-02-09T15:08:53","guid":{"rendered":"https:\/\/cheapwindowsvps.com\/blog\/how-to-use-powershell-for-managing-system-and-user-assigned-managed-identities-in-azure\/"},"modified":"2025-01-20T10:27:15","modified_gmt":"2025-01-20T10:27:15","slug":"how-to-use-powershell-for-managing-system-and-user-assigned-managed-identities-in-azure","status":"publish","type":"post","link":"https:\/\/cheapwindowsvps.com\/blog\/how-to-use-powershell-for-managing-system-and-user-assigned-managed-identities-in-azure\/","title":{"rendered":"How to Use PowerShell for Managing System and User-Assigned Managed Identities in Azure"},"content":{"rendered":"<p><div>Managed identities provide secure authentication for resources accessing other resources in Azure without requiring sensitive information such as secrets, credentials, and certificates to be handled. Microsoft Entra ID manages these identities, enabling applications to obtain tokens for authentication. In this post, I will provide an example that illustrates how to use system and user-assigned managed identities with PowerShell.<\/div>\n<\/p>\n<p><p>Two types of managed identities are available: system-assigned managed identities and user-assigned managed identities.<\/p>\n<\/p>\n<p><h2>System-assigned managed identities<\/h2>\n<\/p>\n<p><p>These identities are specific to an Azure resource, such as a virtual machine or a web app. Therefore, their lifecycle is closely tied to that resource. A system-assigned managed identity is automatically deleted if the resource is removed.<\/p>\n<\/p>\n<p><p>These identities are only used by the corresponding Azure resource to request tokens from Microsoft Entra ID, meaning they cannot be used for more than one resource.<\/p>\n<\/p>\n<p><h2>User-assigned managed identities<\/h2>\n<\/p>\n<p><p>These standalone Azure resources can be assigned to multiple Azure resources, providing greater flexibility and scalability than system-assigned managed identities.<\/p>\n<\/p>\n<p><p>For example, a web app or a virtual machine can have multiple user-assigned managed identities, which multiple Azure resources can share.<\/p>\n<\/p>\n<p><p>Initially, one has to establish a managed identity within Azure. This identity is then connected with the relevant Azure resource. Subsequently, the identity has to be allowed to access the required resource. We&#8217;ll illustrate this with a particular example in the subsequent discussion.<\/p>\n<h2>Instance of Managed identities<\/h2>\n<p>Let\u2019s hypothesize we possess the following setup:<\/p>\n<ul>\n<li>A VM termed <em>jbox01<\/em> that holds both a system-assigned managed identity and a user-assigned managed identity<\/li>\n<li>A storage account termed <em>rbacstracc<\/em> possessing a blob named <em>data.txt<\/em><\/li>\n<li>A Key Vault called <em>certkv01<\/em> containing a secret referred to as <em>an-important-secret<\/em>.<\/li>\n<\/ul>\n<p><p>So, we need to:<\/p>\n<\/p>\n<ul>\n<li>Assign the <em>Key Vault Secret User<\/em> role to the system-assigned managed identity of the VM on the Key Vault Secret<\/li>\n<li>Assign the <em>Reader and Data Access<\/em> role to the user-assigned managed identity of the VM on the storage account<\/li>\n<li>Try to access the <em>Key Vault<\/em>, read the secret using each identity separately, and observe the outcome<\/li>\n<li>Try to access the Storage Account, read the blob content using each identity separately, and observe the outcome.<\/li>\n<\/ul>\n<p><p>Let\u2019s start by enabling a system-assigned identity on the VM using the following command.<\/p>\n<\/p>\n<p><pre>Get-AzVM -ResourceGroupName jumpbox -Name jbox01 | Update-AzVM -IdentityType SystemAssigned<\/p><p><\/pre>\n<\/p>\n<p><div><a href=\"https:\/\/4sysops.com\/wp-content\/uploads\/2024\/02\/Adding-a-system-assigned-managed-identity-to-a-VM.png\" rel=\"nofollow noopener\" target=\"_blank\">Adding a system-assigned managed identity to a VM<\/a><\/p>\n<p><p>Adding a system-assigned managed identity to a VM<\/p>\n<\/div>\n<p><div><a href=\"https:\/\/4sysops.com\/wp-content\/uploads\/2024\/02\/System-assigned-managed-identity-can-be-seen-on-the-VM\u2019s-identity-section.png\" rel=\"nofollow noopener\" target=\"_blank\">System-assigned managed identity can be seen on the VM\u2019s identity section<\/a><\/p>\n<p><p>System-assigned managed identity can be seen on the VM\u2019s identity section<\/p>\n<\/div>\n<p><p>Once the system-assigned managed identity on the VM has been created, we can see it along with its <em>objectID<\/em>, which we will use to assign roles to this identity in the following steps.<\/p>\n<\/p>\n<p><p>Now, create a new user-assigned managed identity and assign it to the VM. So, the VM will have both system-assigned and user-assigned managed identities at the same time. It is important to note that we will set the <em>IdentityType<\/em> to <em>SystemAssignedUserAssigned<\/em> to ensure we keep the system-assigned managed identity and attach the user-assigned managed identity to the VM.<\/p>\n<\/p>\n<p><p>If you forget this and set the parameter to <em>UserAssigned<\/em>, the system-assigned managed identity will be disabled. You will end up having only a user-assigned managed identity on the VM.<\/p>\n<\/p>\n<p><pre><\/p><p>New-AzUserAssignedIdentity -Name UAMI-for-VM -ResourceGroupName jumpbox -Location UKSouth<\/p><p>$UAMI = Get-AzUserAssignedIdentity -Name UAMI-for-VM -ResourceGroupName jumpbox<\/p><p>Get-AzVM -ResourceGroupName jumpbox -Name jbox01 | Update-AzVM -IdentityType SystemAssignedUserAssigned -IdentityId $UAMI.Id<\/p><p><\/pre>\n<\/p>\n<p><a href=\"https:\/\/4sysops.com\/wp-content\/uploads\/2024\/02\/Creating-a-new-user-assigned-managed-identity.png\" rel=\"nofollow noopener\" target=\"_blank\"><\/p>\n<p>Creating a new user-assigned managed identity<\/p>\n<p><\/a><\/p>\n<p><a href=\"https:\/\/4sysops.com\/wp-content\/uploads\/2024\/02\/User-assigned-managed-Identity-has-been-added-to-the-VM.png\" rel=\"nofollow noopener\" target=\"_blank\"><\/p>\n<p>User-assigned managed Identity has been added to the VM<\/p>\n<p><\/a><\/p>\n<p><p>We can now go to the target resources and assign these managed identities with desired RBAC roles.<\/p>\n<\/p>\n<p><p>First, let\u2019s give the <em>Key Vault Secrets User<\/em> role to the system-assigned managed identity on the Secret <em>an-important-secret<\/em>. For this, we will use the following command. The <em>ObjectID<\/em> can be obtained directly from the VM.<\/p>\n<\/p>\n<p><pre>New-AzRoleAssignment -ObjectId \"35942614-984a-4067-8552-b806e2211124\" -Scope \"\/subscriptions\/xxxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx\/resourceGroups\/certs\/providers\/Microsoft.KeyVault\/vaults\/certkv01\/secrets\/an-important-secret\" -RoleDefinitionName \"Key Vault Secrets User\"<\/p><p><\/pre>\n<\/p>\n<p><a href=\"https:\/\/4sysops.com\/wp-content\/uploads\/2024\/02\/System-assigned-managed-identity-has-been-granted-the-Key-Vault-Secrets-User-role-on-the-Key-Vault.png\" rel=\"nofollow noopener\" target=\"_blank\">System-assigned managed identity has been granted the Key Vault Secrets User role on the Key Vault<\/a><\/p>\n<p><p>Next, assign the user-created managed identity with the <em>Storage Blob Data Reader<\/em> role on the Blob <em>data.txt<\/em>.<\/p>\n<\/p>\n<p><pre><\/p><p>$UAMIObjectID = (Get-AzUserAssignedIdentity -Name uami-for-vm -ResourceGroupName jumpbox).PrincipalId<\/p><p>New-AzRoleAssignment -ObjectId \"$UAMIObjectID\" -Scope \"\/subscriptions\/xxxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx\/resourceGroups\/RBAC\/providers\/Microsoft.Storage\/storageAccounts\/rbacstracc\/\" -RoleDefinitionName \"Reader and Data Access\"<\/p><p><\/pre>\n<\/p>\n<p><a href=\"https:\/\/4sysops.com\/wp-content\/uploads\/2024\/02\/User-assigned-managed-identity-has-been-granted-Reader-and-Data-Access-role-on-the-Storage-Account.png\" rel=\"nofollow noopener\" target=\"_blank\"><\/a><\/p>\n<p><p>User-assigned managed identity has been given Reader and Data Access role on the Storage Account<\/p>\n<\/p>\n<p><p>Firstly, let&#8217;s examine how we can connect to Azure from within the VM using the system-assigned managed identity and see what we can access. If you have both a system-assigned managed identity and a user-assigned managed identity enabled on a resource and attempt to connect to Azure using the Connect-AzAccount -Identity command, the system-assigned managed identity will be the default choice.<\/p>\n<\/p>\n<p><p>If you possess solely a user-allotted managed identity, it is effectually chosen. In scenarios where you have multiple user-assigned managed identities, designate the identity you wish to employ when establishing connection with Azure.<\/p>\n<\/p>\n<p><p>Reflecting upon the example provided below, we observed that connection to Azure was achieved using a system-distributed managed identity, accessing the key vault secret. This could be attributed to the fact that the identity had been given the privilege of accessing the Key Vault Secret.<\/p>\n<\/p>\n<p><a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/4sysops.com\/wp-content\/uploads\/2024\/02\/Connecting-to-Azure-from-within-the-VM-using-a-system-assigned-managed-identity.png\">Connecting to Azure from within the VM using a system-assigned managed identity<\/a><\/p>\n<p><p>In a subsequent endeavor, attempt establishing connection making use of the user-assigned managed identity to discern the resources which are accessible. It is important to note on this occasion, the client ID of the user-assigned managed identity was employed for Azure connection.<\/p>\n<\/p>\n<p><p>With the user-assigned managed identity, we can get the blob content.<\/p>\n<\/p>\n<p><a href=\"https:\/\/4sysops.com\/wp-content\/uploads\/2024\/02\/Connecting-to-Azure-from-within-the-VM-using-the-user-assigned-managed-identity.png\" rel=\"nofollow noopener\" target=\"_blank\">Connecting to Azure from within the VM using the user-assigned managed identity<\/a><\/p>\n<p><p>However, we cannot get the Key Vault Secret with the same user identity as the user-assigned managed identity does not have access to the Key Vault.<\/p>\n<\/p>\n<p><a href=\"https:\/\/4sysops.com\/wp-content\/uploads\/2024\/02\/User-assigned-managed-identity-does-not-have-access-to-the-Key-Vault-Secret.png\" rel=\"nofollow noopener\" target=\"_blank\">User-assigned managed identity does not have access to the Key Vault Secret<\/a><\/p>\n<p><p>Similarly, suppose we return to the system-assigned managed identity and try to access the storage account blob. In that case, it will not be permitted as the system-assigned managed identity does not have permissions on the storage account.<\/p>\n<\/p>\n<p><h2>Subscribe to 4sysops newsletter!<\/h2>\n<\/p>\n<p><a href=\"https:\/\/4sysops.com\/wp-content\/uploads\/2024\/02\/System-assigned-managed-identity-does-not-have-access-to-the-Storage-Account.png\" rel=\"nofollow noopener\" target=\"_blank\">System-assigned managed identity does not have access to the Storage Account<\/a><\/p>\n<p><h2>Conclusion<\/h2><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Managed identities provide secure authentication for resources accessing other resources in Azure without requiring sensitive information such as secrets, credentials, and certificates to be handled. Microsoft Entra ID manages these identities, enabling applications to obtain tokens for authentication. In this post, I will provide an example that illustrates how to use system and user-assigned managed [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[92,107,126,163,117,114],"tags":[],"class_list":["post-9090","post","type-post","status-publish","format-standard","hentry","category-articles","category-azure","category-cloud-computing","category-password","category-powershell","category-security"],"_links":{"self":[{"href":"https:\/\/cheapwindowsvps.com\/blog\/wp-json\/wp\/v2\/posts\/9090","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/cheapwindowsvps.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cheapwindowsvps.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cheapwindowsvps.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/cheapwindowsvps.com\/blog\/wp-json\/wp\/v2\/comments?post=9090"}],"version-history":[{"count":2,"href":"https:\/\/cheapwindowsvps.com\/blog\/wp-json\/wp\/v2\/posts\/9090\/revisions"}],"predecessor-version":[{"id":10383,"href":"https:\/\/cheapwindowsvps.com\/blog\/wp-json\/wp\/v2\/posts\/9090\/revisions\/10383"}],"wp:attachment":[{"href":"https:\/\/cheapwindowsvps.com\/blog\/wp-json\/wp\/v2\/media?parent=9090"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cheapwindowsvps.com\/blog\/wp-json\/wp\/v2\/categories?post=9090"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cheapwindowsvps.com\/blog\/wp-json\/wp\/v2\/tags?post=9090"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}