HEX
Server: Apache
System: Linux 185.122.168.184.host.secureserver.net 5.14.0-570.60.1.el9_6.x86_64 #1 SMP PREEMPT_DYNAMIC Wed Nov 5 05:00:59 EST 2025 x86_64
User: barbeatleanalyti (1024)
PHP: 8.1.33
Disabled: NONE
Upload Files
File: //var/opt/nydus/ops/customer_local_ops/operating_system/powershell/update_invalid_resolvers.ps1
param([Parameter(Mandatory)] [String] $validResolvers,
      [Parameter(Mandatory)] [String] $invalidResolvers)

function Get-Nic {
    if ($adapter = Get-NetAdapter -Name Ethernet) {
        return Get-WmiObject -Class "Win32_NetworkAdapterConfiguration" -Namespace "root\CIMV2" | Where-Object {$_.InterfaceIndex -eq $adapter.ifIndex}
    } else {
        throw "Adapter with name 'Ethernet' not found"
    }
}

function Test-Resolvers {
    # Use an internally and publicly resolvable domain to test, as the vm may not have a public IP
    $domain = "hfs-public.godaddy.com"
    return Test-NetConnection -Computername $domain -InformationLevel Quiet
}

function Backup-Resolvers {
    param([Parameter(Mandatory)] [String[]] $currentResolvers)
    $filePath = "C:\Windows\Temp\resolvers"
    "Backing up resolvers to file $filePath"
    $current_resolvers | Out-File -FilePath $filePath
}

function Restore-Resolvers {
    $filePath = "C:\Windows\Temp\resolvers"
    "Restoring resolvers from backup file $filePath"
    $old_resolvers = Get-Content $filePath
    "Old resolvers: $old_resolvers"
    $nic = Get-Nic
    $nic.SetDNSServerSearchOrder($old_resolvers)
}

function Remove-Backup {
    $filePath = "C:\Windows\Temp\resolvers"
    "Removing backup file $filePath"
    Remove-Item -Path $filePath -Force -ErrorAction SilentlyContinue
}

try {
    "Valid resolvers: $validResolvers"
    $invalid_resolvers_present = $False
    "Invalid resolvers: $invalidResolvers"

    $validResolversArray = $validResolvers.Split(",")
    $invalidResolversArray = $invalidResolvers.Split(",")

    $nic = Get-Nic

    $current_resolvers = $nic.DNSServerSearchOrder
    "Current resolvers are: $current_resolvers"
    $new_resolvers = @()

    # Loop through current resolvers and check for invalid resolvers. If a resolver is not invalid,
    # add it to the new resolvers list.
    foreach ($current_resolver in $current_resolvers) {
        if ($invalidResolversArray.Contains($current_resolver)) {
            "Current resolvers have invalid resolver: $current_resolver"
            $invalid_resolvers_present = $True
        } else {
            # Add resolver to new resolvers list
            $new_resolvers = $new_resolvers + $current_resolver
        }
    }

    "Invalid resolvers present: $invalid_resolvers_present"

    if ($invalid_resolvers_present) {
        # Test if network resolves. If it doesn't, we can't test if new resolvers will work.
        if (Test-Resolvers) {
            # We can proceed to trying to update the resolvers
            "Existing resolvers can resolve test domain"
            # Backup the existing resolvers before making any changes
            Backup-Resolvers -currentResolvers $current_resolvers
            # Update the resolvers
            # Add the valid resolvers to the new_resolvers list, if they are not there already
            foreach ($valid_resolver in $validResolversArray) {
                if (!$new_resolvers.Contains($valid_resolver)) {
                    $new_resolvers = $new_resolvers + $valid_resolver
                }
            }
            $nic = Get-Nic
            "Setting new resolvers: $new_resolvers"
            $ret = $nic.SetDNSServerSearchOrder($new_resolvers)
            $exit_code = $ret.ReturnValue
            "ReturnValue is $exit_code"
            if ($exit_code -eq 0) {
                # Update was OK
                "Resolvers updated successfully"
            } elseif ($exit_code -eq 1) {
                # Update was OK, but a reboot is required!
                # As we can't perform a reboot here, we'll test the new resolvers and revert to backup if they are not working
                "Resolvers were updated but a reboot may be required"
            } else {
                # Something went wrong with the update
                throw "Unable to update resolvers"
            }
            # Test if network resolves
            if (Test-Resolvers) {
                "New resolvers can resolve test domain successfully"
                $nic = Get-Nic
                $updated_resolvers = $nic.DNSServerSearchOrder
                "New resolvers: $updated_resolvers"
                Remove-Backup
                exit 0
            } else {
                # Restore the backup
                "Invalid resolvers were replaced, but test domain is not resolving."
                Restore-Resolvers
                # Re-test resolvers
                if (Test-Resolvers) {
                    "Restored resolvers can resolve test domain"
                    Remove-Backup
                    throw "Resolvers were replaced but old resolvers had to be restored, as new resolvers could not resolve test domain."
                } else {
                    throw "Restored resolvers cannot resolve test domain. Something may have gone wrong with the restore."
                }
            }
        } else {
            # We have no way to test if new resolvers will work, as the current configuration is not resolving
            throw "Current resolvers cannot resolve test domain. Unable to proceed."
        }
    } else {
        # There are no invalid resolvers in the current resolver list. Nothing to do here.
        "There are no invalid resolvers to replace."
        exit 0
    }
} catch {
"An error occurred: $_"
Write-Error $Error[0].ToString()
exit 1
}