Powershell – how to recover the Azure powershell context, right after creating a new VM, in a new powershell

azurepowershell

I just followed the commands in the Azure create a Windows VM training. After successfully creating the new VM the powershell timed out. When I tried to continue the training I got this message:

Get-AzVMImagePublisher : No subscription found in the context. Please ensure that the credentials you provided are
authorized to access an Azure subscription, then run Connect-AzAccount to login.
At line:1 char:1
+ Get-AzVMImagePublisher -Location "WestUS"
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : CloseError: (:) [Get-AzVMImagePublisher], ApplicationException
+ FullyQualifiedErrorId : Microsoft.Azure.Commands.Compute.GetAzureVMImagePublisherCommand

and it keeps happening.

Best Answer

Before creating the VM, there should be a subscription in the tenant that you log in with your account. In PowerShell, you can login first like this:

Connect-AzAccount

It shows like this after you login with your Azure account:

enter image description here

Then you can get the subscription through the PowerShell command:

Get-AzSubscription

It would show the subscriptions you have. Like this:

enter image description here

And select the subscription through the PowerShell command:

Select-AzSubscription -Subscription subscription_Id

Then you can create the VM as you do before.

Related Topic