We start by creating a machine through the portal

Creating machine in Azure Portal

In this example, we use Basic_A2 as the size, but any size should work.
The operating system is Windows Server 2012 R2 as this is a Windows tutorial.
All other options are the default options as set by Azure.

Azure Machine creating

Once the machine is created, we can remote to it and make the necessary changes. On this machine – for example – we are going to install IIS.

Installing IIS in Win Server 2012R2

The next step is to run sysprep on this machine. The recommended settings are “Enter the Out-of-the-Box-Experience”, “Generalize”, and “Shutdown”.

sysprep in Win Server 2012 R2

Back in the portal the machine should now shut down. The next step is to deallocate it. Once the machine has been stopped, we need to run the following Azure PowerShell script to create an image:

#you will be prompted to log in
#this makes sure the right subscription is being used
Select-AzureRMSubscription -SubscriptionId <Your Subscription Id>
#this makes sure that the machine is fully deallocated
Stop-AzureRMVM -Name <VM Name> -ResourceGroupName <Resource Group Name> -Force
#this makes sure Azure ‘knows’ that the machine is ready to become an image
Set-AzureRMVM -Name <VM Name> -ResourceGroupName <Resource Group Name> -Generalized 
Save-AzureRmVMImage -Name <VM Name> -DestinationContainerName <Folder Name in the storage account, where the image should be placed> -VHDNamePrefix <prefix for newly created vhds based on this image> -ResourceGroupName <Resource Group Name> -Path <for example: C:\Templates\json.json>

Download Azure PowerShell: https://azure.microsoft.com/en-us/downloads/


Once the image has been saved, the easiest way of redeploying it is via the JSON template that was created.

To do this, we first need to create a new network interface in a Vnet. The network interface in ARM holds the network config for the machine along with links to load balancers, IP addresses, and DNS servers. The easiest way to create the required networking resource(s) is through the portal. (Creating the resource(s) throuhg PowerShell is also an option and useful for larger deployments)

We can pull the resource Id out in PowerShell and then proceed to run the template deployment:

7 8