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:
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
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" # Add random number/characters here New-AzureRmStorageAccount -Name $storageAccountName -ResourceGroupName $resourceGroup -Type Standard_LRS -Location $location
We need a Virtual Network where our Virtual Machine will live.
$vnetName = "test-net" $subnet = New-AzureRmVirtualNetworkSubnetConfig -Name frontendSubnet -AddressPrefix 10.0.1.0/24 $vnet = New-AzureRmVirtualNetwork -Name $vnetName -ResourceGroupName $resourceGroup -Location $location -AddressPrefix 10.0.0.0/16 -Subnet $subnet
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 Dynamic $nic = New-AzureRmNetworkInterface -Name $nicName -ResourceGroupName $resourceGroup -Location $location -SubnetId $vnet.Subnets.Id -PublicIpAddressId $pip.Id
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"
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" # Either this for the prompt or #$username = "YOURUSERNAME" #$password = ConvertTo-SecureString "YOUR_PASSWORD" -AsPlainText -Force #$cred = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $username, $password
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"
$vm = Add-AzureRmVMNetworkInterface -VM $vm -Id $nic.Id
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() + "vhds/" + $diskName + ".vhd" $vm=Set-AzureRmVMOSDisk -VM $vm -Name $diskName -VhdUri $osDiskUri -CreateOption fromImage
Everything should now be configured, it’s time to create the actual Virtual Machine.
New-AzureRmVM -ResourceGroupName $resourceGroup -Location $location -VM $vm
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!
I'm a .NET Software Engineer that is currently working at SafeToNet. Opinions are my own.
Nant (1) Load testing (1) Visual studio (1) Azure (24) Aws (1) Asp.net (1) Tfs (1) Puppetlabs reports (2) Devops (2) Iis (1) Powershell (6) Arm templates (2) Arm template (1) Azure (5) Powershell (1) Dsc (1) Arm (1) Jekyll (1) Vsts (1)