Tagged: Azure

Spinning up and spinning down Azure VMs

Introduction

This post discusses a relatively simple piece of code used to start or stop one or more Azure virtual machines within a defined time window.

Why am I doing this?

So, I have come up with this code to solve an issue with one of our customers wanting to de-allocate their VMs during non-business hours using PowerShell; the alternative would be to login to the Azure portal and selecting start…

AzureVMStart

…or shutdown everyday; not going to happen.

AzureVMStop

How does it work?

So this post references an earlier article I wrote around authenticating against Azure when scripting – see here.

The script reads in data from a csv file called AzureIaaSVMs.csv; the script looks for this file within the directory the script is being run from.

CSVFile

CheckForCsv

Once the data has been imported it works out what day it is and then checks whether that particular Azure VM should be running or shutdown and does the necessary.

AzureVMLogic

I have this script scheduled to run every hour using Task Scheduler. The command for example would be c:\…\powershell.exe -file c:\…\deallocateAzureVMs.ps1

Conclusion

So the this script is relatively simple at the minute but does a job. The script could be extended to…

…report failures using email notification

…include multiple subscriptions

…be more granular regarding start or stop time and improved time logic

The current version of the script can be downloaded from GitHub here – https://github.com/heathen1878/AzureVMControl

Advertisements

Copying VHDs between Windows Azure Storage Accounts

Script overview

Thought I’d write an interactive script which allows you to specify the source and destination Storage Account name, container and blob.

Requires you’re already connected to Windows Azure – see this article for more info – should really integrate connecting to Azure as part of this script.

Functions

The functions are there basically to check whether the storage accounts, containers and blobs exist.

CheckStorageAccount

Script body

The main body of the script uses a valid variable to determine whether it can move to the next step.

CheckStorageAccount1

Example of script output:

BlobCopyExample

The script is available here.

Connecting to Windows Azure with PowerShell

Installing the PowerShell module

First of all you need the Windows Azure PowerShell module which can be downloaded from here.

InstallPoSHModule

The module is installed via the Microsoft Web PI, simply follow the installer. If PowerShell is open when you install the Windows Azure module simply restart PowerShell i.e. close it and reopen.

Next check the module is available using Get-Module -ListAvailable

GetModule

This will be listed at the bottom if Azure is available.

AzureModule

Import the module using Import-Module Azure

ImportModule

Get-Command -Module Azure will give you a list of the available commands.

Connecting to Azure

[Updated 16/03/2016]:

If you’re looking to use an interactive PowerShell session with Azure then the Add-AzureAccount cmdlet is suitable; this will give you a 12 hour session token; after this time you need to re-authenticate.

AddAzureAccount

Enter your email address associated with the Azure subscription and follow the prompts. Once you have authenticated return to PowerShell and type Get-AzureSubscription to see your subscriptions.

If you have multiple subscription then you’ll need to determine which one if default and which is current.

AzureSubscriptions

Get-AzureSubscription -Default returns the default subscription.

defaultSub

Get-AzureSubscription -Current returns the currently selected subscription.

currentSub

If you have multiple subscriptions with the same name then the -ExtendedDetails parameter is useful to determine what is what.

extendedSub

Now comes the question…how do I authenticate against Azure when scripting? Well you need to use the PublishSettingsFile but If you’ve already added the subscription using the Add-AzureAccount cmdlet you’ll need to remove it first.

scriptingSub1

Use the Remove-AzureAccount and then use the Get-AzurePublishSettingsFile to get the certificate for the subscription to enable non-interactive authentication.

removeSub

[Updated 16/03/2016]

Using PublishSettingsFile

First of all run Get-AzurePublishSettingsFile

GetPublishSettingsFile

This will open an internet browser and you’ll be prompted to enter your credentials associated with your Azure subscription, once authenticated a publishsettings file will be downloaded.

PublishSettingsFile

Next import the publish settings file using Import-AzurePublishSettingsFile -PublishSettingsFile FileName…

PublishSettingsFile1

When you run Get-AzureSubscription you’ll notice your subscription will contain a certificate, you should now delete the downloaded .publishSettings file.

Subscription1

Run some commands against your subscription…Get-AzureVM…Get-AzureStorageAccount…