$npx -y skills add hashicorp/agent-skills --skill windows-builderBuild Windows images with Packer using WinRM communicator and PowerShell provisioners. Use when creating Windows AMIs, Azure images, or VMware templates.
| 1 | # Windows Builder |
| 2 | |
| 3 | Platform-agnostic patterns for building Windows images with Packer. |
| 4 | |
| 5 | **Reference:** [Windows Builders](https://developer.hashicorp.com/packer/guides/windows) |
| 6 | |
| 7 | > **Note:** Windows builds incur significant costs and time. Expect 45-120 minutes per build due to Windows Updates. Failed builds may leave resources running - always verify cleanup. |
| 8 | |
| 9 | ## WinRM Communicator Setup |
| 10 | |
| 11 | Windows requires WinRM for Packer communication. |
| 12 | |
| 13 | ### AWS Example |
| 14 | |
| 15 | ```hcl |
| 16 | source "amazon-ebs" "windows" { |
| 17 | region = "us-west-2" |
| 18 | instance_type = "t3.medium" |
| 19 | |
| 20 | source_ami_filter { |
| 21 | filters = { |
| 22 | name = "Windows_Server-2022-English-Full-Base-*" |
| 23 | } |
| 24 | most_recent = true |
| 25 | owners = ["amazon"] |
| 26 | } |
| 27 | |
| 28 | ami_name = "windows-server-2022-${local.timestamp}" |
| 29 | |
| 30 | communicator = "winrm" |
| 31 | winrm_username = "Administrator" |
| 32 | winrm_use_ssl = true |
| 33 | winrm_insecure = true |
| 34 | winrm_timeout = "15m" |
| 35 | |
| 36 | user_data_file = "scripts/setup-winrm.ps1" |
| 37 | } |
| 38 | ``` |
| 39 | |
| 40 | ### WinRM Setup Script (scripts/setup-winrm.ps1) |
| 41 | |
| 42 | ```powershell |
| 43 | <powershell> |
| 44 | # Configure WinRM |
| 45 | winrm quickconfig -q |
| 46 | winrm set winrm/config '@{MaxTimeoutms="1800000"}' |
| 47 | winrm set winrm/config/service '@{AllowUnencrypted="true"}' |
| 48 | winrm set winrm/config/service/auth '@{Basic="true"}' |
| 49 | |
| 50 | # Configure firewall |
| 51 | netsh advfirewall firewall add rule name="WinRM 5985" protocol=TCP dir=in localport=5985 action=allow |
| 52 | netsh advfirewall firewall add rule name="WinRM 5986" protocol=TCP dir=in localport=5986 action=allow |
| 53 | |
| 54 | # Restart WinRM |
| 55 | net stop winrm |
| 56 | net start winrm |
| 57 | </powershell> |
| 58 | ``` |
| 59 | |
| 60 | ### Azure Example |
| 61 | |
| 62 | ```hcl |
| 63 | source "azure-arm" "windows" { |
| 64 | client_id = var.client_id |
| 65 | client_secret = var.client_secret |
| 66 | subscription_id = var.subscription_id |
| 67 | tenant_id = var.tenant_id |
| 68 | |
| 69 | managed_image_resource_group_name = "images-rg" |
| 70 | managed_image_name = "windows-${local.timestamp}" |
| 71 | |
| 72 | os_type = "Windows" |
| 73 | image_publisher = "MicrosoftWindowsServer" |
| 74 | image_offer = "WindowsServer" |
| 75 | image_sku = "2022-datacenter-g2" |
| 76 | |
| 77 | location = "East US" |
| 78 | vm_size = "Standard_D2s_v3" |
| 79 | |
| 80 | # Azure auto-configures WinRM |
| 81 | communicator = "winrm" |
| 82 | winrm_use_ssl = true |
| 83 | winrm_insecure = true |
| 84 | winrm_timeout = "15m" |
| 85 | winrm_username = "packer" |
| 86 | } |
| 87 | ``` |
| 88 | |
| 89 | ## PowerShell Provisioners |
| 90 | |
| 91 | ### Install Software |
| 92 | |
| 93 | ```hcl |
| 94 | build { |
| 95 | sources = ["source.amazon-ebs.windows"] |
| 96 | |
| 97 | # Install Chocolatey |
| 98 | provisioner "powershell" { |
| 99 | inline = [ |
| 100 | "Set-ExecutionPolicy Bypass -Scope Process -Force", |
| 101 | "iex ((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1'))" |
| 102 | ] |
| 103 | } |
| 104 | |
| 105 | # Install applications |
| 106 | provisioner "powershell" { |
| 107 | inline = [ |
| 108 | "choco install -y googlechrome", |
| 109 | "choco install -y 7zip", |
| 110 | ] |
| 111 | } |
| 112 | |
| 113 | # Install IIS |
| 114 | provisioner "powershell" { |
| 115 | inline = [ |
| 116 | "Install-WindowsFeature -Name Web-Server -IncludeManagementTools" |
| 117 | ] |
| 118 | } |
| 119 | } |
| 120 | ``` |
| 121 | |
| 122 | ### Windows Updates |
| 123 | |
| 124 | ```hcl |
| 125 | provisioner "powershell" { |
| 126 | inline = [ |
| 127 | "Install-PackageProvider -Name NuGet -Force", |
| 128 | "Install-Module -Name PSWindowsUpdate -Force", |
| 129 | "Import-Module PSWindowsUpdate", |
| 130 | "Get-WindowsUpdate -Install -AcceptAll -AutoReboot", |
| 131 | ] |
| 132 | timeout = "2h" |
| 133 | } |
| 134 | |
| 135 | # Wait for reboots |
| 136 | provisioner "windows-restart" { |
| 137 | restart_timeout = "30m" |
| 138 | } |
| 139 | ``` |
| 140 | |
| 141 | ## Cleanup |
| 142 | |
| 143 | ```hcl |
| 144 | provisioner "powershell" { |
| 145 | inline = [ |
| 146 | "# Clear temp files", |
| 147 | "Remove-Item -Path 'C:\\Windows\\Temp\\*' -Recurse -Force -ErrorAction SilentlyContinue", |
| 148 | "# Clear Windows Update cache", |
| 149 | "Stop-Service -Name wuauserv -Force", |
| 150 | "Remove-Item -Path 'C:\\Windows\\SoftwareDistribution\\*' -Recurse -Force -ErrorAction SilentlyContinue", |
| 151 | "Start-Service -Name wuauserv", |
| 152 | ] |
| 153 | } |
| 154 | ``` |
| 155 | |
| 156 | ## Common Issues |
| 157 | |
| 158 | **WinRM Timeout** |
| 159 | - Increase `winrm_timeout` to 15m or more |
| 160 | - Verify security group allows ports 5985/5986 |
| 161 | - Check user data script completed successfully |
| 162 | |
| 163 | **PowerShell Execution Policy** |
| 164 | ```hcl |
| 165 | provisioner "powershell" { |
| 166 | inline = [ |
| 167 | "Set-ExecutionPolicy Bypass -Scope Process -Force", |
| 168 | "# Your commands here", |
| 169 | ] |
| 170 | } |
| 171 | ``` |
| 172 | |
| 173 | **Long Build Times** |
| 174 | - Windows Updates can take 1-2 hours |
| 175 | - Use pre-patched base images when available |
| 176 | - Set provisioner `timeout = "2h"` |
| 177 | |
| 178 | ## References |
| 179 | |
| 180 | - [Packer Windows Builders](https://developer.hashicorp.com/packer/guides/windows) |
| 181 | - [WinRM Communicator](https://developer.hashicorp.com/packer/docs/communicators/winrm) |
| 182 | - [PowerShell Provisioner](https://developer.hashicorp.com/packer/docs/provisioners/powershell) |