Salesforce, Python, SQL, & other ways to put your data where you need it

Need event music? 🎸

Live and recorded jazz, pop, and meditative music for your virtual conference / Zoom wedding / yoga class / private party with quality sound and a smooth technical experience

Logging your command line into Azure

13 Mar 2023 🔖 beginner azure
💬 EN

Table of Contents

To rent resources from Microsoft Azure’s public cloud, we need to be logged into it.


Prerequisites

  1. Have an account in Azure. You can get one:
  2. You must have the Azure command line interface tool (“Azure CLI”) or PowerShell installed onto your local computer.
    • If you’re on Windows, there’s a good chance PowerShell came with your computer.
  3. If using PowerShell, you must also have the Az PowerShell module installed onto your local computer.
    • If you don’t have admin rights to your computer, make sure you use the “-Scope CurrentUser” parameter as part of your installation process so that you don’t get prompted for an admin password to the computer.

Log PowerShell into Azure

  1. At the PowerShell command prompt, execute the following command:
     Connect-AzAccount -UseDeviceAuthentication
    
  2. Look at PowerShell’s response to you. It should say something like the following message, only with a unique code in place of “XXXXXX.” Copy the code where “XXXXXX” would be onto your computer’s clipboard:

    “WARNING: To sign in, use a web browser to open the page https://microsoft.com/devicelogin and enter the code XXXXXX to authenticate.”

  3. Open a brand new incognito/private web browser tab in a browser you don’t already have incognito/private sessions open in and use it to visit https://microsoft.com/devicelogin.
  4. At the “Enter code” prompt in your incognito web browser, paste the “XXXXXX“-style code and click “Next.”
  5. Enter the username and password to the Azure account you’d like to log into and click “Sign in.”.
    • Tip: Don’t worry that the username prompt warns you that you’re signing into PowerShell “on another device.” In this case, you know you’re doing using PowerShell and logging into Azure on the same computer.
      • (That said, this same technique can be used to use a totally different computer for PowerShell vs. the browser you’re logging in with, should you ever find yourself needing to do such a thing.)
    • Tip: If you just spun up a playground from A Cloud Guru, the browser tab where you asked to do so should show you a username and password to use to log into your playground.
  6. Click “Continue” when asked, in your incognito/private web browser tab:

    “Are you trying to sign in to Microsoft Azure PowerShell? Only continue if you downloaded the app from a store or website that you trust.”

  7. You can leave this web browser open if you’re doing an exercise where you’ll also be playing around in the Azure portal. Alternatively, if you’re a neat freak, feel free to close your incognito/private web browser session when you see the following message:

    “Microsoft Azure PowerShell: You have signed in to the Microsoft Azure PowerShell application on your device. You may now close this window.”

  8. Back in PowerShell, you should see output indicating an “Account,” a “SubscriptionName,” and a “TenantId” that feel appropriate, given the nature of the Azure account you chose to sign into.
  9. Double-check that your PowerShell session is really logged into the Azure account you intended to be logged into by executing the following command and validating that the “Name” property of its output matches “SubscriptionName” from above and that the “TenantId” value also matches:
     Get-AzContext
    
    • If they don’t match, substituting the first “TenantId” value from when you ran Connect-AzAccount -UseDeviceAuthentication (not the second one from Get-AzContext), execute the following command:
        Set-AzContext -Tenant "your_tenant_id_goes_here"
      
    • Now things should match the original Connect-AzAccount -UseDeviceAuthentication output when you rerun this command:
        Get-AzContext
      

Log the Azure CLI into Azure

  1. At your computer’s command prompt, execute the following command:
     az login --use-device-code
    
  2. Look at your computer’s command prompt’s response to you. It should say something like the following message, only with a unique code in place of “XXXXXX.” Copy the code where “XXXXXX” would be onto your computer’s clipboard:

    “To sign in, use a web browser to open the page https://microsoft.com/devicelogin and enter the code XXXXXX to authenticate.”

  3. Open a brand new incognito/private web browser tab in a browser you don’t already have incognito/private sessions open in and use it to visit https://microsoft.com/devicelogin.
  4. At the “Enter code” prompt in your incognito web browser, paste the “XXXXXX“-style code and click “Next.”
  5. Enter the username and password to the Azure account you’d like to log into and click “Sign in.”
    • Tip: Don’t worry that the username prompt warns you that you’re signing into Microsoft Azure Cross-platform Command Line Interface “on another device.” In this case, you know you’re doing using the Azure CLI tool and logging into Azure on the same computer.
      • (That said, this same technique can be used to use a totally different computer for the CLI tool vs. the browser you’re logging in with, should you ever find yourself needing to do such a thing.)
    • Tip: If you just spun up a playground from A Cloud Guru, the browser tab where you asked to do so should show you a username and password to use to log into your playground.
  6. Click “Continue” when asked, in your incognito/private web browser tab:

    “Are you trying to sign in to Microsoft Azure CLI? Only continue if you downloaded the app from a store or website that you trust.”

  7. You can leave this web browser open if you’re doing an exercise where you’ll also be playing around in the Azure portal. Alternatively, if you’re a neat freak, feel free to close your incognito/private web browser session when you see the following message:

    “Microsoft Azure Cross-platform Command Line Interface: You have signed in to the Microsoft Azure Cross-platform Command Line Interface application on your device. You may now close this window.”

  8. Back in your computer’s command prompt, you should see JSON-formatted output indicating, among other things, a “name,” a “tenantId,” and a “user.name” property that feel appropriate, given the nature of the Azure account you chose to sign into.
  9. Double-check that your command-line session is really logged into the Azure account you intended to be logged into by executing the following command and validating that the output matches the output from az login --use-device-code
     az account show
    
    • If they don’t match, substituting the “name” value from when you ran az login --use-device-code (not the second one from az account show), execute the following command:
        az account set --name "your subscription name here"
      
    • Now things should match the original az login --use-device-code output when you rerun this command:
        az account show
      

Posts in this series

--- ---