Winhance icon indicating copy to clipboard operation
Winhance copied to clipboard

[BUG] DNS autounattend issues

Open oemskin opened this issue 7 months ago • 1 comments

Please confirm these before moving forward

  • [x] I have searched for my issue and have not found a similiar in the Open or Closed issues.
  • [x] I have tested that this issue has not been fixed in the latest (beta or stable) release.
  • [x] I have checked the Discussions section for solutions.
  • [x] This issue is about a bug (if it is not, please use the correct template).

Winhance Version

26.04.17

Windows version, edition, and architecture

Windows 11 Pro 24H2/25H2

Describe your issue

There are several issues with setting the DNS Server in winhance and then exporting teh autounattend.xml file.

  1. If I have a custom dns set the block of code in the xml file seems to be resetting the dns rather than setting it to the custom value

    try { Get-NetAdapter | ForEach-Object { Set-DnsClientServerAddress -InterfaceIndex $.InterfaceIndex -ResetServerAddresses } Write-Log "Select a DNS server for all network adapters. Changes apply to every adapter on your system (Wi-Fi and Ethernet). Use Automatic to restore your default ISP/router DNS" "SUCCESS" } catch { Write-Log "Failed: Select a DNS server for all network adapters. Changes apply to every adapter on your system (Wi-Fi and Ethernet). Use Automatic to restore your default ISP/router DNS - $($.Exception.Message)" "ERROR" }

  2. If I set the DNS Server to cloudflare in winhance the block of code in the xml file looks correct BUT it does not work. The code is in the HKLM section of the code. If I move the code down to the HKLM section of the code, it does work.

    PowerShell script for: DNS Server

    try { Get-NetAdapter | ForEach-Object { Set-DnsClientServerAddress -InterfaceIndex $.InterfaceIndex -ServerAddresses @('1.1.1.1','1.0.0.1') } Write-Log "Select a DNS server for all network adapters. Changes apply to every adapter on your system (Wi-Fi and Ethernet). Use Automatic to restore your default ISP/router DNS" "SUCCESS" } catch { Write-Log "Failed: Select a DNS server for all network adapters. Changes apply to every adapter on your system (Wi-Fi and Ethernet). Use Automatic to restore your default ISP/router DNS - $($.Exception.Message)" "ERROR" }

Steps to reproduce the issue

  1. Set DNS Server in winhance
  2. Export autounattend.xml (see issue 1 for custom DNS)
  3. Install Windows with autounattend.xml and see that the DNS is not set.

Expected behavior

  1. The exported code for a custom DNS should be the custom DNS IP address and not reset code.
  2. The DNS should be set in the installed Windows

Actual behavior

see above

Screenshots or Logs (if applicable)

No response

Additional context

It would be nice if the DNS would also set IPv6 and optionally set it all to be encrypted (DoH) Here is a simple script sample from AI:

1. Define Cloudflare DNS addresses and the DoH Template

$IPv4_DNS = @("1.1.1.1", "1.0.0.1") $IPv6_DNS = @("2606:4700:4700::1111", "2606:4700:4700::1001") $DoH_Template = "https://cloudflare-dns.com/dns-query"

2. Register Cloudflare as a known DoH provider in Windows

Write-Host "Registering Cloudflare DoH templates..." -ForegroundColor Cyan foreach ($IP in ($IPv4_DNS + $IPv6_DNS)) { Add-DnsClientDohServerAddress -ServerAddress $IP -DohTemplate $DoH_Template -AutoUpgrade $true -ErrorAction SilentlyContinue }

3. Apply to all active physical adapters

$Adapters = Get-NetAdapter | Where-Object { $_.Status -eq "Up" }

foreach ($Adapter in $Adapters) { Write-Host "Configuring adapter: $($Adapter.Name)..." -ForegroundColor Yellow

# Set IPv4 and IPv6 DNS Addresses
Set-DnsClientServerAddress -InterfaceAlias $Adapter.InterfaceAlias -ServerAddresses ($IPv4_DNS + $IPv6_DNS)

# 4. Force Encryption (DoH) via Registry
# This step is often required to ensure Windows UI shows "Encrypted (only)"
$InterfaceGuid = $Adapter.InterfaceGuid

# Configure IPv4 DoH Registry Keys
foreach ($IP in $IPv4_DNS) {
    $RegPath = "HKLM:\System\CurrentControlSet\Services\Dnscache\InterfaceSpecificParameters\$InterfaceGuid\DohInterfaceSettings\Doh\$IP"
    if (!(Test-Path $RegPath)) { New-Item -Path $RegPath -Force | Out-Null }
    New-ItemProperty -Path $RegPath -Name "DohFlags" -Value 1 -PropertyType QWORD -Force | Out-Null
}

# Configure IPv6 DoH Registry Keys
foreach ($IP in $IPv6_DNS) {
    $RegPath = "HKLM:\System\CurrentControlSet\Services\Dnscache\InterfaceSpecificParameters\$InterfaceGuid\DohInterfaceSettings\Doh6\$IP"
    if (!(Test-Path $RegPath)) { New-Item -Path $RegPath -Force | Out-Null }
    New-ItemProperty -Path $RegPath -Name "DohFlags" -Value 1 -PropertyType QWORD -Force | Out-Null
}

}

5. Flush DNS to apply changes

Clear-DnsClientCache Write-Host "Cloudflare Encrypted DNS (DoH) is now active for all physical adapters." -ForegroundColor Green

oemskin avatar Apr 19 '26 21:04 oemskin

Hey, thanks for the really clear write-up on both of these — diagnosing the HKLM-vs-HKCU split in the generated XML was exactly the clue that pointed to the root cause.

What was happening: Winhance's autounattend builder correctly sorts registry-setting payloads into the right pass (HKLM/system entries run during specialize as SYSTEM; HKCU/per-user entries run at first logon as the actual user). But the PowerShell scripts attached to some settings were hard-coded into the system pass regardless of what they touched. So the DNS script (which needs to talk to the network stack in the user's session) ended up running as SYSTEM during specialize, where it either silently failed or didn't stick to the final user context — that's exactly why moving it to the HKCU/first-logon section made it work for you.

The second half of the DNS issue was that when you picked Custom DNS, the exporter was reading placeholder values only from the preset options and never from the custom-entered IPs, so the script fell back to the 'disabled' template (-ResetServerAddresses) instead of using your IPs.

What changed:

  • PowerShell scripts now carry an explicit RunContext flag, and the DNS script is marked as user-context so it lands in the FirstLogon section where you confirmed it works.
  • The custom DNS path now reads your entered IPs and substitutes them into the Set-DnsClientServerAddress ... -ServerAddresses @('…','…') line — no more -ResetServerAddresses for custom.
  • Added catalog-level tests so any future setting author who writes an HKCU-touching script without marking it user-context will fail CI.

The IPv6 / DoH enhancement you suggested is a nice feature idea but separate from this bug — feel free to open a new feature request for it and we'll look at it independently.

This fix is in the development branch (eaceefc) and will be available for testing in Release 25.

memstechtips avatar Apr 20 '26 11:04 memstechtips