How to create AzureProfile.json file using AzModule command in PowerShell?

3.2k views Asked by At

I want to create the AzureProfile.json file using the Az Module command in PowerShell, so that I can import the json file anytime I want to login to Azure to start\stop my VM's.

Below is the command\script I am using to create it but it is giving me error as shown in the screenshot.

enter image description here

I tried using the below code as well but it gives same error.

Connect-AzAccount -UseDeviceAuthentication

Save-AzProfile -path "$PSScriptRoot\AzureProfile.json"

WARNING: Unable to acquire token for tenant 'organizations' Connect-AzAccount : DeviceCodeCredential authentication failed: Retry failed after 4 tries.

2

There are 2 answers

5
Satya V On

You have tried both :

  • Connect-AzAccount -UseDeviceAuthentication
  • Login-AzAccount

Both Interactive and non-interactive mode of login.

The error is occurring while trying you communicate to azure. I am suspecting it could be an issue at a machine level or network level (proxy/firewall)

enter image description here

The above issue usually occurs (reproducible at my end) when there are connection issues with the Microsoft Services - blocked by proxy /firewall/GPO etc...

For a quick test you could run the below PowerShell command :

Invoke-WebRequest -Uri "https://login.microsoftonline.com/organizations/oauth2/v2.0/devicecode" -Body "client_id=1950a258-227b-4e31-a9cf-717495945fc2&scope=https%3A%2F%2Fmanagement.core.windows.net%2F%2F.default+offline_access+profile+openid" -Method Post

Sample Success Response :

enter image description here

Why the above command ?

The above end point https://login.microsoftonline.com/organizations/oauth2/v2.0/devicecode is hit when you use Connect-AzAccount -UseDeviceAuthentication by powershell.

enter image description here

If you encounter a timeout/Name unresolvable issue - the issue is with the network/machine config.

8
Harshita Singh On

From the error message, it looks like the authentication of your account is failing in the Browser pop-up that comes after Login-AzAccount.

I ran the first set of commands and it ran successfully.

Powershell:

enter image description here

Created File:

enter image description here

Note that my PowerShell version is 5.7.0.18831.