My customer wanted full Cloud Environments to be easily created without the need of too much head ache.

They wanted Virtual Machines, Load Balancers, VNets all in 1 Resource Group etc, I’ve recently been diving into Azure and this was a prefect opportunity for me to get this automated instead of using the Portal.Azure and manually entering things in (I’m all for Automation hehe)

Below is a .json Script which will deploy 2 VM’s, 1 Load Balancer, 2 Nics and 1 Vnet all built into 1 Resource Group, all the functions will prompt you for the information VM Name, Nic Name, etc etc, so there’s no need to edit the .json 🙂

 </span></pre>
<pre> "$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json",
 "contentVersion": "1.0.0.0",
 "parameters": {
 "storageAccountName": {
 "type": "string",
 "metadata": {
 "description": "Name of storage account"
 }
 },
 "adminUsername": {
 "type": "string",
 "metadata": {
 "description": "Admin username"
 }
 },
 "adminPassword": {
 "type": "securestring",
 "metadata": {
 "description": "Admin password"
 }
 },
 "dnsNameforLBIP": {
 "type": "string",
 "metadata": {
 "description": "DNS for Load Balancer IP"
 }
 },
 "vmNamePrefix": {
 "type": "string",
 "metadata": {
 "description": "Prefix to use for VM names"
 }
 },
 "imagePublisher": {
 "type": "string",
 "defaultValue": "MicrosoftWindowsServer",
 "metadata": {
 "description": "Image Publisher"
 }
 },
 "imageOffer": {
 "type": "string",
 "defaultValue": "WindowsServer",
 "metadata": {
 "description": "Image Offer"
 }
 },
 "imageSKU": {
 "type": "string",
 "defaultValue": "2012-R2-Datacenter",
 "metadata": {
 "description": "Image SKU"
 }
 },
 "lbName": {
 "type": "string",
 "metadata": {
 "description": "Load Balancer name"
 }
 },
 "nicNamePrefix": {
 "type": "string",
 "metadata": {
 "description": "Network Interface name prefix"
 }
 },
 "publicIPAddressName": {
 "type": "string",
 "metadata": {
 "description": "Public IP Name"
 }
 },
 "vnetName": {
 "type": "string",
 "metadata": {
 "description": "VNET name"
 }
 },
 "vmSize": {
 "type": "string",
 "defaultValue": "Standard_D1",
 "metadata": {
 "description": "Size of the VM"
 }
 }
 },
 "variables": {
 "storageAccountType": "Standard_LRS",
 "availabilitySetName": "myAvSet",
 "addressPrefix": "10.110.3.0/24",
 "subnetName": "Test-Subnet",
 "subnetPrefix": "10.110.3.0/28",
 "publicIPAddressType": "Dynamic",
 "vnetID": "[resourceId('Microsoft.Network/virtualNetworks',parameters('vnetName'))]",
 "subnetRef": "[concat(variables('vnetID'),'/subnets/',variables ('subnetName'))]",
 "publicIPAddressID": "[resourceId('Microsoft.Network/publicIPAddresses',parameters('publicIPAddressName'))]",
 "numberOfInstances": 2,
 "lbID": "[resourceId('Microsoft.Network/loadBalancers',parameters('lbName'))]",
 "frontEndIPConfigID": "[concat(variables('lbID'),'/frontendIPConfigurations/LoadBalancerFrontEnd')]",
 "lbPoolID": "[concat(variables('lbID'),'/backendAddressPools/BackendPool1')]",
 "lbProbeID": "[concat(variables('lbID'),'/probes/tcpProbe')]"
 },
 "resources": [
 {
 "type": "Microsoft.Storage/storageAccounts",
 "name": "[parameters('storageAccountName')]",
 "apiVersion": "2015-05-01-preview",
 "location": "[resourceGroup().location]",
 "properties": {
 "accountType": "[variables('storageAccountType')]"
 }
 },
 {
 "type": "Microsoft.Compute/availabilitySets",
 "name": "[variables('availabilitySetName')]",
 "apiVersion": "2015-05-01-preview",
 "location": "[resourceGroup().location]",
 "properties": {}
 },
 {
 "apiVersion": "2015-05-01-preview",
 "type": "Microsoft.Network/publicIPAddresses",
 "name": "[parameters('publicIPAddressName')]",
 "location": "[resourceGroup().location]",
 "properties": {
 "publicIPAllocationMethod": "[variables('publicIPAddressType')]",
 "dnsSettings": {
 "domainNameLabel": "[parameters('dnsNameforLBIP')]"
 }
 }
 },
 {
 "apiVersion": "2015-05-01-preview",
 "type": "Microsoft.Network/virtualNetworks",
 "name": "[parameters('vnetName')]",
 "location": "[resourceGroup().location]",
 "properties": {
 "addressSpace": {
 "addressPrefixes": [
 "[variables('addressPrefix')]"
 ]
 },
 "subnets": [
 {
 "name": "[variables('subnetName')]",
 "properties": {
 "addressPrefix": "[variables('subnetPrefix')]"
 }
 }
 ]
 }
 },
 {
 "apiVersion": "2015-05-01-preview",
 "type": "Microsoft.Network/networkInterfaces",
 "name": "[concat(parameters('nicNamePrefix'), copyindex())]",
 "location": "[resourceGroup().location]",
 "copy": {
 "name": "nicLoop",
 "count": "[variables('numberOfInstances')]"
 },
 "dependsOn": [
 "[concat('Microsoft.Network/virtualNetworks/', parameters('vnetName'))]",
 "[concat('Microsoft.Network/loadBalancers/', parameters('lbName'))]"
 ],
 "properties": {
 "ipConfigurations": [
 {
 "name": "ipconfig1",
 "properties": {
 "privateIPAllocationMethod": "Dynamic",
 "subnet": {
 "id": "[variables('subnetRef')]"
 },
 "loadBalancerBackendAddressPools": [
 {
 "id": "[concat(variables('lbID'), '/backendAddressPools/BackendPool1')]"
 }
 ],
 "loadBalancerInboundNatRules": [
 {
 "id": "[concat(variables('lbID'),'/inboundNatRules/RDP-VM', copyindex())]"
 }
 ]
 }
 }
 ]
 }
 },
 {
 "apiVersion": "2015-05-01-preview",
 "name": "[parameters('lbName')]",
 "type": "Microsoft.Network/loadBalancers",
 "location": "[resourceGroup().location]",
 "dependsOn": [
 "[concat('Microsoft.Network/publicIPAddresses/', parameters('publicIPAddressName'))]"
 ],
 "properties": {
 "frontendIPConfigurations": [
 {
 "name": "LoadBalancerFrontEnd",
 "properties": {
 "publicIPAddress": {
 "id": "[variables('publicIPAddressID')]"
 }
 }
 }
 ],
 "backendAddressPools": [
 {
 "name": "BackendPool1"
 }
 ],
 "inboundNatRules": [
 {
 "name": "RDP-VM0",
 "properties": {
 "frontendIPConfiguration": {
 "id": "[variables('frontEndIPConfigID')]"
 },
 "protocol": "tcp",
 "frontendPort": 50001,
 "backendPort": 3389,
 "enableFloatingIP": false
 }
 },
 {
 "name": "RDP-VM1",
 "properties": {
 "frontendIPConfiguration": {
 "id": "[variables('frontEndIPConfigID')]"
 },
 "protocol": "tcp",
 "frontendPort": 50002,
 "backendPort": 3389,
 "enableFloatingIP": false
 }
 }
 ],
 "loadBalancingRules": [
 {
 "name": "LBRule",
 "properties": {
 "frontendIPConfiguration": {
 "id": "[variables('frontEndIPConfigID')]"
 },
 "backendAddressPool": {
 "id": "[variables('lbPoolID')]"
 },
 "protocol": "tcp",
 "frontendPort": 80,
 "backendPort": 80,
 "enableFloatingIP": false,
 "idleTimeoutInMinutes": 5,
 "probe": {
 "id": "[variables('lbProbeID')]"
 }
 }
 }
 ],
 "probes": [
 {
 "name": "tcpProbe",
 "properties": {
 "protocol": "tcp",
 "port": 80,
 "intervalInSeconds": 5,
 "numberOfProbes": 2
 }
 }
 ]
 }
 },
 {
 "apiVersion": "2015-06-15",
 "type": "Microsoft.Compute/virtualMachines",
 "name": "[concat(parameters('vmNamePrefix'), copyindex())]",
 "copy": {
 "name": "virtualMachineLoop",
 "count": "[variables('numberOfInstances')]"
 },
 "location": "[resourceGroup().location]",
 "dependsOn": [
 "[concat('Microsoft.Storage/storageAccounts/', parameters('storageAccountName'))]",
 "[concat('Microsoft.Network/networkInterfaces/', parameters('nicNamePrefix'), copyindex())]",
 "[concat('Microsoft.Compute/availabilitySets/', variables('availabilitySetName'))]"
 ],
 "properties": {
 "availabilitySet": {
 "id": "[resourceId('Microsoft.Compute/availabilitySets',variables('availabilitySetName'))]"
 },
 "hardwareProfile": {
 "vmSize": "[parameters('vmSize')]"
 },
 "osProfile": {
 "computerName": "[concat(parameters('vmNamePrefix'), copyIndex())]",
 "adminUsername": "[parameters('adminUsername')]",
 "adminPassword": "[parameters('adminPassword')]"
 },
 "storageProfile": {
 "imageReference": {
 "publisher": "[parameters('imagePublisher')]",
 "offer": "[parameters('imageOffer')]",
 "sku": "[parameters('imageSKU')]",
 "version": "latest"
 },
 "osDisk": {
 "name": "osdisk",
 "vhd": {
 "uri": "[concat('http://',parameters('storageAccountName'),'.blob.core.windows.net/vhds/','osdisk', copyindex(), '.vhd')]"
 },
 "caching": "ReadWrite",
 "createOption": "FromImage"
 }
 },
 "networkProfile": {
 "networkInterfaces": [
 {
 "id": "[resourceId('Microsoft.Network/networkInterfaces',concat(parameters('nicNamePrefix'),copyindex()))]"
 }
 ]
 },
 "diagnosticsProfile": {
 "bootDiagnostics": {
 "enabled": "true",
 "storageUri": "[concat('http://',parameters('storageAccountName'),'.blob.core.windows.net')]"
 }
 }
 }
 }
 ]
}
}

 

 {
# Script Created by Craig SCCM N00B #

#####################################################################
# Import the Azure Cmdlets #
#####################################################################

Import-Module Azure

Import-Module AzureRM

#####################################################################
# Login to Azure Portal using your Credentials #
#####################################################################

Login-AzureRmAccount

#####################################################################
# Enter your Subscription Name #
#####################################################################

$Subscr = Read-Host "Enter your Subscription Name"
Set-AzureRmContext -SubscriptionName $SubScr 
Select-AzureRmSubscription -SubscriptionName $Subscr

#####################################################################
# Resource Group Deployment Details #
#####################################################################

$deployName = Read-Host "Enter your Deployment Name"
$RGName = Read-Host "Enter your Resource Group Name"
$locName = "West Europe"
New-AzureRmResourceGroup -Name $RGName -Location $locName 

#####################################################################
# Point your .json template to a location on the web or a directory # 
#####################################################################

$templateFileLoc = "C:\Users\CraigSCCMN00B\Documents\Visual Studio 2015\Projects\azureVMLBdeploy.json" 

New-AzureRmResourceGroupDeployment -Name $deployName -ResourceGroupName $RGName -TemplateFile $templateFileLoc 

}

Here is my Powershell Script – just point the template to the location of your .json file

Advertisements