Last active
March 11, 2020 04:40
-
-
Save darrenjrobinson/0960b5fec0b545262cd4 to your computer and use it in GitHub Desktop.
DeployAzureRMMIMLab
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#****Don't forget to go and get the Invoke-Parallel.ps1 script referenced in the associated blog post.***** | |
#Global Variables | |
# Where do we want to put the VM's | |
$global:locName = 'Australia East' | |
# Resource Group name | |
$global:rgName = 'MIM2016-Dev10' | |
# Virtual Network Name | |
$global:virtNetwork = 'MIM-Net10' | |
# Storage account names must be between 3 and 24 characters in length and use numbers and lower-case letters only | |
$global:stName = 'mimdev16021610' | |
# VMName | |
$global:NewVM = $null | |
# MIM Servers to Auto Deploy | |
$VMRole = @() | |
$VMRole += ,('MIMPortal1') | |
$VMRole += ,('MIMPortal2') | |
$VMRole += ,('MIMSync') | |
$VMRole += ,('ADDC1') | |
$VMRole += ,('ADDC2') | |
# Authenticate to the Azure Portal | |
Add-AzureRmAccount | |
# Get the UserID and Password info that we want associated with the new VM's. | |
$global:cred = Get-Credential -Message "Type the name and password for the local administrator account that will be created for your new VM(s)." | |
$SubscriptionName = Get-AzureRmSubscription | sort SubscriptionName | Select SubscriptionName | |
$SubscriptionName = $SubscriptionName.SubscriptionName | |
Select-AzureRmSubscription -SubscriptionName $SubscriptionName | |
# Create Resource Group | |
New-AzureRmResourceGroup -Name $rgName -Location $locName | |
# Create RG Storage Account | |
$storageAcc = New-AzureRmStorageAccount -ResourceGroupName $rgName -Name $stName -Type "Standard_GRS" -Location $locName | |
# Create RG Subnet | |
$singleSubnet = New-AzureRmVirtualNetworkSubnetConfig -Name singleSubnet -AddressPrefix 10.0.0.0/24 | |
# Create RG Network | |
$global:vnet = New-AzureRmVirtualNetwork -Name $virtNetwork -ResourceGroupName $rgName -Location $locName -AddressPrefix 10.0.0.0/16 -Subnet $singleSubnet | |
# VM Config for each VM | |
$VMConfig = @() | |
# Create VMConfigs and add to an array | |
foreach ($NewVM in $VMRole) { | |
# ******** Create IP and Network for the VM *************************************** | |
# *****We do this upfront before the bulk create of the VM************** | |
$pip = New-AzureRmPublicIpAddress -Name "$NewVM-IP1" -ResourceGroupName $rgName -Location $locName -AllocationMethod Dynamic | |
$nic = New-AzureRmNetworkInterface -Name "$NewVM-NIC1" -ResourceGroupName $rgName -Location $locName -SubnetId $vnet.Subnets[0].Id -PublicIpAddressId $pip.Id | |
$vm = New-AzureRmVMConfig -VMName $NewVM -VMSize "Standard_A1" | |
$vm = Set-AzureRmVMOperatingSystem -VM $vm -Windows -ComputerName $NewVM -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 | |
# VM Disks. Deploying an OS and a Data Disk for each | |
$osDiskUri = $storageAcc.PrimaryEndpoints.Blob.ToString() + "vhds/WindowsVMosDisk$NewVM.vhd" | |
$DataDiskUri = $storageAcc.PrimaryEndpoints.Blob.ToString() + "vhds/WindowsVMDataDisk$NewVM.vhd" | |
$vm = Set-AzureRmVMOSDisk -VM $vm -Name "windowsvmosdisk" -VhdUri $osDiskUri -CreateOption fromImage | |
$vm = Add-AzureRmVMDataDisk -VM $vm -Name "windowsvmdatadisk" -VhdUri $DataDiskUri -CreateOption Empty -Caching 'ReadOnly' -DiskSizeInGB 10 -Lun 0 | |
# Add the Config to an Array | |
$VMConfig += ,($vm) | |
# ******** End NEW VM *************************************** | |
} | |
# In Parallel Create all the VM's | |
$VMConfig | Invoke-Parallel -ImportVariables -ScriptBlock { | |
New-AzureRmVM -ResourceGroupName $rgName -Location $locName -VM $_ | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment