Deploy a Basic VM in Azure Resource Manager

This is the second part of a two part video on configuring the prerequisite and deploying a virtual machine in Microsoft Azure Resource Manager.  This video is intended for people new to Azure or Azure RM.  The script used in this video can be found below.  

Click here for the first video on setting up prerequisites.

Copy the code and paste the code below into Microsoft PowerShell ISE.  

#List Variables
# Ls variable:*
#Remove Variables
# Remove-Variable * -ErrorAction SilentlyContinue

#Server name and IP
$vmName="Server Name"
$staticIP="192.168.200.4"

#Location for the VM and all components
$locName="Central US"

#Resource Group Name
#To list Resource Groups
# Get-AzureRmResourceGroup | format-table resourcegroupname,location
$rgName="Resource Group name"

#List Virtual Networks
# Get-AzureRmVirtualNetwork -ResourceGroupName $rgName | Sort Name | Select Name
#Set Virtual Network
$vnetName="vNet Name"

#Set Subnet and index number
# The subnet index is the number of the subnet in the display of this command, 
# numbering them consecutively from left to right and starting at 0.
# Get-AzureRmVirtualNetwork -Name $vnetName -ResourceGroupName $rgName | Select Subnets
# Set the subnet to use by index number
$subnetIndex=0

#Set the VNet
$vnet=Get-AzureRmVirtualNetwork -Name $vnetName -ResourceGroupName $rgName

#Specify a NIC Name and assign a public IP and static, private IP (default is server name with _NIC1) 
$nicName=$vmName +"_NIC1"
$pipName=$vmName +"_PIP1"
$pip = New-AzureRmPublicIpAddress -Name $pipName -ResourceGroupName $rgName -Location $locName -AllocationMethod Dynamic
$nic = New-AzureRmNetworkInterface -Name $nicName -ResourceGroupName $rgName -Location $locName -SubnetId $vnet.Subnets[$subnetIndex].Id -PublicIpAddressId $pip.Id -PrivateIpAddress $staticIP

#Specify Storage Account. This is the account where the server disks will reside
#List Storage Account if needed with the line below.
# Get-AzureRmStorageAccount | Format-table StorageAccountName,ResourceGroupName,Location,AccountType
$saName="Storage Account Name"

#Get the size of VM's available
#Use the command below to get the sizes avaialble for virtual machines
# Get-AzureRmVMSize -Location $locName | FT Name,MemoryInMB,NumberOfCores,OSDiskSizeInMB
$vmSize="Standard_A0"
$vm=New-AzureRmVMConfig -VMName $vmName -VMSize $vmSize

# Get-AzureRmVMImagePublisher -Location $locName | where-object {$_.PublisherName -like "*windows*"} | ft PublisherName,Location
$pubName="MicrosoftWindowsServer"
# Get-AzureRmVMImageOffer -Location $locName -PublisherName $pubName | ft Offer,PublisherName,Location
$offerName="WindowsServer"
# Get-AzureRmVMImageSku -Location $locName -PublisherName $pubName -Offer $offerName | ft Skus,Offer,PublisherName,Location
$skuName="2012-R2-Datacenter"

#The commands below will prompt for a local username and password. do not use the Administrator account.
$cred=Get-Credential -Message "Type the name and password of the local administrator account."
$vm=Set-AzureRmVMOperatingSystem -VM $vm -Windows -ComputerName $vmName -Credential $cred -ProvisionVMAgent -EnableAutoUpdate
$vm=Set-AzureRmVMSourceImage -VM $vm -PublisherName $pubName -Offer $offerName -Skus $skuName -Version "latest"
$vm=Add-AzureRmVMNetworkInterface -VM $vm -Id $nic.Id

#This command sets the OS disk
$diskName=$vmName -join "_OS"
$storageAcc=Get-AzureRmStorageAccount -ResourceGroupName $rgName -Name $saName
$osDiskUri=$storageAcc.PrimaryEndpoints.Blob.ToString() + "vhds/" + $diskName + ".vhd"
$vm=Set-AzureRmVMOSDisk -VM $vm -Name $diskName -VhdUri $osDiskUri -CreateOption fromImage
New-AzureRmVM -ResourceGroupName $rgName -Location $locName -VM $vm

This information is as-is with no warranty, expressed or implied.  Test it before you trust it!

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.