Skip to content

HostPrep.ps1 v4.4.0: offer to fix the ESXi hostname before certificate regeneration - #9

Merged
pauldiee merged 2 commits into
mainfrom
issue-8-fix-esxi-hostname
Jul 14, 2026
Merged

pauldiee merged 2 commits into
mainfrom
issue-8-fix-esxi-hostname

Conversation

@pauldiee

Copy link
Copy Markdown
Owner

Closes #8.

Why

/sbin/generate-certificates builds the certificate CN from the host's own configured hostname, not from the FQDN in the host list. A freshly imaged host that still calls itself localhost.localdomain therefore keeps minting localhost.localdomain certificates no matter how often it is regenerated — and each attempt costs a reboot.

The script already refused to waste that reboot, but it stopped there and left the operator to go fix the hostname by hand and re-run:

  CN does not match hostname. Regeneration needed.
  ESXi hostname 'localhost' does not match expected FQDN 'testhost.localdomain.local'.
  Certificate regeneration skipped: fix the ESXi hostname first or
  the regenerated certificate will still have the wrong CN.

What changed

New Repair-ESXiHostname. On a mismatch the script now offers to correct it:

  ESXi hostname 'localhost.localdomain' does not match expected FQDN 'esxi01.vcf.lab'.
  Fix the hostname on esxi01.vcf.lab so the certificate CN will match? [Y/N]: y
    Setting hostname : esxi01
    Setting domain   : vcf.lab
  Hostname updated to 'esxi01.vcf.lab'. Proceeding with certificate regeneration.
  • Y — sets hostname and domain from the host list via Set-VMHostNetwork, reads them back from the host to confirm the change stuck, then continues into regeneration. Regen reboots the host anyway, so the rename needs no reboot of its own.
  • N — previous behaviour: regen skipped, host flagged CN mismatch in the report.

Hosts are never renamed silently — hence the prompt, matching the -WipeDisk idiom. -DryRun reports the offer and changes nothing.

Also handled: a short name in the host list (no domain to derive) is refused rather than guessed at, and a rename that throws or silently fails to apply skips regeneration instead of rebooting into the same wrong CN.

Testing

Not yet run end-to-end against a live host — the guard that motivated it was hit on a live host, but the fix path has not been.

Repair-ESXiHostname was exercised in isolation with PowerCLI mocked:

Case Regen proceeds? Set-VMHostNetwork called with
Declines (N) no not called
Accepts (y) yes esxi01 / vcf.lab
Accepts, deep subdomain yes esxi01 / mgmt.vcf.lab
Short name, no domain no not called
Accepts, but change silently does not stick no called; read-back catches it
Set-VMHostNetwork throws no called; error caught

Note on versioning

Branched off main, so this is v4.4.0 and assumes #7 (v4.3.0) merges first. If #7 is merged, I can rebase this to keep the changelog linear.

🤖 Generated with Claude Code

pauldiee and others added 2 commits July 14, 2026 15:14
generate-certificates derives the certificate CN from the host's own
configured hostname, so a host that still calls itself
localhost.localdomain keeps minting the wrong CN however often it is
regenerated. The script already refused to waste a reboot on that, but it
left the operator to go fix the hostname by hand and re-run.

Add Repair-ESXiHostname: on a mismatch, prompt Y/N, set the hostname and
domain from the host list via Set-VMHostNetwork, read them back to confirm
the change stuck, then continue into regeneration -- which reboots the host
anyway, so the rename costs no extra reboot. Declining keeps the previous
behaviour (skip regen, flag CN mismatch in the report). Hosts are never
renamed silently.

Found while testing against a live host. (GitHub issue #8)

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
A host that obtains its DNS settings from DHCP takes its hostname and
domain from there as well. Set-VMHostNetwork accepts -HostName/-DomainName
on such a host, reports no error, and changes nothing -- so the read-back
reported a baffling "hostname still reads 'localhost' after the change".

Check DnsFromDhcp before prompting: explain that ESXi ignores a hostname
set while DHCP is in charge, and point at the static-DNS setting. No
pointless Y/N question, and no silent no-op call.

Hit on a live host: the rename appeared to succeed and did nothing.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@pauldiee
pauldiee force-pushed the issue-8-fix-esxi-hostname branch from 9ddf3e4 to 38e4e77 Compare July 14, 2026 13:14
@pauldiee
pauldiee merged commit 64a6e9a into main Jul 14, 2026
1 check passed
@pauldiee
pauldiee deleted the issue-8-fix-esxi-hostname branch July 14, 2026 13:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Cert regen: offer to fix the ESXi hostname instead of only skipping

1 participant