Skip to content

Instantly share code, notes, and snippets.

@piyushawasthi
Last active May 25, 2018 13:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save piyushawasthi/cd32fc3cbcdfb5211e50772b0e7da2ff to your computer and use it in GitHub Desktop.
Save piyushawasthi/cd32fc3cbcdfb5211e50772b0e7da2ff to your computer and use it in GitHub Desktop.
Easy way to Create VM Image in Azure

Create VM Image in Azure

1: Create VM to create Image for same.

a: Open the Command Prompt window as an administrator.
b: Change the directory to %windir%\system32\sysprep
   
   cd C:\Windows\system32\sysprep
   
c: run sysprep.exe
   
   .\sysprep.exe
   

2: create-vmimage.ps1

Param(
	[Parameter(Mandatory=$true)]
	$ResourceGroupName,
	[Parameter(Mandatory=$true)]
	$SubscriptionId,
	[Parameter(Mandatory=$true)]
	$storageAccName,
    [Parameter(Mandatory=$true)]
	$VMName,
	[Parameter(Mandatory=$true)]
	$VHDNamePrefix
)

Login-AzureRmAccount

Select-AzureRmSubscription -SubscriptionId $SubscriptionId

Stop-AzureRmVM -ResourceGroupName $ResourceGroupName -Name $VMName -Force

Set-AzureRmVm -ResourceGroupName $ResourceGroupName -Name $VMName -Generalized

$vm = Get-AzureRmVM -ResourceGroupName $ResourceGroupName -Name $VMName -Status

$vm.Statuses

Save-AzureRmVMImage -ResourceGroupName $ResourceGroupName -Name $VMName -DestinationContainerName $ResourceGroupName.ToLower() -VHDNamePrefix $VHDNamePrefix -Path C:\testImage.json

  1. Run script on PowershellISE as
.\create-vmimage.ps1 -ResourceGroupName <ResourceGroupName> -SubscriptionId <SubscriptionId> -storageAccName <storageAccName> -VMName <VMName> -VHDNamePrefix <imagenameyouwant>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment