Friday, June 29, 2018

Fully configured virtual machine create using PowerShell Script

If you are very concern on the naming-convention of your services when you are deploying your Azure hosted VM's then you can try the below mentioned script. This is purely support for Windows Server deployment.

Here I have done some additional work and took all the services into one place where you can do the changes in the first place and then run the deployment.

Steps are listed below.

1. First login into the AzureRM using the PowerShell



Once the login is successful then you will be able to see the below information.


2. Copy paste the below mentioned script and and changed the names as you required under the Variables.

ResourceGroup    - Give the resource group name. Define the Resource Group Name/s
Location                - Give the correct location name where you going to host you services. E.g. Australia East, UK South
VmName                - Server Names                                         
SubnetName          -  Subnet Name
NamevNET             - Virtual Network name.
Namemypublicdns- Public DNS Name.
NameNetworkSecurityGroupRuleRDP  - Network Security Group Rule name. Here you can add the additional ACLs and the script is used to enable only the RDP port. Later you need to add the IP restriction.
NameNetworkSecurityGroup – Network Security Group Name.
NameVNic               - Virtual Network name.
VMSize                     - The Size of the VM. This will need to select carefully with the requirement
AddressPrefix         - The  Address prefix need to be change under Subnet configuration and the Virtual Network Section.

PowerShell Script


# Variables for common values

$resourceGroup = "AAA-Production3"

$location = "West US 2"

$vmName = "AAA-Prod-SVR1"

$SubnetName = "AAA-PROD-SUBNET01"


$Namepublicdns = "AAA-PROD-ADF01-IP01"

$NameNetworkSecurityGroupRuleRDP = 'Default-allow-rdp'

$NameNetworkSecurityGroup = 'AAA-SVR1-NSG'

$NameVNic = "AAA-PROD-VNIC01"

$VMSize = 'Standard_D1_v2'

# Create user object

$cred = Get-Credential -Message "Enter a username and password for the virtual machine."

# Create a resource group

New-AzureRmResourceGroup -Name $resourceGroup -Location $location

# Create a subnet configuration

$subnetConfig = New-AzureRmVirtualNetworkSubnetConfig -Name $SubnetName -AddressPrefix

# Create a virtual network

$vnet = New-AzureRmVirtualNetwork -ResourceGroupName $resourceGroup -Location $location `

-Name $NamevNET -AddressPrefix -Subnet $subnetConfig

# Create a public IP address and specify a DNS name

$pip = New-AzureRmPublicIpAddress -ResourceGroupName $resourceGroup -Location $location `

-Name "$Namepublicdns$(Get-Random)" -AllocationMethod Static -IdleTimeoutInMinutes 4

# Create an inbound network security group rule for port 3389

$nsgRuleRDP = New-AzureRmNetworkSecurityRuleConfig -Name $NameNetworkSecurityGroupRuleRDP -Protocol Tcp `

-Direction Inbound -Priority 1000 -SourceAddressPrefix * -SourcePortRange * -DestinationAddressPrefix * `

-DestinationPortRange 3389 -Access Allow

# Create a network security group

$nsg = New-AzureRmNetworkSecurityGroup -ResourceGroupName $resourceGroup -Location $location `

-Name $NameNetworkSecurityGroup -SecurityRules $nsgRuleRDP

# Create a virtual network card and associate with public IP address and NSG

$nic = New-AzureRmNetworkInterface -Name $NameVNic -ResourceGroupName $resourceGroup -Location $location `

-SubnetId $vnet.Subnets[0].Id -PublicIpAddressId $pip.Id -NetworkSecurityGroupId $nsg.Id

# Create a virtual machine configuration

$vmConfig = New-AzureRmVMConfig -VMName $vmName -VMSize $VMSize | `

Set-AzureRmVMOperatingSystem -Windows -ComputerName $vmName -Credential $cred | `

Set-AzureRmVMSourceImage -PublisherName MicrosoftWindowsServer -Offer WindowsServer -Skus 2016-Datacenter -Version latest | `

Add-AzureRmVMNetworkInterface -Id $nic.Id

# Create a virtual machine

New-AzureRmVM -ResourceGroupName $resourceGroup -Location $location -VM $vmConfig


Same script can be download here

No comments: