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

我是研究僧i 提交于 2019-12-22 17:08:29

问题


When i execute following command

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

in Azure PowerShell i get this error.

Set-AzContext : Please provide a valid tenant or a valid subscription.
At line:6 char:1
+ Set-AzContext -SubscriptionId xxxxxxxxxxxxxxxxxxx

and if i run the same command in Azure Cloud Shell it works

Name        Account         SubscriptionName    Environment    TenantId         
xxxx        xxxxxxx         xxxx                 xxxx             xxxx

I switched from free-trial to pay-as-you-go subscription and using credentials for pay-as-you-go in both environment but it doesn't work. can anyone help


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/56691220/set-azcontext-works-in-azure-cloud-shell-but-doesnt-in-azure-powershell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!