Set-AzContext works in Azure Cloud Shell but doesn't in Azure PowerShell

前端 未结 3 864
难免孤独
难免孤独 2021-02-15 10:39

When i execute following command

Clear-AzureProfile
Connect-AzAccount -TenantID xxxxxxxxxxxxxxxxxxx
Set-AzContext -SubscriptionId xxxxxxxxxxxxxxxxxxx

相关标签:
3条回答
  • 2021-02-15 10:44

    If you are cycling through subscriptions in the same tenant and don't want to have to sign in with Connect-AzAccount multiple times, the following worked for me to switch between subscriptions:

    Remove-AzContext -InputObject (Get-AzContext) -Force | Out-Null;
    $sub = Set-AzContext -Subscription $_.SubscriptionName;
    

    Before adding the Remove-AzContext statement I was seeing that Set-AzContext was not actually switching the context to another subscription for some reason.

    0 讨论(0)
  • 2021-02-15 10:45

    Close your powershell and open a new one, or use Clear-AzContext, not Clear-AzureProfile. Then use Connect-AzAccount -Tenant xxxxx -Subscription xxxxx, it should work.

    0 讨论(0)
  • 2021-02-15 11:08

    I used to have this problem too, and my solution was different that one provided on top of this. Apparently, in our Azure Cloud Shell, we have several contexts available, so, we don't have to set the context (using Set-AzContext), but to switch to one or other context, using Select-AzContext)

    I can list the contexts using

    Get-AzContext -ListAvailable
    

    Then choose one using the

    Select-AzContext -Name ...
    

    For-example, in scripts, I use this one-line command to switch to the subscription having ID $SubscriptionID :

    Select-AzContext -name ((Get-AzContext -ListAvailable).Name -match $SubscriptionId)[0]
    

    Not elegant, but efficient

    I don't know why we are in such situation. Maybe because we are administrating using invited accounts from another tenant.

    Hope this help someone in same situation than us.

    0 讨论(0)
提交回复
热议问题