Skip to content

Instantly share code, notes, and snippets.

@pietergheysens
Created January 3, 2018 16:28
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save pietergheysens/d3ca8bea10fbd04586c8540eb153da58 to your computer and use it in GitHub Desktop.
Save pietergheysens/d3ca8bea10fbd04586c8540eb153da58 to your computer and use it in GitHub Desktop.
Create Visual Studio ALM Virtual Machines based on specialized vhd file in Microsoft Azure
Login-AzureRmAccount
Select-AzureRmSubscription -SubscriptionId "<yoursubscriptionid>"
$destinationResourceGroup = Read-Host -Prompt "What's the desired resource group name?"
$numberOfVMs = Read-Host -Prompt "How many VMs do you want to generate?"
$location = "West Europe"
$vmSize = "Standard_F4s_v2"
$accountType = "PremiumLRS"
#RESOURCE GROUP CREATION
New-AzureRmResourceGroup -Location $location -Name $destinationResourceGroup
$sourceUri = "<sourceidofvhdfileinazure>"
#SUBNET
$subnetName = "vsalmsubnet"
$singleSubnet = New-AzureRmVirtualNetworkSubnetConfig -Name $subnetName -AddressPrefix 10.0.0.0/24
#VNET
$vnetName = "vsalmVnetName"
$vnet = New-AzureRmVirtualNetwork -Name $vnetName -ResourceGroupName $destinationResourceGroup -Location $location -AddressPrefix 10.0.0.0/16 -Subnet $singleSubnet
#NSG
$nsgName = "vsalmNsg"
$rdpRule = New-AzureRmNetworkSecurityRuleConfig -Name myRdpRule -Description "Allow RDP" -Access Allow -Protocol Tcp -Direction Inbound -Priority 110 -SourceAddressPrefix Internet -SourcePortRange * -DestinationAddressPrefix * -DestinationPortRange 3389
$nsg = New-AzureRmNetworkSecurityGroup -ResourceGroupName $destinationResourceGroup -Location $location -Name $nsgName -SecurityRules $rdpRule
#LOOP FOR CREATING VMS
for($i=1;$i -le $numberOfVMs;$i++) {
$nameOfVM = "vsalm" + $i.ToString("000")
$osDisk = New-AzureRmDisk -DiskName $nameOfVM -Disk (New-AzureRmDiskConfig -AccountType $accountType -Location $location -CreateOption Import -SourceUri $sourceUri) -ResourceGroupName $destinationResourceGroup
$pip = New-AzureRmPublicIpAddress -Name $nameOfVM -ResourceGroupName $destinationResourceGroup -Location $location -AllocationMethod Dynamic
$nic = New-AzureRmNetworkInterface -Name $nameOfVM -ResourceGroupName $destinationResourceGroup -Location $location -SubnetId $vnet.Subnets[0].Id -PublicIpAddressId $pip.Id -NetworkSecurityGroupId $nsg.Id
$vmConfig = New-AzureRmVMConfig -VMName $nameOfVM -VMSize $vmSize
$vm = Add-AzureRmVMNetworkInterface -VM $vmConfig -Id $nic.Id
$vm = Set-AzureRmVMOSDisk -VM $vm -ManagedDiskId $osDisk.Id -StorageAccountType $accountType -DiskSizeInGB 128 -CreateOption Attach -Windows
New-AzureRmVM -ResourceGroupName $destinationResourceGroup -Location $location -VM $vm
}
@pietergheysens
Copy link
Author

credits to Microsoft Docs article about creating a VM from a specialized VM: https://docs.microsoft.com/en-us/azure/virtual-machines/windows/create-vm-specialized.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment