11

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

3355307
  • 1,508
  • 4
  • 17
  • 42
  • 1
    you need to login first – 4c74356b41 Jun 20 '19 at 17:38
  • @4c74356b41. Thanks. i edited my question with complete details and still the same issue – 3355307 Jun 20 '19 at 17:44
  • update to latest module version, check your permissions. you probably do not have rights – 4c74356b41 Jun 20 '19 at 18:10
  • 1
    Close your powershell and open a new one, or use `Clear-AzContext`, not `Clear-AzureProfile`. Then use `Connect-AzAccount -Tenant xxxxx -Subscription xxxxx`. – Joy Wang Jun 21 '19 at 02:32
  • @4c74356b41. If i do not have rights then how I can create resources in cloud shell and Azure Portal – 3355307 Jun 21 '19 at 09:36
  • you might be using a different user – 4c74356b41 Jun 21 '19 at 09:37
  • 2
    @JoyWang. Thanks. your suggestion worked, i used Clear-AzContext instead of Clear-AzureProfile at the top of script and then Connect-AzAccount and every resource in the script created successfully. Please move your comments into answer – 3355307 Jun 21 '19 at 09:44
  • @4c74356b41. same user in both environments and it was caching issue because i used Clear-AzContext as suggested by Joy and it worked – 3355307 Jun 21 '19 at 09:44
  • Related post - [The subscription of xxx' doesn't exist in cloud 'AzureCloud'](https://stackoverflow.com/q/51911225/465053) – RBT Nov 18 '21 at 16:01

4 Answers4

14

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.

Joy Wang
  • 39,905
  • 3
  • 30
  • 54
  • Could you also put this answer for my following question https://stackoverflow.com/questions/56689736/azure-powershell-script-does-not-work-when-change-to-a-different-azure-subscript?as this is the same issue – 3355307 Jun 21 '19 at 09:52
  • 1
    @3355307 I added it to that question. – Joy Wang Jun 21 '19 at 10:30
  • I think you should also specify how to get TenantId and SubscriptionID for those trying to login first time. In my case I had to switch to `AZ CLI` and use: `az account show -otable` and `az account list -otable`. – Rajesh Swarnkar Jan 12 '23 at 10:36
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.

GregGalloway
  • 11,355
  • 3
  • 16
  • 47
1

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.

L. Boggio
  • 13
  • 3
0

I also had this issue and just needed to login via powershell with

Connect-AzAccount -Tenant $tenantId -Subscription $subscriptionId

SpeedOfSpin
  • 1,611
  • 20
  • 21