skip to Main Content

I am trying to automate azure login in a powershell script and for that, I am using the below command as suggested in the Microsoft doc here:

az login --tenant <tenant> --use-device-code

But the script is pausing here with the below warning expecting us to open browser and enter a code.

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

How do I automate the login in my powershell script?

Edit: The intention of this post is to understand the working of –use-device-code and get it working as expected.

2

Answers


  1. This is how az login --use-device-code works:

    enter image description here

    Enter the code in the given url:

    enter image description here

    Now in powershell you will get this type of output:

    enter image description here

    Login or Signup to reply.
  2. Another way to do this is to run your script on an Azure VM with a managed identity. In that instance, no credentials are needed. All you need to do is run:

    az login --identity
    

    https://learn.microsoft.com/en-us/azure/active-directory/managed-identities-azure-resources/how-to-use-vm-sign-in

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search