Working with images in Azure PowerShell - The Legend of Hanuman

Working with images in Azure PowerShell


Deallocating and generalizing

 

Before creating an image from the source virtual machine, the virtual machine must first be deallocated and marked as generalized in Azure:

 

  1. Confirm the myVM2 virtual machine has been shut down by running the Get-AzureRmVm command:

 

      Get-AzureRmVm -Name MyVM2 -ResourceGroupName VMLab -status

 

  • Stop and deallocate the virtual machine by running the following command:

 

     Stop-AzureRmVm -ResourceGroupName VMLab -Name myVM2 -Force

 

  • Run the Get-AzureRmVm command to be sure the virtual machine is stopped and deallocated:

 

      Get-AzureRmVm -Name MyVM2 -ResourceGroupName VMLab -status

 

  • Once the virtual machine has stopped and shows deallocated when you run Get-AzureRmVm, set its status to generalized using the Set-AzureRmVm command:

 

      Set-AzureRmVM -ResourceGroupName VMLab -Name myVM2 -Generalized

 

  • Running the preceding command deallocates the source virtual machine and marks it as generalized, preparing it for the imaging process. Confirm that the machine is generalized by running the Get-AzureRmVm command:

 

      Get-AzureRmVm -Name MyVM2 -ResourceGroupName VMLab -status

You should see VM generalized as one of the statuses listed.

Creating an image

With the source virtual machine (myVM2) generalized and deallocated, it can now be used to generate an image. Creating an image requires the use of two different PowerShell commands: New-AzureRmImageConfig and New-AzureRmImage.

To create an image from myVM2, three things need to happen. First, the myVM2 virtual machine config needs to be loaded into a variable. Second, the image configuration needs to be created from myVM2. And third, the actual image needs to be created.

Follow these instructions to complete the necessary steps.

Retrieving the source VM

Run the following command to retrieve the source myVM2 info and store it in a variable:

 

$vm = Get-AzureRmVM -Name myVM2 -ResourceGroupName VMLab

This command will not generate any feedback on the screen, since all it’s doing is loading info into a variable.

Creating an image configuration

Run the New-AzureRmImageConfig command to create an image configuration that will be used to create the eventual image:

 

$image = New-AzureRmImageConfig `

 

-Location EastUS `

 

-SourceVirtualMachineId $vm.ID

Creating an image

The last step in image creation is the actual creation of the image from the image configuration. Run the New-AzureRmImage command to create the actual image:

 

New-AzureRmImage `

 

-Image $image `

 

-ImageName myImage `

 

-ResourceGroupName VMLab

Upon completion of the preceding steps, you are left with a new image, called myImage. The following screenshot shows the new image that was successfully created:



Share this content:

I am a passionate blogger with extensive experience in web design. As a seasoned YouTube SEO expert, I have helped numerous creators optimize their content for maximum visibility.

Leave a Comment