Skip to content

Instantly share code, notes, and snippets.

@najibninaba
Created May 22, 2016 06:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save najibninaba/77ba595e62938d8218376b4e46a3f9ba to your computer and use it in GitHub Desktop.
Save najibninaba/77ba595e62938d8218376b4e46a3f9ba to your computer and use it in GitHub Desktop.
Simple Powershell to provision Windows 2012 and Ubuntu 14.04 TLS servers from scratch using Powershell
# Provision Windows 2012 and Ubuntu 14.04 TLS servers from scratch using Powershell
# ----- Common Section
# Set the resource group name and location
$resourceGroup = "psod-iaas"
$location = "Southeast Asia"
# Logs into Azure and get the credentials for executing this in Azure.
Login-AzureRmAccount
# Provisions the new resource group.
New-AzureRmResourceGroup -Name $resourceGroup -Location $location
# Provisions a new storage account to store the VM disk image.
$storageAccountName = "psodiaasnajib"
New-AzureRmStorageAccount -Name $storageAccountName -ResourceGroupName $resourceGroup -Type Standard_LRS -Location $location
# Get the Storage Account reference so that we can use this for below.
$storageAcc = Get-AzureRmStorageAccount -ResourceGroupName $resourceGroup -Name $storageAccountName
# Provisions the VM networking.
$vnetName = "psod-iaas-net"
$subnet = New-AzureRmVirtualNetworkSubnetConfig -Name frontendSubnet -AddressPrefix 10.0.1.0/24
$vnet = New-AzureRmVirtualNetwork -Name $vnetName -ResourceGroupName $resourceGroup -Location $location -AddressPrefix 10.0.0.0/16 -Subnet $subnet
# ----- Windows 2012 server
# Provisions the network interface and sets the Public IP.
$nicName = "vm1-najib-nic"
$pip = New-AzureRmPublicIpAddress -Name $nicName -ResourceGroupName $resourceGroup -Location $location -AllocationMethod Dynamic
$nic = New-AzureRmNetworkInterface -Name $nicName -ResourceGroupName $resourceGroup -Location $location -SubnetId $vnet.Subnets[0].Id -PublicIpAddressId $pip.Id
# Configures the Windows VM including the VM name and the size.
$vmName = "win-web-najib"
$vm = New-AzureRmVMConfig -VMName $vmName -VMSize "Basic_A1"
# Sets the admin credentials for accessing the VM as well as the OS source image to install from.
$cred = Get-Credential -Message "Admin credentials for Windows VM"
$vm = Set-AzureRmVMOperatingSystem -VM $vm -Windows -ComputerName $vmName -Credential $cred -ProvisionVMAgent -EnableAutoUpdate
$vm = Set-AzureRmVMSourceImage -VM $vm -PublisherName "MicrosoftWindowsServer" -Offer "WindowsServer" -Skus "2012-R2-Datacenter" -Version "latest"
# Attaches the network interface (defined earlier) to the VM.
$vm = Add-AzureRmVMNetworkInterface -VM $vm -Id $nic.Id
# Configures the VM OS disk (defined earlier) for the VM.
$diskName = "win-os-disk"
$osDiskUri = $storageAcc.PrimaryEndpoints.Blob.ToString() + "vhds/" + $diskName + ".vhd"
$vm = Set-AzureRmVMOSDisk -VM $vm -Name $diskName -VhdUri $osDiskUri -CreateOption FromImage
# Finally, provisions the VM with the settings configured earlier.
New-AzureRmVM -ResourceGroupName $resourceGroup -Location $location -VM $vm
# ----- Linux server
# Provisions the network interface and sets the Public IP.
$nic2Name = "vm2-najib-nic"
$pip2 = New-AzureRmPublicIpAddress -Name $nic2Name -ResourceGroupName $resourceGroup -Location $location -AllocationMethod Dynamic
$nic2 = New-AzureRmNetworkInterface -Name $nic2Name -ResourceGroupName $resourceGroup -Location $location -SubnetId $vnet.Subnets[0].Id -PublicIpAddressId $pip2.Id
# Configures the Linux VM including the VM name and the size.
$vm2Name = "linux-web-najib"
$vm2 = New-AzureRmVMConfig -VMName $vm2Name -VMSize "Basic_A1"
# Sets the admin credentials for accessing the VM as well as the OS source image to install from.
$cred2 = Get-Credential -Message "Admin credentials for Linux VM"
$vm2 = Set-AzureRmVMOperatingSystem -VM $vm2 -Linux -ComputerName $vm2Name -Credential $cred2
$vm2 = Set-AzureRmVMSourceImage -VM $vm2 -PublisherName "Canonical" -Offer "UbuntuServer" -Skus "14.04.4-LTS" -Version "latest"
# Attaches the network interface (defined earlier) to the VM.
$vm2 = Add-AzureRmVMNetworkInterface -VM $vm2 -Id $nic2.Id
# Configures the VM OS disk (defined earlier) for the VM.
$disk2Name = "linux-os-disk"
$osDisk2Uri = $storageAcc.PrimaryEndpoints.Blob.ToString() + "vhds/" + $disk2Name + ".vhd"
$vm2 = Set-AzureRmVMOSDisk -VM $vm2 -Name $disk2Name -VhdUri $osDisk2Uri -CreateOption FromImage
# Finally, provisions the VM with the settings configured earlier.
New-AzureRmVM -ResourceGroupName $resourceGroup -Location $location -VM $vm2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment