Skip to content

Instantly share code, notes, and snippets.

@giseongeom
Last active March 2, 2024 22:24
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save giseongeom/586752c5af8c282a29e7ed498afa1dc3 to your computer and use it in GitHub Desktop.
Save giseongeom/586752c5af8c282a29e7ed498afa1dc3 to your computer and use it in GitHub Desktop.
packer template (sysprep-safe)
{
"variables": {
"client_id": "{{env `AZURE_CLIENT_ID`}}",
"client_secret": "{{env `AZURE_CLIENT_SECRET`}}",
"subscription_id": "{{env `AZURE_SUBSCRIPTION_ID`}}",
"tenant_id": "{{env `AZURE_TENANT_ID`}}",
"spn_object_id": "{{env `AZURE_SPN_OBJECT_ID`}}",
"resource_group": "{{env `AZURE_BUILD_RESOURCE_GROUP`}}",
"resource_location": "{{env `AZURE_BUILD_LOCATION`}}",
"storage_account": "{{env `AZURE_BUILD_STORAGE_ACCOUNT`}}",
"vm_size": "{{env `AZURE_DEFAULT_VM_SIZE`}}",
"image_os_sku": "2016-Datacenter-Server-Core"
},
"builders": [{
"type": "azure-arm",
"client_id": "{{user `client_id`}}",
"client_secret": "{{user `client_secret`}}",
"subscription_id": "{{user `subscription_id`}}",
"tenant_id": "{{user `tenant_id`}}",
"object_id": "{{user `spn_object_id`}}",
"managed_image_name": "{{user `image_os_sku`}}--{{isotime \"2006.01.02-1504\"}}",
"managed_image_resource_group_name": "{{user `resource_group`}}",
"managed_image_storage_account_type": "Premium_LRS",
"os_type": "Windows",
"image_publisher": "MicrosoftWindowsServer",
"image_offer": "WindowsServer",
"image_sku": "{{user `image_os_sku`}}",
"communicator": "winrm",
"winrm_use_ssl": "true",
"winrm_insecure": "true",
"winrm_timeout": "5m",
"winrm_username": "packer",
"location": "{{user `resource_location`}}",
"vm_size": "{{user `vm_size`}}"
}],
"provisioners": [
{
"type": "windows-shell",
"inline": [
"powershell.exe -ExecutionPolicy bypass -windowstyle hidden -Command $PSVersionTable",
"systeminfo"
]
},
{
"type": "powershell",
"inline": [
"(gcim win32_service | ? { $_.name -match 'WindowsAzureGuestAgent' }).PathName",
"get-service WindowsAzureGuestAgent | ft -autosize"
]
},
{
"type": "powershell",
"inline": [
"if( Test-Path $Env:SystemRoot\\windows\\system32\\Sysprep\\unattend.xml ){ rm $Env:SystemRoot\\windows\\system32\\Sysprep\\unattend.xml -Force}",
"& $Env:SystemRoot\\System32\\Sysprep\\Sysprep.exe /oobe /generalize /quit /quiet",
"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 } }"
]
}
]
}
@giseongeom
Copy link
Author

  • build log
==> azure-arm: Provisioning with powershell script: C:\Users\giseong.eom\AppData\Local\Temp\packer-powershell-provisioner475239259
    azure-arm: #< CLIXML
    azure-arm: IMAGE_STATE_COMPLETE
    azure-arm: IMAGE_STATE_UNDEPLOYABLE
    azure-arm: IMAGE_STATE_UNDEPLOYABLE
    azure-arm: <Objs Version="1.1.0.1" xmlns="http://schemas.microsoft.com/powershell/2004/04"><Obj S="progress" RefId="0"><TN RefId="0"><T>System.Management.Automation.PSCustomObject</T><T>System.Object</T></TN><MS><I64 N="SourceId">1</I64><PR N="Record"><AV>Preparing modules for first use.</AV><AI>0</AI><Nil /><PI>-1</PI><PC>-1</PC><T>Completed</T><SR>-1</SR><SD> </SD></PR></MS></Obj></Objs>

@giseongeom
Copy link
Author

  • Launch VM using powershell
Select-AzureRmSubscription -SubscriptionId xxxxxxxx
$rgname   = 'xxxxxxxx'
$location = 'westcentralus'
$vmSize   = "Standard_DS3_v2"

$imagename = '2016-Datacenter-Server-Core--2017.09.28-0127'

$resid = (Get-Random).ToString().Substring(0, 4)
$image = Get-AzureRmImage -ImageName $imageName -ResourceGroupName $rgName
$ipName = "an-pip-$resid" 
$pip = New-AzureRmPublicIpAddress -Name $ipName -ResourceGroupName $rgName -Location $location -AllocationMethod Dynamic
$vnet = Get-AzureRmVirtualNetwork -ResourceGroupName $rgName
$nicName = "an-nic-$resid" 
$nic = New-AzureRmNetworkInterface -Name $nicName -ResourceGroupName $rgName -Location $location -SubnetId $vnet.Subnets[0].Id -PublicIpAddressId $pip.Id
$cred = $cred_azure_opsadmin
$vmName = "an-vm-$resid"
$computerName = "myCo-$resid" 
$vmconfig = New-AzureRmVMConfig -VMName $vmName -VMSize $vmSize
$vmsrcimage = Set-AzureRmVMSourceImage -vm $vmconfig -id $image.Id
$vmosdisk = Set-AzureRmVMOSDisk -VM $vmconfig  -StorageAccountType PremiumLRS -DiskSizeInGB 128 -CreateOption FromImage -Caching ReadWrite
$vmos = Set-AzureRmVMOperatingSystem -VM $vmconfig -Windows -ComputerName $computerName -Credential $cred -ProvisionVMAgent -EnableAutoUpdate
$vmbootdiag = Set-AzureRmVMBootDiagnostics -Disable -VM $vmconfig
$vmnic = Add-AzureRmVMNetworkInterface -VM $vmconfig -Id $nic.Id
New-azurermvm -VM $vmconfig -ResourceGroupName $rgName -Location $location -Verbose
  • output
Thursday, September 28, 2017 11:37:08 AM
VERBOSE: Performing the operation "New" on target "an-vm-2043".

RequestId IsSuccessStatusCode StatusCode ReasonPhrase
--------- ------------------- ---------- ------------
                         True         OK OK         

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment