Using PowerShell with Azure

Seems like something reasonable anyone using Azure would want to do. After all, one of the draws of “the cloud” is automation. So I thought I would throw together a quick video and blog post on how to do it.

If you don’t want to read the small book I wrote below or need deeper demos and explanations check out my video Installing the Azure PowerShell cmdlets.

The long story short is it really only takes two cmdlets:

Install-Module AzureRM

And

Install-Module Azure

But unfortunately, it isn’t always quite that simple.

Why two modules?

The AzureRM module is the latest greatest and installs the Azure Resource Manager cmdlets. That is all of the fancy new stuff. The easiest way to equate it, though not completely accurate, is it is the same as the new portal.azure.com environment. Anything, new and fun going forward is being done with Azure Resource Manager. 1,398 cmdlets to play with.

The Azure module is the old service manager cmdlets. Anything that you do in the Azure portal that is called Classic or old is what this module represents. While if you are new to Azure you should be able to avoid most of this stuff some features haven’t made their way to the RM module yet so good to go ahead and install these. 708 more cmdlets to enjoy.

If you want a better explanation of what the module are and do, then you can read this Microsoft Azure documentation. It does a good job of explaining what it is doing but it doesn’t do a very good job of explaining how to install things. So that is where this blog post picks up.

Step 1: Permissions

To install these modules, you will need to run your PowerShell session as an Administrator. Also, you will have to be running an execution policy of RemoteSigned or less. For Windows Server 2012 R2 this is the default. If you are installing on your Windows 10 machine though the default is restricted so you will need to change it.

To find your current policy you can use:

Get-ExecutionPolicy

If it is set to Restricted, then you will need to run the following cmdlet to change it with following:

Set-ExecutionPolicy RemoteSigned

Unfortunately, the Azure cmdlets will only run with RemoteSigned or greater permissions so you will have to leave this changed going forward.

Step 2: Installing the Modules

Now that you have permissions set you can simply run the following:

Install-Module AzureRM

If this is the first time installing a remote module, then you will need to type Y to accept the NuGet module.

Then you will need to type Y again to say you trust the repository.

Once that finishes you will need to install the old modules (which are updated) with:

Install-Module Azure -AllowClobber

The reason for -AllowClobber is the two seems to bicker. So I just let the Azure module win and move on.

You will have to type Y again to say you trust the repository.

After a couple of minutes, you will have both modules installed and you will be ready to go.

Step 3: Logging in

Seems like a great next step.

To login to AzureRM use the following:

Login-AzureRMAccount

Now you will need to enter your Username and Password in the popup authentication box. After a few flashes, you are in.

To login to Azure Service Manager use the following:

Add-AzureAccount

The same process of logging into the authentication box that popups up and you are in business.

Step 4: The scripts that made me figure all of this out.

Running resources in Azure cost money and I don’t think anything quite runs up your bill like Compute. Unfortunately for me the main thing I do with Azure is use Infrastructure as a Service (IaaS) and incur all of those Compute charges. Since my money tree still isn’t growing in the back yard I try to minimize this spend. To do that I have created scripts for starting and stopping my Virtual Machines (VMs).

For Azure Resource Manger VMs I use:

get-azurermvm -ResourceGroupName proper2013 | foreach {start-azurermvm -Name $_.name -resourcegroupname proper2013

What that script does is starts all of the VMs in the “proper2013” resource group. If you are unfamiliar with resources groups check out Working with Resource Groups in Microsoft Azure. One of my more underappreciated videos.

When the day is over and you want to stop all of those VMs just run the following:

get-azurermvm -ResourceGroupName proper2013 | foreach {stop-azurermvm -Name $_.name -resourcegroupname proper2013 -confirm:$false -force}

That script will deallocate your VMs compute resources (release its IP Address for example) which will save you compute charges. You will still incur storage charges, though. If you don’t want to deallocate the compute resources then you can add the -StayProvisioned option.

If you are still rocking Old School aka Classic VMs then you will use this to start those VMs:

Start-AzureVM -ServiceName VideoDemoClassicVMs -Name "*"

ServiceName is the Classic version of Resource Groups.

And because you aren’t printing money in your basement to stop them you should run:

Stop-Azurevm -ServiceName VideoDemoClassicVMs -Name "*" -Force

Step 5: Stop reading and go play

There are about a million (2,106) more things you can do now that you are connected to your Azure account with PowerShell so go forth and learn. Other resources to help you out if you aren’t terribly comfortable with Azure and or PowerShell yet.

Until next time,

Shane – @ShanesCows

Bold Zebras - Microsoft Cloud Consulting

Bold Zebras – Microsoft Cloud Consulting



Leave a Reply