Skip to content

Instantly share code, notes, and snippets.

@darrenjrobinson
Last active April 4, 2017 00:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save darrenjrobinson/82d9a049baac7461d6b41d9510fb01c2 to your computer and use it in GitHub Desktop.
Save darrenjrobinson/82d9a049baac7461d6b41d9510fb01c2 to your computer and use it in GitHub Desktop.
Create Azure VM from an existing VHD in it's own Resource Group
# Variables for the Resource Group, Networking and the Virtual Machine
$ResourceGroupName ="BrewTools"
$Location = "West US"
$VMName = "WinXP"
# Retreived from storage account in Azure Portal after VHD upload
$OSDiskUri = "https://mystorage.blob.core.windows.net/vhds/WinXP.vhd"
$VMSize = "Standard_A2"
# Networking
$SubnetName = "BTools-Subnet"
$InterfaceName = "BTools-NIC1"
$VNetName = "BTools-VNet"
$VNetResourceGroupName = "BrewTools"
$OSDiskName = $VMName
# Login to Azure
Login-AzureRMAccount
# Networking
# Create Network Security Group, Subnet and Virtual Network
$NSG = New-AzureRmNetworkSecurityGroup -Name $ResourceGroupName -ResourceGroupName $ResourceGroupName -Location $Location
$Subnet = New-AzureRMVirtualNetworkSubnetConfig -Name $SubnetName -AddressPrefix 10.0.0.0/24 -NetworkSecurityGroup $NSG
$VNet = New-AzureRmVirtualNetwork -Name $VNetName -ResourceGroupName $VNetResourceGroupName -Location $Location -AddressPrefix 10.0.0.0/16 -Subnet $Subnet
# Create the Interface
$pip = New-AzureRmPublicIpAddress -Name "$VMName-IP" -ResourceGroupName $ResourceGroupName -Location $Location -AllocationMethod Dynamic
$Interface = New-AzureRMNetworkInterface -Name $InterfaceName -ResourceGroupName $ResourceGroupName -Location $Location -SubnetId $vnet.Subnets[0].Id -PublicIpAddressId $pip.Id
# VM Details
$VirtualMachine = New-AzureRMVMConfig -VMName $VMName -VMSize $VMSize # -AvailabilitySetID $AvailabilitySet.Id
$VirtualMachine = Add-AzureRMVMNetworkInterface -VM $VirtualMachine -Id $Interface.Id
$VirtualMachine = Set-AzureRMVMOSDisk -VM $VirtualMachine -Name $OSDiskName -VhdUri $OSDiskUri -CreateOption Attach -Windows
# Create the VM in Azure
New-AzureRMVM -ResourceGroupName $ResourceGroupName -Location $Location -VM $VirtualMachine
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment