Azure, PowerShell, Scripts

Rapid Virtual Machine Deployment using Azure Managed Disks

With the recent release of generally available Managed Disks, the ability to rapidly deploy virtual machines using these disks has been greatly improved.

Below is a simple script to create a snapshot of an ARM virtual machine, create a Managed Disk from that snapshot and deploy as a new virtual machine.


# Source VM Details START
        $resourceGroupName = 'resourcegroup01'
        $location = 'australiaeast' 
        $dataDiskName = 'vm01-osdisk' 
        $snapshotName = 'vm01_snapshot'
        $snapshotResourceGroupName = 'resourcegroup02'
# Source VM Details END

# New VM Details START
        $vmName = 'vm02'
        $location = "australiaeast"
        $vmresourceGroupName = "resourcegroup01"
        $vnetname = 'vnet1'
        $pipName = "$vmName-pip"
        $nicName = "$vmName-nic"
        $vmSize = 'Standard_A2'
# New VM Details END

###Create Snapshot of Source VM START

        $disk = Get-AzureRmDisk -ResourceGroupName $resourceGroupName -DiskName $dataDiskName

        $snapshot =  New-AzureRmSnapshotConfig -SourceUri $disk.Id -CreateOption Copy -Location $location

        New-AzureRmSnapshot -Snapshot $snapshot -SnapshotName $snapshotName -ResourceGroupName $snapshotResourceGroupName

###Create Snapshot of Source VM END

###Create New VM using Snapshot START

        # ResourceGroupName parameter has to be specified, without it. Cmdlet returns all snapshots it can find regardless their name.
        $snapshot = Get-AzureRmSnapshot -SnapshotName $snapshotName -ResourceGroupName $snapshotResourceGroupName

        #$subnet = New-AzureRmVirtualNetworkSubnetConfig -Name $subnetName -AddressPrefix $subnetPrefix
        $vnet = Get-AzureRmVirtualNetwork -Name $vnetName -ResourceGroupName $resourceGroupName

        $pip = New-AzureRmPublicIpAddress -Name $pipName -ResourceGroupName $resourceGroupName `
            -Location $location -AllocationMethod Dynamic

        $nic = New-AzureRmNetworkInterface -Name $nicName -ResourceGroupName $resourceGroupName `
            -Location $location -SubnetId $vnet.Subnets[0].Id -PublicIpAddressId $pip.Id

        $managedDiskType = 'StandardLRS'
        $managedDiskCreateOption = 'Copy'
        $diskName = "$vmName-osdisk"
        $diskCreateOption = 'Attach'
        $vm = New-AzureRmVMConfig -VMName $vmName -VMSize $vmSize
        $vm = Add-AzureRmVMNetworkInterface -VM $vm -Id $nic.Id
        $diskConfig = New-AzureRmDiskConfig -AccountType $managedDiskType -Location $location -CreateOption $managedDiskCreateOption -SourceResourceId $snapshot.Id
        $osDisk = New-AzureRmDisk -DiskName $diskName -Disk $diskConfig -ResourceGroupName $resourceGroupName
        $vm = Set-AzureRmVMOSDisk -VM $vm -Name $diskName -ManagedDiskId $osDisk.Id -CreateOption $diskCreateOption -Windows -Caching ReadWrite
        New-AzureRmVM -ResourceGroupName $resourceGroupName -Location $location -VM $vm

###Create New VM using Snapshot END


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s