Create Azure VM from Snapshot — PowerShell

  1. Create a Snapshot (already created for this exercise)
  2. Create Managed disks from Operating System (OS) and Data Disk Snapshots
  3. Create a VM from the Managed OS Disk and add data disk
  • VNet
  • Subnet
  • PIP
  • Network Interface
$SourceResourceGroup = <YourSourceResourceGroupName>
$Location = <DestinationResourceGroupLocation>
$SnapshotName = <YourOSSnapShotName>
$ManagedDiskName = <OSManagedDiskName>
$DataDiskSnapshotName = <YourDataDiskSnapshotName>
$ManagedDataDiskName = <YourManagedDataDiskName>
$DestinationResourceGroup = <YOurDestinationResourceGroupName>
$StorageType = 'StandardLRS'
$VnetName = <YourVNetName>
$VnetAddressPrefix = <YourAddressChoice>
$SubnetName = <YourSubnetName>
$NewVmName = <NewVMName>
$NewVmSize = 'Standard_D2_v2_Promo'
$NewVm_pip = <YourNewVM_PIP>
$NewVm_nic = <YourVM_NIC>
New-AzureRmResourceGroup -DemoVM-RG -Location $Location
$VirtualNetwork = New-AzureRMVirtualNetwork -ResourceGroupName $DestinationResourceGroup -Location $Location -Name $VnetName -AddressPrefix $VnetAddressPrefix
$VirtualNetwork = Get-AzureRMVirtualNetwork -Name $VnetName -ResourceGroupName $DestinationResourceGroup
$SubNetConfig = Add-AzureRMVirtualNetworkSubnetConfig -Name $SubnetName -AddressPrefix $VnetAddressPrefix -VirtualNetwork $VirtualNetwork
$Snapshot = Get-AzureRmSnapshot -ResourceGroupName $SourceResourceGroup -SnapshotName $SnapshotName$NewOSDiskConfig = New-AzureRmDiskConfig -AccountType $StorageType -Location $Snapshot.Location -SourceResourceId $Snapshot.Id -CreateOption Copy
$NewOSDisk = New-AzureRmDisk -Disk $NewOSDiskConfig -ResourceGroupName $DestinationResourceGroup -DiskName $ManagedDiskName
$DataSnapshot = Get-AzureRmSnapshot -ResourceGroupName $SourceResourceGroup -SnapshotName $DataDiskSnapshotName$NewDataDiskConfig = New-AzureRmDiskConfig -AccountType $StorageType -Location $DataSnapshot.location -SourceResourceId $DataSnapshot.Id -CreateOption Copy
$NewDataDisk = New-AzureRmDisk -Disk $NewDataDiskConfig -ResourceGroupName $DestinationResourceGroup -DiskName $ManagedDataDiskName
$VNet = Get-AzureRmVirtualNetwork -Name $VnetName -ResourceGroupName $DestinationResourceGroup$publicIP = New-AzureRmPublicIpAddress -Name $NewVm_pip -ResourceGroupName $DestinationResourceGroup -Location $Location -AllocationMethod Dynamic$subnetid = (Get-azurermvirtualNetworkSubnetConfig -Name $SubnetName -VirtualNetwork $vnet).Id$NIC = New-AzureRmNetworkInterface -Name $NewVM_nic -ResourceGroupName $DestinationResourceGroup -Location centralus -SubnetId $subnetid -PublicIpAddressId $publicIP.Id$VirtualMachine = New-AzureRmVMConfig -VMName $NewVMName -VMSize $NewVMSize 
$VirtualMachine = Set-AzureRmVMOSDisk -VM $VirtualMachine -ManagedDiskId $NewOSDisk.Id -CreateOption Attach -Windows
$VirtualMachine = Add-AzureRmVMNetworkInterface -VM $VirtualMachine -Id $NIC.Id
$VirtualMachine = Set-AzureRmVMBootDiagnostics -VM $VirtualMachine -Disable
New-AzureRmVM -VM $VirtualMachine -ResourceGroupName $DestinationResourceGroup -Location $Location
Start-AzureRmVM -Name $NewVmName -ResourceGroupName $DestinationResourceGroup
$VirtualMachine = Get-AzureRmVM -ResourceGroupName $DestinationResourceGroup -Name $NewVmName
$ManagedDiskId = Get-AzureRmDisk -ResourceGroupName $DestinationResourceGroup -DiskName $ManagedDataDiskName
Add-AzureRmVMDataDisk -VM $VirtualMachine -Name $ManagedDataDiskName -ManagedDiskId $ManagedDiskId.IdUpdate-AzureRmVM -ResourceGroupName $DestinationResourceGroup -VM $VirtualMachine

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store