Created
January 3, 2018 16:28
-
-
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
This file contains hidden or 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
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 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.