Sysprep is a tool to capture the state of a Windows machine and reuse this as an image to create other Virtual Machines quickly from. The following article is intended to be used to create an image from an Azure Virtual machine to create other machines quickly.
Once the virtual machine is in a state to be imaged, follow these steps to Sysprep the machine.
Generalize a VM before creating an image – Azure Virtual Machines | Microsoft Docs
- Create a backup of the VM, as after the VM is Sysprep’d you cannot go back and make future changes. This will allow you to restore from a backup to maintain your image going forward. Wait until the backup completes before moving onto the next step.
- Create a new Virtual Machine from the backup that was just created. Go to the virtual machine, and choose Backup from the Operations section.
- Choose Restore VM to create the new virtual machine.
-
- Choose a restore point.
- Create the new Virtual Machine.
NOTE: Applications that were installed with per-user context will now cause sysprep to fail. An example application that will cause sysprep to fail is the Edge browser if used during the configuration of the machine. Follow the instructions below to remove the app before continuing with sysprep instructions.
- If any app is installed using the Per-user context, sysprep will fail. and you will have to perform the actions provided in the article below.
- https://learn.microsoft.com/en-us/troubleshoot/windows-client/deployment/sysprep-fails-remove-or-update-store-apps#resolution
- If applications need to be a part of your image, they will have to do done in the per-machine context. For more information on the two different contexts, please visit the site below.
- Installation Context – Win32 apps | Microsoft Learn
Steps to create the Sysprep Image:
- Once the new virtual machine is created, sign into your new Windows VM.
- Open a Command Prompt window as an administrator.
- Delete the panther directory (C:\Windows\Panther).
- Then change the directory to %windir%\system32\sysprep, and then run:
- c:\windows\system32\sysprep\sysprep.exe /oobe /generalize /shutdown /mode:vm
- Note: The /mode:vm switch seemed to address that other issue where in virtual machines view the VM would show up as updating… Now it shows updating for short period of time but then goes to Stopped.
- Go to the Azure Portal to view the virtual machine that is being imaged to gather the Virtual Machine name, the resource group name, and subscription ID.
- Open Cloud Shell or log into Azure on your PC locally.
- It is a good practice to set the subscription that you are working on.
- az account set –subscription “xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx”
- It is a good practice to set the subscription that you are working on.
- Then run this to tag the VM as generalized
- set-azvm -resourcegroupname XXXresourcegroupnameXXX -name XXXvmnameXXX -generalized
- If you would try to start the virtual machine it would show this message.
- This command may also be used to check to see if a virtual machine is generalized or not.
- get-azvm -resourcegroupname XXXresourcegroupnameXXX -name XXXvmnameXXX -status
- After the Virtual Machine is finished with the Sysprep command the virtual machine should go to a Stopped state. Once the status has changed to stopped. Stop the virtual machine in the Azure portal to deallocate the VM. This will stop the VM from being billed for compute.
- At this point the virtual machine may be captured to an image. Follow this article for the image capture process.
Note: There are other Sysprep articles intended for on-premises Virtual Machines. This refers to using the GUI version of Sysprep and command line syntax. It was determined that using these options on some SKU’s of VM’s did not generalize the Azure machines properly which caused failures after images were created causing timeouts during image deployments. https://docs.microsoft.com/en-us/azure/virtual-machines/windows/upload-generalized-managed
It is not recommended to use the above link for Azure Virtual Machines Sysprep process.
Other Sysprep Troubleshooting
Microsoft Store Apps cause Sysprep to fail – (ex: installing Edge Updates). This link describes issues when software is installed per user. It is unable to clean the application up and must be done manually.
Sysprep fails with Microsoft Store apps – Windows Client | Microsoft Learn
Registry Errors
It was determined that some Windows Server SKU’s were missing a Registry entry causing Sysprep failures.
Use Sysprep log files to look at errors. If you see this error in the log continue to the step 2.
- Look at the log file: C:\windows\System32\Sysprep\Panther\setuperr.log
- Error [0x0f0082] SYSPRP LaunchDll:Failure occurred while executing ‘DscCore.dll,SysPrep_Cleanup’, returned error code 0x2
- Set-ItemProperty -Path “HKLM:\SOFTWARE\Microsoft\DesiredStateConfiguration” -Name “AgentId” -Value “”
- This command was used if the value was not able to be able to be set properly with the reg add command noted in (a.)
Sysprep errors
- User received the Error Handler message during sysprep.
- User received the error: ‘A fatal error occurred while trying to sysprep the machine’
The article below references to checking several registry entries.
Fix: A Fatal Error Occurred While Trying to Sysprep the Machine – Appuals.com
Make sure to backup your registry before making any changes.
Check the registry to see if Sysprep has been ran multiple times, and correct the values.
- KEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\SoftwareProtectionPlatform\
- Change this value to 1
- HKEY_LOCAL_MACHINE\SYSTEM\Setup\Status\SysprepStatus
- Change CleanupState to 2 and GeneralizationState to 7
If the above do not fix the sysprep issue try this command below. Verify that the registry entries are correct before trying the next command.
- msdtc -uninstall
- msdtc -install