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

4

12

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

Swarm answered 20/6, 2019 at 17:34 Comment(9)
you need to login firstBabi
@4c74356b41. Thanks. i edited my question with complete details and still the same issueSwarm
update to latest module version, check your permissions. you probably do not have rightsBabi
Close your powershell and open a new one, or use Clear-AzContext, not Clear-AzureProfile. Then use Connect-AzAccount -Tenant xxxxx -Subscription xxxxx.Symer
@4c74356b41. If i do not have rights then how I can create resources in cloud shell and Azure PortalSwarm
you might be using a different userBabi
@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 answerSwarm
@4c74356b41. same user in both environments and it was caching issue because i used Clear-AzContext as suggested by Joy and it workedSwarm
Related post - The subscription of xxx' doesn't exist in cloud 'AzureCloud'Minta
S
16

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.

Symer answered 21/6, 2019 at 9:47 Comment(3)
Could you also put this answer for my following question #56690236 this is the same issueSwarm
@Swarm I added it to that question.Symer
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.Spectroscopy
A
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.

Amygdala answered 31/10, 2019 at 21:35 Comment(0)
S
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.

Seljuk answered 3/1, 2020 at 14:24 Comment(0)
C
0

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

Connect-AzAccount -Tenant $tenantId -Subscription $subscriptionId

Cutler answered 31/7, 2023 at 14:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.