Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link
Owner Author

pietergheysens commented Jan 3, 2018

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
You can’t perform that action at this time.