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:

 
Login-AzureRmAccount

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

1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading...