Sheridan Wendt
  • Home
  • Technology
  • Business
  • Music
  • Adventures
  • Resume
  • Contact

Technology

Create Azure VMs with PowerShell

9/9/2018

Comments

 
Do you often create virtual machines in Azure? Have you ever wished you could automate it? Then this article is for you! This PowerShell script will create a new VM in Azure and prompt the user to set the new VM's local administrator credentials. Depending on your processes and environment, you may want to plug in some default credentials or create random credentials for each new VM and save them in a protected location or password manager. ​
Picture
There are a lot of variables involved with creating a new virtual machine (VM) in Azure. From configuring the Resource Group all the way down to the new VM's network interface card (NIC), this article will cover each variable involved in creating a new VM in Azure. 

Click below to view the full .ps1 file on github
Azure-NewVM.ps1
Best Practices for Windows VMs in Azure
This article can't cover the best configuration for every environment, so be sure that the variables are changed before using this script. For more information see Microsoft's article covering best practices for Windows VMs in Azure. This article assumes that best practices are being followed in your environment already. 

The variables we will configure in this article include:
  • Resource groups
  • Location
  • VM sizes
  • Storage accounts
  • Storage types
  • Networking

Global Variables
In this instance, the resource group and location for resources this VM will use are global variables. If you already have a resource group in mind, be sure to change the $ResourceGroupName variable accordingly.
## Global
$ResourceGroupName = "RG2"
$Location = "SouthCentralUS"
Compute Variables
Next we will define the name of the VM, the computer name as seen by Windows, the size of the VM, and the name of the boot disk that Windows will run on. The disk will automatically be named based on the name of the VM. 
## Compute
$VMName = "SQL1"
$ComputerName = "SQL1"
$VMSize = "Standard_B1ms"
$OSDiskName = "$ComputerName" + "_OSDisk"
Storage Variables
Storage accounts must be unique across Azure and contain all lowercase characters. This script accounts for that by generating a random string and then converting that string to lowercase using the ".ToLower" function. This script creates a new storage account for the VM. If a storage account exists, replace the $StorageAccount variable with "Get-AzureRmStorageAccount | Select storageaccountname".
​## Storage
$Random = -join ((65..90) + (97..122) | Get-Random -Count 4 | % {[char]$_})
###The random variable is configured here because your Storage Account Name must be unique across Azure 
$StorageAccountName = "Storage" + $VMName + "$Random"
$StorageAccountName = $StorageAccountName.ToLower()
$StorageType = "Standard_GRS"
### type options currently include Premium_LRS, Standard_GRS, Standard_LRS, Standard_RAGRS, Standard_ZRS
$StorageAccount = New-AzureRmStorageAccount -ResourceGroupName $ResourceGroupName -Name $StorageAccountName -Type $StorageType -Location $Location
 Network Variables
The network variables are named based on the name of the VM so keep that in mind. For more information see Plan Virtual Networks in Azure.
​## Network
$InterfaceName = "NIC" + $VMName
$Subnet1Name = "Subnet1"
$VNetName = "VNet" + $VMName
$VNetAddressPrefix = "10.1.0.0/16"
$VNetSubnetAddressPrefix = "10.1.1.0/24"
### Create Public New IP
$pIP = New-AzureRmPublicIpAddress -Name $InterfaceName -ResourceGroupName $ResourceGroupName -Location $Location -AllocationMethod Dynamic
### Create a Subnet Configuration
$SubnetConfig = New-AzureRmVirtualNetworkSubnetConfig -Name $Subnet1Name -AddressPrefix $VNetSubnetAddressPrefix
### Create a Virtual Network
$VNet = New-AzureRmVirtualNetwork -Name $VNetName -ResourceGroupName $ResourceGroupName -Location $Location -AddressPrefix $VNetAddressPrefix -Subnet $SubnetConfig
### Create a NIC
$Interface = New-AzureRmNetworkInterface -Name $InterfaceName -ResourceGroupName $ResourceGroupName -Location $Location -SubnetId $VNet.Subnets[0].Id -PublicIpAddressId $pIP.Id
Create OS Credentials
Next we'll need to define a local administrator account to be used to log into the VM. This can be pre-configured, but this script prompts the user running the script to create it on the spot so that they can be specific to each VM. The credentials are assigned to the $Credential variable. 
$Credential = Get-Credential
Add Variables to Virtual Machine Configuration
This portion of the script assigns the values and variables to the $VirtualMachine variable to be used in the creation of the VM.
## Add Variables to Virtual Machine Configuration
$VirtualMachine = New-AzureRmVMConfig -VMName $VMName -VMSize $VMSize
$VirtualMachine = Set-AzureRmVMOperatingSystem -VM $VirtualMachine -Windows -ComputerName $ComputerName - Credential $Credential -ProvisionVMAgent -EnableAutoUpdate
$VirtualMachine = Set-AzureRmVMSourceImage -VM $VirtualMachine -PublisherName MicrosoftWindowsServer -Offer WindowsServer -Skus 2016-Datacenter -Version "latest"
$VirtualMachine = Add-AzureRmVMNetworkInterface -VM $VirtualMachine -Id $Interface.Id
$OSDiskUri = $StorageAccount.PrimaryEndpoints.Blob.ToString() + "vhds/" + $OSDiskName + ".vhd"
$VirtualMachine = Set-AzureRmVMOSDisk -VM $VirtualMachine -Name $OSDiskName -VhdUri $OSDiskUri -CreateOption FromImage
Create the VM
Now that the variables are all stored in the $VirtualMachine variable, it can be used for a rather short one-line command to actually create the virtual machine.
## Create the VM
New-AzureRmVM -ResourceGroupName $ResourceGroupName -Location $Location -VM $VirtualMachine
Conclusion
Whew! We're done. Now we have a VM in Azure that has been created using PowerShell. Feel free to use all or portions of this script to help your organization automate your workloads in Azure. 
Picture
Comments

    Repositories

    PowerShell
    SQL

    Author

    Sheridan's interests are in technology, business, music, and adventures

    View my profile on LinkedIn

    RSS Feed

    Categories

    All
    Alerts
    Azure
    Business Intelligence
    Data Visualization
    Notifications
    Photo Frame
    PowerShell
    Raspberry Pi
    Scripting
    SMS
    SQL
    Technology
    Virtualization
    VMWare

    Business

    Archives

    June 2019
    May 2019
    September 2018
    May 2018
    April 2018
    March 2018
    February 2018
    December 2017
    September 2017
    July 2003

Powered by Create your own unique website with customizable templates.
  • Home
  • Technology
  • Business
  • Music
  • Adventures
  • Resume
  • Contact