azurepowershell

Error When Connecting to Azure from PowerShell


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

Solution

  • 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.