Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I connect to Azure using Powershell.

I am able to connect once or twice with below command, but in 3rd attempt i gives me error.

I have tried clear-azcontext, disconnect-azaccount. But still the error comes in 3rd or 4th attempt of running below command.

Command

Connect-AzAccount -Subscription ***-***-Tenant ***-*** -Environment ***-Credential $Credential

ERROR:

Connect-AzAccount : An error occurred while sending the request
At line:1 char:1
+ Connect-AzAccount -Subscription ***-*** -Tenant ***-***...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : CloseError: (:) [Connect-AzAccount], HttpRequestException
    + FullyQualifiedErrorId : Microsoft.Azure.Commands.Profile.ConnectAzureRmAccountCommand
                Try to run your powershell as admin, update the module with Update-Module -Name Az, then login again.
– Joy Wang
                Jul 16, 2019 at 2:41

powershell cmdlets, sometimes it couldn't give a proper error message. So to resolve the issue , you can try to re-install or update the concerned module.

Alternatively you can try to use $DebugPreference= "Continue" before executing the Connect-AzAccount

It determines how PowerShell responds to debugging messages generated by a script, cmdlet or provider, or by a Write-Debug command at the command line.

Some cmdlets display debugging messages, which are typically very technical messages designed for programmers and technical support professionals. By default, debugging messages are not displayed, but you can display debugging messages by changing the value of $DebugPreference.

You can read more about $DebugPreference here. Sample output for the same:

PS> $ErrorView                         # Verify the value.
NormalView
PS> Get-ChildItem nofile.txt           # Find a non-existent file.
Get-ChildItem : Cannot find path 'C:\nofile.txt' because it does not exist.
At line:1 char:14
+ Get-ChildItem  <<<< nofile.txt
Status Code:
NotFound
Headers:
Pragma                        : no-cache
x-ms-failure-cause            : gateway
x-ms-ratelimit-remaining-subscription-writes: 1199
x-ms-request-id               : 65972d3e-495e-41fd-84fd-9b068c62df22
x-ms-correlation-request-id   : 65972d3e-xx-41fd-84fd-xx
x-ms-routing-request-id       : xxx:xxx:65972d3e-xx-41fd-84fd-xx
Strict-Transport-Security     : max-age=31536000; includeSubDomains
X-Content-Type-Options        : nosniff
Cache-Control                 : no-cache
Date                          : Tue, 04 Sep 2018 02:45:49 GMT
Body:
  "error": {
    "code": "ResourceGroupNotFound",
    "message": "Resource group 'test' could not be found."

Hope it helps.

Try running the below first from Powershell

[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls12

Then run connect-az command again to see if it works.

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.