Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.