{"id":705,"date":"2020-09-16T14:46:34","date_gmt":"2020-09-16T13:46:34","guid":{"rendered":"https:\/\/wp.bizoir.dk\/?p=705"},"modified":"2020-10-11T18:06:26","modified_gmt":"2020-10-11T17:06:26","slug":"authenticating-to-azure-with-azcli-og-az-powershell-module","status":"publish","type":"post","link":"https:\/\/wp.bizoir.dk\/?p=705","title":{"rendered":"Authenticating to Azure with AzCli og the Az Powershell module"},"content":{"rendered":"<p>When you only have access to one Azure directory, AzCLI and the Az Powershell module can figure out the Authentication itself, but if you have access to multiple directories on one user, you have to be more specific &#8211; especially if one of the directories uses MFA.<br \/>\nThe typical &#8220;<a href=\"https:\/\/docs.microsoft.com\/en-us\/cli\/azure\/install-azure-cli-windows?view=azure-cli-latest\">Get started<\/a>&#8221; introductions does not cover this.<\/p>\n<p>I have not tried it with multiple directories all requiring (different) MFA&#8230;<\/p>\n<p><!--more--><\/p>\n<h2>AzCLI:<\/h2>\n<p>Make sure to use &#8220;az login&#8221; with the tenant parameter for the MFA directory. The tenant guid can be found in several places &#8211; among others in the &#8220;Show diagnostics&#8221; in the <a href=\"https:\/\/portal.azure.com\">Azure portal<\/a><\/p>\n<pre>az login --tenant deadbeef-0000-0000-0000-000000000000<\/pre>\n<p>The AzCLI tool will remember your login-session for some time.<\/p>\n<p>The accessible subscriptions can then be listed with:<\/p>\n<pre>az account list --output table<\/pre>\n<p>and the default subscription can be set with:<\/p>\n<pre>az account set --subscription deadbeef-1111-2222-3333-000000000000<\/pre>\n<h2>Powershell Az module:<\/h2>\n<pre>Import-Module Az\r\n\r\nConnect-AzAccount -Tenant deadbeef-0000-0000-0000-000000000000 -Subscription deadbeef-1111-2222-3333-000000000000<\/pre>\n<p>When you are connected, Powershell will by default remember your login-session, and you can change subscriptions with<\/p>\n<pre>Set-AzContext -SubscriptionId \"deadbeef-1111-2222-3333-000000000000\"<\/pre>\n<p>As mentioned <a href=\"https:\/\/vgaltes.com\/post\/deploying-azure-functions-an-introduction\/\" target=\"_blank\" rel=\"nofollow noopener ugc noreferrer\">here<\/a>, it is in general better to login using a service principal (like for a CI agent or unattended runs), but you might not always have the possibility to get a SP.<\/p>\n<p>You can see how to create a new service principal here<br \/>\n<a href=\"https:\/\/docs.microsoft.com\/en-us\/cli\/azure\/create-an-azure-service-principal-azure-cli\" rel=\"nofollow ugc\">https:\/\/docs.microsoft.com\/en-us\/cli\/azure\/create-an-azure-service-principal-azure-cli?view=azure-cli-latest<\/a><br \/>\nand how to login using it here<br \/>\n<a href=\"https:\/\/docs.microsoft.com\/en-us\/cli\/azure\/authenticate-azure-cli?view=azure-cli-latest#sign-in-with-a-service-principal\" rel=\"nofollow ugc\">https:\/\/docs.microsoft.com\/en-us\/cli\/azure\/authenticate-azure-cli?view=azure-cli-latest#sign-in-with-a-service-principal<\/a><br \/>\nassign new password to Service Principal<br \/>\n<a href=\"https:\/\/docs.microsoft.com\/en-us\/cli\/azure\/create-an-azure-service-principal-azure-cli?view=azure-cli-latest#reset-credentials\" rel=\"nofollow ugc\">https:\/\/docs.microsoft.com\/en-us\/cli\/azure\/create-an-azure-service-principal-azure-cli<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>When you only have access to one Azure directory, AzCLI and the Az Powershell module can figure out the Authentication itself, but if you have access to multiple directories on one user, you have to be more specific &#8211; especially if one of the directories uses MFA. The typical &#8220;Get started&#8221; introductions does not cover &hellip; <a href=\"https:\/\/wp.bizoir.dk\/?p=705\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Authenticating to Azure with AzCli og the Az Powershell module&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[30],"class_list":["post-705","post","type-post","status-publish","format-standard","hentry","category-guides","tag-azure"],"_links":{"self":[{"href":"https:\/\/wp.bizoir.dk\/index.php?rest_route=\/wp\/v2\/posts\/705","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wp.bizoir.dk\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wp.bizoir.dk\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wp.bizoir.dk\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/wp.bizoir.dk\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=705"}],"version-history":[{"count":12,"href":"https:\/\/wp.bizoir.dk\/index.php?rest_route=\/wp\/v2\/posts\/705\/revisions"}],"predecessor-version":[{"id":731,"href":"https:\/\/wp.bizoir.dk\/index.php?rest_route=\/wp\/v2\/posts\/705\/revisions\/731"}],"wp:attachment":[{"href":"https:\/\/wp.bizoir.dk\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=705"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wp.bizoir.dk\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=705"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wp.bizoir.dk\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=705"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}