skip to Main Content

I have a very strange problem which I cant wrap my head around.
I have a host with PowerShell 5.1 installed.
When I run the PowerShell "regulary" with the user logged in to Windows I can use the Connect-AzAccount command:
Command working

When I open the Powershell location and then select "run as different user" and login to the pwoershell with a different user the command does not work. Everytime I run Connect-AzAccount the command start but fails to open the login window returning the following error:
Command fails
Then the PowerShell session freezes and i need to close it with TaskManager.

I ran $PSVersionTable in both Powershell sessions and it is the same in both sessions:
PSVersion

I’m at a complete loss whats the issue here.
Did any of you guys experience something like this before?

2

Answers


  1. Chosen as BEST ANSWER

    Found a solution. Downgraded from Az.Accounts v. 3.0.0 to 2.19.0. With the older version everything works as expected.


  2. This is a very transient issue and can be occurred due to the following reasons.

    Firstly, check your execution policies which might block the script execution in the PowerShell. Set your execution policy to remote signed or set it as unrestricted and try it.

    Use below to remove the context cache and try logging again into a different account.

     Clear-AzContext -Scope CurrentUser
     set-Azcontext -Subscription "xxxxx" #Account B subscription
    

    enter image description here

    Check if there any other profiles existed in your current PowerShell environment as shown in the below way.

    enter image description here

    Update Az PowerShell modules using Update-Module -Name Az if required and make sure that it is set to the latest releases.

    There are also some other ways for logging into the Azure with connect-AzAccount.

    When I tried to login after clearing the cache context, it worked as expected.

    enter image description here

    1. You can use Connect-AzAccount -UseDeviceAuthentication as it doesn’t require to provide any credential.

    enter image description here

    1. Use below command to login into the subscription directly with the help of -Subscription parameter.
    Connect-AzAccount -Tenant "xxxx" -Subscription "xxxxx"
    
    1. Alternatively, create an application ID and a client Secret (Service Principal) under Microsoft Entra ID and use it along with the connection command as shown below.
    Connect-AzAccount -ServicePrincipal -TenantId xxxx -Credential $Credential
    

    Also refer this SO by @Luuk for relevant issue.

    Update:

    enter image description here

    I have faced the same error in my current PS environment now and you can also try the below workaround which worked for me.

    az account get-access-token 
    $conv = $token | convertfrom-json
    $logtoken=$conv.accessToken
    Connect-AzAccount -AccessToken $logtoken
    

    enter image description here

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