During this module, you will learn how to Capture a windows VM as an Image on Azure.
After completing the exercises in this module, you will be able to:
- Prepare the VM for image capture
- Capture the VM using PowerShell
- Generalize a virtual machine
- Completion of Module on Storage
30 min
Using Azure PowerShell create a generalized image of an existing Azure VM. You can then use the image to create another VM. This image includes the OS disk and the data disks that are attached to the virtual machine. The image doesn't include the virtual network resources, so you need to set up those resources when you create a VM using the image.
-
Sign in to your Windows virtual machine. In the Azure portal, navigate through Browse > Virtual machines > Your Windows virtual machine > Connect.
-
Open a Command Prompt window as an administrator.
-
Change the directory to %windir%\system32\sysprep, and then run sysprep.exe.
-
In the System Preparation Tool dialog box, do the following:
-
Click OK.
Login-AzureRmAccount
Get-AzureRmVM -ResourceGroupName MYGROUP2 -Name myvm1
## Code: PowerState/stopped
## DisplayStatus : VM Stopped
$vm = Get-AzureRmVM -ResourceGroupName MYGROUP2 -Name myvm1 -status
$vm.Statuses
Stop-AzureRmVM -ResourceGroupName MYGROUP2 -Name myvm1 -Verbose
## Code : PowerState/deallocated
## DisplayStatus : VM deallocated
$vm = Get-AzureRmVM -ResourceGroupName MYGROUP2 -Name myvm1 -status
$vm.Statuses
- Note that you will need to do this because the generalization step above (sysprep) does not do it in a way that Azure can understand.
Set-AzureRmVm -ResourceGroupName MYGROUP2 -Name myvm1 -Generalized
- Note: The generalized state as set above will not be shown on the portal. However, you can verify it by using the Get-AzureRmVM command.
## Code : OSState/generalized
## DisplayStatus : VM generalized
$vm = Get-AzureRmVM -ResourceGroupName MYGROUP2 -Name myvm1 -status
$vm.Statuses
<# Output
Code : OSState/generalized
Level : Info
DisplayStatus : VM generalized
Message :
Time :
#>
Save-AzureRmVMImage -ResourceGroupName MYGROUP2 -Name myvm1 -DestinationContainerName myimage -VHDNamePrefix customimage -Path C:\Users\user1\Documents\temp\customimage.json -Verbose
Location of VHD of the image : https://mystorageaccountft.blob.core.windows.net/system/Microsoft.Compute/Images/image/customimage-osDisk.7e5505ce-57c5-4cbb-a881-258a60b5c2df.vhd
Get-AzureRmStorageAccount | Get-AzureStorageContainer| Get-AzureStorageBlob |select Name
<# output
Microsoft.Compute/Images/image/customimage-osDisk.7e5505ce-57c5-4cbb-a881-258a60b5c2df.vhd
Microsoft.Compute/Images/image/customimage-vmTemplate.7e5505ce-57c5-4cbb-a881-258a60b5c2df.json
myvm1.7e5505ce-57c5-4cbb-a881-258a60b5c2df.status
myvm1_os.vhd
#>