Hello,

In previous post, you have learned how to create Azure VM using Azure Portal. Now, I will show you how you can do that using Azure PowerShell.

Once you login to Azure tenant and select subscription, you can use following script.

# Assign variables
$rgName		= 'TechTrainerRG'
$vmName 	= 'TechTrainerVM'
$pubName	= 'MicrosoftWindowsServer'
$offerName	= 'WindowsServer'
$skuName	= '2016-Datacenter'
$vmSize 	= 'Standard_D1_v2'
$vnetName 	= 'TechTrainerVnet'
$subnetName = 'ServerSubnet'
$nsgName    = "$vmName-nsg"
$pipName    = "$vmName-pip" 
$nicName    = "$vmName-nic"
$osDiskName = "$vmName-osdisk"
$osDiskSize = 128
$osDiskType = 'StandardLRS'
$location = 'westeurope'
$storageaccountname = 'techtrainerstorage'

# Create resource group
New-AzureRmResourceGroup -Name $rgName -Location $location

# Create virtual network
$vnet = New-AzureRmVirtualNetwork -ResourceGroupName $rgName `
            -Name $vnetName `
            -AddressPrefix '10.10.0.0/16' `
            -Location $location
Add-AzureRmVirtualNetworkSubnetConfig -Name $subnetName `
            -VirtualNetwork $vnet `
            -AddressPrefix '10.10.1.0/24'
Set-AzureRmVirtualNetwork -VirtualNetwork $vnet


# Identify virtual network and subnet
$vnet = Get-AzureRmVirtualNetwork -Name $vnetName -ResourceGroupName $rgName
$subnetid = (Get-AzureRmVirtualNetworkSubnetConfig -Name $subnetName -VirtualNetwork $vnet).Id

# Identify the diagnostics storage account
$storageAccount	= New-AzureRmStorageAccount -Name $storageaccountname `
                    -ResourceGroupName $rgName `
                    -Location $location `
                    -SkuName Standard_LRS

# Create admin credentials
$adminUsername = 'vladimir'
$adminPassword = 'Pa55w.rd1234'
$adminCreds = New-Object PSCredential $adminUsername, ($adminPassword | ConvertTo-SecureString -AsPlainText -Force) 

# Create an NSG
$nsgRuleRDP = New-AzureRmNetworkSecurityRuleConfig -Name 'default-allow-rdp' `
                -Protocol Tcp `
                -Direction Inbound `
                -Priority 1000 `
                -SourceAddressPrefix * `
                -SourcePortRange * `
                -DestinationAddressPrefix * `
                -DestinationPortRange 3389 `
                -Access Allow
$nsg = New-AzureRmNetworkSecurityGroup -ResourceGroupName $rgName `
        -Location $location `
        -Name $nsgName `
        -SecurityRules $nsgRuleRDP

# Create a public IP and NIC
$pip = New-AzureRmPublicIpAddress -Name $pipName `
            -ResourceGroupName $rgName `
            -Location $location `
            -AllocationMethod Dynamic 
$nic = New-AzureRmNetworkInterface -Name $nicName `
            -ResourceGroupName $rgName `
            -Location $location `
            -SubnetId $subnetid `
            -PublicIpAddressId $pip.Id `
            -NetworkSecurityGroupId $nsg.Id

# Set VM Configuration
$vmConfig	= New-AzureRmVMConfig -VMName $vmName -VMSize $vmSize
Add-AzureRmVMNetworkInterface -VM $vmConfig -Id $nic.Id
Set-AzureRmVMBootDiagnostics -Enable -ResourceGroupName $rgName `
    -VM $vmConfig `
    -StorageAccountName $storageAccount
Set-AzureRmVMOperatingSystem -VM $vmConfig `
    -Windows `
    -ComputerName $vmName `
    -Credential $adminCreds 
Set-AzureRmVMSourceImage -VM $vmConfig `
    -PublisherName $pubName `
    -Offer $offerName `
    -Skus $skuName `
    -Version 'latest'
Set-AzureRmVMOSDisk -VM $vmConfig `
    -Name $osDiskName `
    -DiskSizeInGB $osDiskSize `
    -StorageAccountType $osDiskType `
    -CreateOption fromImage

#Create the VM
New-AzureRmVM -ResourceGroupName $rgName -Location $location -VM $vmConfig

 

This way is little bit complicated, especially if you are now familiar with PowerShell.

 

Cheers!

Leave a Reply

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