Been trying to run Packer to configure some Windows images (Server 2022 and Windows 11 Enterprise) for testing purposes but I’m hitting some issues with WinRM timing out.
This is my first time using this solution, but as far as I can see the setup is ok on the base image I’m using for both systems.
WinRM has been setup with basic authentication to eliminate possible issues regarding certificates and TLS.
Packer is 1.7.10.
WinRM setup
winrm qc
Enable-PSRemoting -SkipNetworkProfileCheck -Force
Set-NetConnectionProfile -NetworkCategory Private
winrm set winrm/config/client/auth '@{Basic="true"}'
winrm set winrm/config/service/auth '@{Basic="true"}'
winrm set winrm/config/service '@{AllowUnencrypted="true"}'
winrm set winrm/config/client '@{AllowUnencrypted="true"}'
Set-Item WSMan:localhost\client\trustedhosts -value *
Is the system’s name HOSTNAME? It’s almost like it has no idea what the name of the system is, and is checking DNS for the information, and can’t find a computer with the name “HOSTNAME”.
Exactly. It’s just a placeholder but that’s supposed to be the hostname defined in the .ova.
What you’re saying makes perfect sense to me, but I can’t seem to find anything pointing to what may be influencing the hostname not being recognized.
Is it possible to use an IP instead, like 127.0.0.1?
Unfortunately, I haven’t set up our vSphere environment yet, just AWS and Azure. And I haven’t looked into virtualbox as of yet, as it’s not really in our environment.
Here is what I have for our WinRM setup for a different environment:
write-output "Running User Data Script"
write-output "(host) Running User Data Script"
Set-ExecutionPolicy Unrestricted -Scope LocalMachine -Force -ErrorAction Ignore
# Don't set this before Set-ExecutionPolicy as it throws an error
$ErrorActionPreference = "stop"
# Remove HTTP listener
Remove-Item -Path WSMan:\Localhost\listener\listener* -Recurse
$Cert = New-SelfSignedCertificate -CertstoreLocation Cert:\LocalMachine\My -DnsName "whateveryouwant"
New-Item -Path WSMan:\LocalHost\Listener -Transport HTTPS -Address * -CertificateThumbPrint $Cert.Thumbprint -Force
#Write-Host "Disable UAC"
New-ItemProperty -Path HKLM:Software\Microsoft\Windows\CurrentVersion\Policies\System -Name EnableLUA -PropertyType DWord -Value 0 -Force
New-ItemProperty -Path HKLM:Software\Microsoft\Windows\CurrentVersion\Policies\System -Name ConsentPromptBehaviorAdmin -PropertyType DWord -Value 0 -Force
# WinRM
write-output "Setting up WinRM"
write-output "(host) setting up WinRM"
cmd.exe /c winrm quickconfig -q
cmd.exe /c winrm set "winrm/config" '@{MaxTimeoutms="1800000"}'
cmd.exe /c winrm set "winrm/config/winrs" '@{MaxMemoryPerShellMB="1024"}'
cmd.exe /c winrm set "winrm/config/service" '@{AllowUnencrypted="true"}'
cmd.exe /c winrm set "winrm/config/client" '@{AllowUnencrypted="true"}'
cmd.exe /c winrm set "winrm/config/service/auth" '@{Basic="true"}'
cmd.exe /c winrm set "winrm/config/client/auth" '@{Basic="true"}'
cmd.exe /c winrm set "winrm/config/service/auth" '@{CredSSP="true"}'
cmd.exe /c winrm set "winrm/config/listener?Address=*+Transport=HTTPS" "@{Port=`"5986`";Hostname=`"whateveryouwant`";CertificateThumbprint=`"$($Cert.Thumbprint)`"}"
cmd.exe /c netsh advfirewall firewall set rule group="remote administration" new enable=yes
cmd.exe /c netsh firewall add portopening TCP 5986 "Port 5986"
cmd.exe /c net stop winrm
cmd.exe /c sc config winrm start= auto
cmd.exe /c net start winrm
Maybe if I can find some spare time, I can try to build something up. Not sure how long that will take me.
I’ll see if I can find what I’m using for WinRM for the AWS builders. I believe it’s similar, if not exactly the same, but maybe using batch instead of Powershell.
Using 127.0.0.1 instead of the hostname in winrm_host seemed to do the trick, at least so far. Still have some more cases do test.
Not really sure why it can resolve the hostname that was set in the .ova but that seems to be the problem.