Skip to content

Instantly share code, notes, and snippets.

@rodolfofadino
Created September 24, 2016 17:38
Show Gist options
  • Save rodolfofadino/46456febe76c83943d1051322820ea06 to your computer and use it in GitHub Desktop.
Save rodolfofadino/46456febe76c83943d1051322820ea06 to your computer and use it in GitHub Desktop.
Create 10 vms from vhd
Login-AzureRmAccount
Select-AzureRmSubscription -SubscriptionId "xxxx-xxxx-xxx-xxxx-xxx"
$rgName = "resourcegrup"
$location = "brazilsouth"
$storageName="storage"
New-AzureRmResourceGroup -Name $rgName -Location $location
New-AzureRmStorageAccount -ResourceGroupName $rgName -Name $storageName -Location $location -SkuName "Standard_LRS" -Kind "Storage"
$urlOfUploadedImageVhd="https://"+$storageName+".blob.core.windows.net/vhds/web.vhd"
Add-AzureRmVhd -ResourceGroupName $rgName -Destination $urlOfUploadedImageVhd -LocalFilePath "E:\VHD\web.vhd"
$avSet =New-AzureRmAvailabilitySet -Location $location -Name "dsavaliability" -ResourceGroupName $rgName -PlatformUpdateDomainCount 4 -PlatformFaultDomainCount 1
$AsObject = New-Object Microsoft.Azure.Management.Compute.Models.SubResource
$AsObject.Id = $avSet.Id
$cred = Get-Credential
$rule1 = New-AzureRmNetworkSecurityRuleConfig -Name rdp-rule -Description "Allow RDP" `
-Access Allow -Protocol Tcp -Direction Inbound -Priority 100 `
-SourceAddressPrefix Internet -SourcePortRange * `
-DestinationAddressPrefix * -DestinationPortRange 3389
$rule2 = New-AzureRmNetworkSecurityRuleConfig -Name web-rule -Description "Allow HTTP" `
-Access Allow -Protocol Tcp -Direction Inbound -Priority 101 `
-SourceAddressPrefix Internet -SourcePortRange * `
-DestinationAddressPrefix * -DestinationPortRange 80
$nsgName="NSG-FrontEnd"
$nsg = New-AzureRmNetworkSecurityGroup -ResourceGroupName $rgName -Location $location -Name $nsgname `
-SecurityRules $rule1,$rule2
$subnetName = "subnet"
$singleSubnet = New-AzureRmVirtualNetworkSubnetConfig -Name $subnetName -AddressPrefix "192.168.32.0/25"
$vnetName = "vnet"
$vnet = New-AzureRmVirtualNetwork -Name $vnetName -ResourceGroupName $rgName -Location $location -AddressPrefix "192.168.32.0/25" -Subnet $singleSubnet
for($i=0; $i -le 10; $i++){
$indice=$i.ToString()
$ipName = "ip0"+$indice
$pip = New-AzureRmPublicIpAddress -Name $ipName -ResourceGroupName $rgName -Location $location -AllocationMethod Dynamic
$nicName = "nic0"+$indice
$nic = New-AzureRmNetworkInterface -Name $nicName -ResourceGroupName $rgName -Location $location -SubnetId $vnet.Subnets[0].Id -PublicIpAddressId $pip.Id
# Name of the storage account where the VHD file is and where the OS disk will be created
$storageAccName = $storageName
# Name of the virtual machine
$vmName = "web0"+$indice
# Size of the virtual machine. See the VM sizes documentation for more information: https://azure.microsoft.com/documentation/articles/virtual-machines-windows-sizes/
$vmSize = "Standard_A3"
# Computer name for the VM
$computerName = "web0"+$indice
# Name of the disk that holds the OS
$osDiskName = "webv20"+$indice
$storageAcc = Get-A zureRmStorageAccount -ResourceGroupName $rgName -AccountName $storageAccName
$vmConfig = New-AzureRmVMConfig -VMName $vmName -VMSize $vmSize
$vm = Set-AzureRmVMOperatingSystem -VM $vmConfig -Windows -ComputerName $computerName -Credential $cred -ProvisionVMAgent -EnableAutoUpdate
$vm = Add-AzureRmVMNetworkInterface -VM $vm -Id $nic.Id
$osDiskUri = '{0}vhds/{1}-{2}.vhd' -f $storageAcc.PrimaryEndpoints.Blob.ToString(), $vmName.ToLower(), $osDiskName
$vm = Set-AzureRmVMOSDisk -VM $vm -Name $osDiskName -VhdUri $osDiskUri -CreateOption fromImage -SourceImageUri $urlOfUploadedImageVhd -Windows
$vm.AvailabilitySetReference=$AsObject
New-AzureRmVM -ResourceGroupName $rgName -Location $location -VM $vm
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment