Click here to Skip to main content
15,886,729 members
Articles / Hosted Services / Azure
Technical Blog

Articles: How to create an Azure Virtual Machine with PowerShell

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
9 Feb 2020CPOL2 min read 1.5K   2  
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..
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:

PowerShell
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.

PowerShell
$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

PowerShell
$storageAccountName = "teststorage1x2" <span class="c"># Add random number/characters here
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.

PowerShell
$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.

PowerShell
$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.

PowerShell
$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.

PowerShell
$cred=Get-Credential -Message "Admin credentials"  <span class="c"># Either this for the prompt or
<span class="c">#$username = "YOURUSERNAME"
<span class="c">#$password = ConvertTo-SecureString "YOUR_PASSWORD" -AsPlainText -Force
<span class="c">#$cred = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $username, $password

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.

PowerShell
$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

PowerShell
$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.

PowerShell
$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.

PowerShell
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!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
United Kingdom United Kingdom
I have been working in software development for over 16 years, during that time I have worn many hats.

I have worked as a Software Engineer, Architect, Agile Coach and Trainer. I’ve created teams, I’ve lead teams, but my main goal is to help teams build great software and enjoy the process.

I help a whole range of businesses – from startups with just an idea who want to build a team to take that idea into reality and FTSE 100 businesses who need to optimise existing teams – I train, mentor and coach them to success.

If you happen to know of anybody who could benefit from results like this, then please go to my contact page and get in touch.

Owen Davies

Comments and Discussions

 
-- There are no messages in this forum --