Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 367
3355307 Avatar asked Jun 20 '19 17:06

3355307


People also ask

Can we run PowerShell script on Azure cloud shell?

In this article This document details how to use the PowerShell in Cloud Shell in the Azure portal. A Bash in Azure Cloud Shell Quickstart is also available.

How do I connect-AzAccount to PowerShell?

Open the PowerShell console. Run Add-AzAccount or Connect-AzAccount or Login-AzAccount command. After the successful authentication validation, it will direct you to the PowerShell console. To see the profile file details, you can run the ls -lrt command to view the time and other details of JSON.

What does set AzContext do?

Description. The Set-AzContext cmdlet sets authentication information for cmdlets that you run in the current session. The context includes tenant, subscription, and environment information.


2 Answers

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.

like image 103
Joy Wang Avatar answered Sep 27 '22 20:09

Joy Wang


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.

like image 36
GregGalloway Avatar answered Sep 27 '22 20:09

GregGalloway