Azure – Deploy ARM based VM with PowerShell

Quick steps to deploy your first Azure RM based VM utilizing PowerShell.


# Step 1 - Login
Add-AzureAccount
Login-AzureRMAccount

# Step 2 - create a resource group
$locName = "centralus"
$rgName = "PowerShellVM"

# Step 3 - Create a storage account

$stName = "hscjhstore01"
Get-AzureRMStorageAccountNameAvailability $stName
$storageAcc = New-AzureRMStorageAccount -ResourceGroupName $rgName -Name $stName -SkuName "Standard_LRS" -Kind "Storage" -Location $locName

$ Step 4 - Configure networking

$subnetName = "VMSubnet"
$ipName = "VMIPAddress"
$vnetName = "VMNetwork"
$nicName = "VMNic"

$singleSubnet = New-AzureRMVirtualNetworkSubnetConfig -Name $subnetName -AddressPrefix 10.0.0.0/24
$pip = New-AzureRMPublicIpAddress -Name $ipName -ResourceGroupName $rgName -Location $locName -AllocationMethod Dynamic
$vnet = New-AzureRMVirtualNetwork -Name $vnetName -ResourceGroupName $rgName -Location $locName -AddressPrefix 10.0.0.0/16 -Subnet $singleSubnet
$nic = New-AzureRMNetworkInterface -Name $nicName -ResourceGroupName $rgName -Location $locName -SubnetID $vnet.Subnets[0].Id -PublicIPAddressId $pip.Id

# Step 5 - Configure the VM itself

$vmName = "vmFS02"
$compName = "FileServer02"

$cred = Get-Credential -Message "Type the name and  password of the local administrator account."
$vm = New-AzureRMVMConfig -VMName $vmName -VMSize "Standard_A1"
$vm = Set-AzureRMVMOperatingSystem -VM $vm -Windows -ComputerName $compName -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

# Step 6 - Attach storage and disks

$blobPath = "vhds/WindowsVMosDisk.vhd"
$osDiskUri = $storageAcc.PrimaryEndpoints.Blob.ToString() + $blobPath
$diskName = "windowsvmosdisk"
$vm = Set-AzureRmVMOSDisk -VM $vm -Name $diskName -VhdUri $osDiskUri -CreateOption fromImage

# Deploy the new VM

New-AzureRmVM -ResourceGroupName $rgName -Location $locName -VM $vm


###  If multiple dynamic nic's 
###   -Get Subnets already existing
###  $frontend = (Get-AzureRMVirtualNetwork -Name 'Cloudnet' -ResourceGroupName $rg.ResourceGroupName).Subnets[0]
###  $backend = = (Get-AzureRMVirtualNetwork -Name 'Cloudnet' -ResourceGroupName $rg.ResourceGroupName).Subnets[1]
###  - Define Nic Objects
###  $FrontNic = New-AzureRMNetworkInterface -Name 'VMFrontNic' -ResourceGroupName $rg.ResourceGroupName -Location 'east us' -Subnet $frontend
###  $BackNic = New-AzureRMNetworkInterface -Name 'VMBackNic' -ResourceGroupName $rg.ResourceGroupName -Location 'east us' -Subnet $backend
###  - Add Nic's to VM
###  Add-AzureRMVMNetworkInterface -VM $vm -Id $FrontNic.Id -Primary
###  Add-AzureRMVMNetworkInterface -VM $vm -Id $BackNic.Id



Leave a Reply

Your email address will not be published. Required fields are marked *