Skip to content

Instantly share code, notes, and snippets.

@zachtuttle
Last active July 19, 2023 14:55
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zachtuttle/6a9d05e40c9a6b6c51bd6dc93e05c8a4 to your computer and use it in GitHub Desktop.
Save zachtuttle/6a9d05e40c9a6b6c51bd6dc93e05c8a4 to your computer and use it in GitHub Desktop.
configure winrm for packer
"user_data_file":"packer/packer-win-userdata.txt",
"communicator": "winrm",
"winrm_username":"Administrator",
"winrm_port": 5985,
"winrm_timeout": "10m"
###############################
<powershell>
# turn off PowerShell execution policy restrictions
Set-ExecutionPolicy -ExecutionPolicy Bypass -Scope LocalMachine
# configure WinRM
winrm quickconfig -q
winrm set winrm/config/winrs '@{MaxMemoryPerShellMB="2048"}'
winrm set winrm/config/winrs '@{MaxConcurrentUsers="100"}'
winrm set winrm/config/winrs '@{MaxProcessesPerShell="0"}'
winrm set winrm/config/winrs '@{MaxShellsPerUser="0"}'
winrm set winrm/config '@{MaxTimeoutms="7200000"}'
winrm set winrm/config/service '@{AllowUnencrypted="true"}'
winrm set winrm/config/service/auth '@{Basic="true"}'
winrm set winrm/config/service/auth '@{CredSSP="true"}'
winrm set winrm/config/client '@{TrustedHosts="*"}'
# open port 5985 in the internal Windows firewall to allow WinRM communication
netsh advfirewall firewall add rule name="WinRM 5985" protocol=TCP dir=in localport=5985 action=allow
net stop winrm
sc config winrm start=auto
net start winrm
</powershell>
@AshTechieDevOps
Copy link

Please guide on how to make this configuration automatically via packer build code. I am converting my windows 2019 iso to vhd on my local hyper-v hypervisor wherein I need to use winrm connectivity to call a powershell script to perform sysprep using provisioner block. But I am wondering to know that how can we automate this winrm configuration using packer build code to automatically configure all what is given here into the VM that I am building using packer code. Please suggest.

@tomwalsh
Copy link

tomwalsh commented Jul 19, 2023

Take the section in between the <powershell> and </powershell> and add it to a file named setup-winrm.ps1. Make sure that this file is included in your Packer build as a floppy file.

floppy_files = ["./scripts/10/setup-winrm.ps1"]

And then in your Autounattended.xml you call that file from the floppy disk.

<SynchronousCommand wcm:action="add">
     <CommandLine>cmd.exe /c C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -File a:\setup-winrm.ps1</CommandLine>
     <Order>21</Order>
     <Description>Setup WinRM</Description>
 </SynchronousCommand>

I am not including everything here, but if you understand the process that should be enough to get you moving in the right direction.

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