So you want to create a Virtual Machine on Azure, this can be done in the portal. But really we want this scriptable, so that we can run it at a later date if needed and also so we can leverage this work and use it to create other virtual machines at a later date.
First, fire up a PowerShell console and login to your subscription:
Login-AzureRmAccount
1. Create a Resource Group
We need to create a Resource Group, this is a logical place to group all the elements that we’re going to create.
$resourceGroup = "test-infra"
$location = "North Europe"
New-AzureRmResourceGroup -Name $resourceGroup -Location $location
2. Create storage account
Now we need a storage account where our virtual machine data will be stored
The storage account name has to be unique globally, so make sure you rename here
$storageAccountName = "teststorage1x2" <span class="c">
New-AzureRmStorageAccount -Name $storageAccountName -ResourceGroupName $resourceGroup -Type <span class="nx">Standard_LRS -Location $location
3. Create Virtual Network
We need a Virtual Network where our Virtual Machine will live.
$vnetName = "test-net"
$subnet = New-AzureRmVirtualNetworkSubnetConfig -Name <span class="nx">frontendSubnet -AddressPrefix <span class="nx">10.0.1.0/24
$vnet = New-AzureRmVirtualNetwork -Name $vnetName -ResourceGroupName $resourceGroup -Location $location -AddressPrefix <span class="nx">10.0.0.0/16 -Subnet $subnet
4. Setup IP Address and Network Interface
Our Virtual Machine needs a Network Interface and IP Address associated with it. We’re going to create a Dynamic IP Address for now, but you can have a static IP Address too.
$nicName ="testvm-nic"
$pip = New-AzureRmPublicIpAddress -Name $nicName -ResourceGroupName $resourceGroup -Location $location -AllocationMethod <span class="nx">Dynamic
$nic = New-AzureRmNetworkInterface -Name $nicName -ResourceGroupName $resourceGroup -Location $location -SubnetId $vnet.Subnets<span class="p">[<span class="mi">0<span class="p">].Id -PublicIpAddressId $pip.Id
5. Start Virtual Machine config setup
Now we can begin with the actual configuation of the Virtual Machine. We’ll give it a name of testvm1, and have a size of Basic A1.
$vmName = "testvm1"
$vm = New-AzureRmVMConfig -VMName $vmName -VMSize "Basic_A1"
6. Set Virtual Machine Credentials
We create a $cred object to be used later, you can either have the script prompt you for your credentials, or have it hard coded in your script. I would suggest prompt you, as you don’t want to store your password in plain text anywhere.
$cred=Get-Credential -Message "Admin credentials" <span class="c">
<span class="c">
<span class="c">
<span class="c">
7. Select Operating System for Virtual Machine
We want a Windows Virtual Machine, and pass in the $cred object that we just setup. We can choose which Windows image we want from the Azure gallery, here I have selected the Windows 2012 R2 Datacenter image.
$vm = Set-AzureRmVMOperatingSystem -VM $vm -Windows -ComputerName $vmName -Credential $cred -ProvisionVMAgent -EnableAutoUpdate
$vm = Set-AzureRmVMSourceImage -VM $vm -PublisherName "MicrosoftWindowsServer" -Offer "WindowsServer" -Skus "2012-R2-Datacenter" -Version "latest"
8. Add Network Interface to Virtual Machine
$vm = Add-AzureRmVMNetworkInterface -VM $vm -Id $nic.Id
9. Create Disk for Virtual Machine
Our Virtual Machine needs somewhere to save itself obviously. So we create a new VHD in the Storage Account we setup earlier.
$diskName="os-disk"
$storageAcc=Get-AzureRmStorageAccount -ResourceGroupName $resourceGroup -Name $storageAccountName
$osDiskUri= $storageAcc.PrimaryEndpoints.Blob.ToString<span class="p">() + "vhds/" + $diskName + ".vhd"
$vm=Set-AzureRmVMOSDisk -VM $vm -Name $diskName -VhdUri $osDiskUri -CreateOption <span class="nx">fromImage
10. Create the Virtual Machine
Everything should now be configured, it’s time to create the actual Virtual Machine.
New-AzureRmVM -ResourceGroupName $resourceGroup -Location $location -VM $vm
Wrap up
So there we go, we now have a nice new Virtual Machine setup. And we can create a new one really simply by changing a couple of parameters.
You can find the full script example on GitHub here.
Any Azure topics you would like to see? Please reach out!