Powershell to Create a VM from an copied VHD

If you are using Windows 10 or have Powershell 5 installed run

Install-Module AzureRM

Once installed, use the following to log into Azure in an interactive fashion:


Edit these lines , use Get-AzureRmVirtualNetwork to get your VirtualNetwork Name

Run these 

$vmName = “%vmname%”
$resourceGroupName = “%resourcename%”

$virtualNetworkName = “%virtualnetworkname%”
$locationName = “australiaeast”
$vmsize = “Standard_A1”
$destinationVhd = “https://vmname.blob.core.windows.net/vhds/osdisk.vhd”

$virtualNetwork = Get-AzureRmVirtualNetwork -ResourceGroupName $resourceGroupName -Name $virtualNetworkName
$publicIp = New-AzureRmPublicIpAddress -Name $vmName -ResourceGroupName $ResourceGroupName -Location $locationName -AllocationMethod Dynamic
$networkInterface = New-AzureRmNetworkInterface -ResourceGroupName $resourceGroupName -Name $vmName -Location $locationName -SubnetId $virtualNetwork.Subnets[0].Id -PublicIpAddressId $publicIp.Id
$vmConfig = New-AzureRmVMConfig -VMName $vmName -VMSize $vmsize
$vmConfig = Add-AzureRmVMNetworkInterface -VM $vmConfig -Id $networkInterface.Id
$vmConfig = Set-AzureRmVMOSDisk -VM $vmConfig -Name $vmName -VhdUri $destinationVhd -CreateOption Attach -Windows
$vm = New-AzureRmVM -VM $vmConfig -Location $locationName -ResourceGroupName $resourceGroupName

VN:F [1.9.22_1171]
Rating: 0.0/10 (0 votes cast)
VN:F [1.9.22_1171]
Rating: 0 (from 0 votes)

Tags: Create a VM from an copied VHD, VHD

Trackback from your site.