Connect to Microsoft Azure with Powershell

In this article I’ll walk you through the steps needed to connecting to your Microsoft Azure environment, as well as giving you a glimpse of how you can manage it by starting up a IaaS virtual machine.

There is endless potential, to what you can manage and automate of Azure resources with PowerShell, but from here to there, first step is connecting it!

autoallthings.png

Installing Azure PowerShell Module

First off we are going to install the Azure PowerShell module

WebPlatformInstaller_2017-07-03_13-05-42
The installer takes a few minutes, once installed we will connect to your Azure subscription.

Connecting to your Azure Subscription

powershell_2017-07-03_13-09-12.png

When you open up your Microsoft Azure PowerShell, you’ll get this lovely bluebox and here we will type

Add-AzureAccount or Login-AzureRMAccount

Which prompts us to load which we accept with a “Y/N” (y to proceed)powershell_2017-07-03_13-11-023.png
This will bring up a login page asking you to input your account that has an active Azure subscription attached.

powershell_2017-07-03_13-12-58.png

Input credentials that can manage your Azure subscription in the login that was prompted by the Microsoft Azure PoweShell.

If successful, you should see an output showing you ID, type, Subscriptions and tenant information.

powershell_2017-07-03_13-19-428.png

You are now ready to start exploring, and you can add multiple subscriptions by using this method.

If you want to get your subscription info later, you can get them by typing:

Get-AzureSubscription

This will show you all Azure subscriptions that you have setup with PowerShell.

chrome_2017-07-03_13-24-51.png

 

You might as I do, have more than one Azure subscription that you’d like to manage through PowerShell and out of those there might be one that you are currently working on, and that you’d like to be the the “current” one as well as what is “default

To use this cmdlet, you will specify the SubscriptionName and pass the name of the subscription that was retrieved with Get-AzureSubscription.

powershell_2017-07-03_13-38-46.png

Now, rule of thumb when using PowerShell and getting used to a new module is running

Get-Command Module Azure

This allows you to easily explore what cmdlets is available to you in a module. Allowing you to see what is possible.

powershell_2017-07-03_13-45-58.png

Starting a Azure Infrastructure-as-a-Service Virtual Machine

Some of the most used commonly used Azure resources is its Infrastructure-as-a-service (IaaS) resources. So the example I will be providing here is how to start and stop (deallocate) a IaaS VM.

Now to accomplish this you will type in

Get-AzureVM / Get-AzureRMVM

Tip:
Get-AzureVM – To fetch classic instances

powershell_2017-07-03_13-53-48.pngGet-AzureRmVM – To fetch ARM instances.

Did you this error? If so, check out this troubleshooting article

Get-AzureVM : ForbiddenError: The server failed to authenticate the request. Verify that the certificate is valid and i
s associated with this subscription.
At line:1 char:1
+ Get-AzureVM
+ ~~~~~~~~~~~
+ CategoryInfo : CloseError: (:) [Get-AzureVM], ComputeCloudException
+ FullyQualifiedErrorId : Microsoft.WindowsAzure.Commands.ServiceManagement.IaaS.GetAzureVMCommand

If you got a normal output:

powershell_2017-07-03_15-20-32

We can then go after the specific VM we want to manage.

powershell_2017-07-03_15-34-43.png

So we start up the one we want to start by inputting the command below, modify for your resource group and Virtual Machine:

Start-AzureRmVM -esourceGroupNamexae-p-rg -Name ttt-xae-master

Start-AzureRmVM -ResourceGroupName ResourceGroupUlv -Name VirtualMachine01″

slack_2017-07-03_15-37-30.png

and when it’s done it gives us a status:powershell_2017-07-03_15-41-42.png

You’ve now started up your VM via Powershell for Azure.

To turn it off you can run

Stop-AzureRmVM -ResourceGroupName “xae-p-rg” -Name “t-Xae-Master

Stop-AzureRmVM -ResourceGroupName ResourceGroupUlv -Name VirtualMachine01

 

powershell_2017-07-03_15-49-51.png

and it’s now stopped and deallocated.

This is only scratching the surface of what is possible, list commands check out tutorials, browse technet for the variables and soon enough you’ll find yourself building and wow-tacoautomating the many mundane tasks giving you more time to do the super-duper awesome stuff!

 

As always, you can follow me on Twitter at @UlvBjornsson or follow me on here, if you have tips for articles you’d like to read or topics you want to hear more about, hit me up!

One thought on “Connect to Microsoft Azure with Powershell

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s