Please find the below packer script i used for your reference.
{
"variables": {
"subscription_id": "",
"WorkingDirectory": "{{env `System_DefaultWorkingDirectory`}}",
"Packages_folder": "C:\\HostedToolCache\\",
"root_folder": "C:"
},
"builders": [
{
"type": "azure-arm",
"subscription_id": "xxxxxxxxxxxxxxxxxxxxxx",
"client_id": "{{user `client_id`}}",
"tenant_id": "{{user `tenant_id`}}",
"managed_image_resource_group_name": "xxxxxxxxxxxxxxxxxxxxxxxxxx",
"build_resource_group_name": "xxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"managed_image_name": "mytestPackerImage1",
"os_type": "Windows",
"image_publisher": "MicrosoftWindowsServer",
"image_offer": "WindowsServer",
"image_sku": "2019-Datacenter",
"communicator": "winrm",
"winrm_use_ssl": true,
"winrm_insecure": true,
"winrm_timeout": "5m",
"winrm_username": "packer",
"azure_tags": {
"dept": "Engineering",
"task": "Image deployment"
},
"vm_size": "Standard_D2_v2"
}
],
"provisioners": [
{
"type": "file",
"source": "{{ template_dir }}/Packages/Java.zip",
"destination": "{{user `Packages_folder`}}"
},
{
"type": "powershell",
"inline": [
"Add-WindowsFeature Web-Server",
"& $env:SystemRoot\\System32\\Sysprep\\Sysprep.exe /oobe /generalize /quiet /quit",
"while($true) { $imageState = Get-ItemProperty HKLM:\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Setup\\State | Select ImageState; if($imageState.ImageState -ne 'IMAGE_STATE_GENERALIZE_RESEAL_TO_OOBE') { Write-Output $imageState.ImageState; Start-Sleep -s 10 } else { break } }"
]
}
]