Updating Hosts file via PowerShell

Update hosts file via PowerShell

To manage a printer or device through, for example, a web portal, go to http://w.x.y.z. If the web portal uses a self-signed SSL certificate, management can be problematic. Browsers often do not trust self-signed certificates, which leads to errors or security warnings. To work around this issue, updating the hosts file via PowerShell is an option. This is especially useful if you already know the IP address and the name of the printer.

You can easily update the hosts file via PowerShell. Open PowerShell ISE with Administrator rights. This is important, as changes to the hosts file require system-level permissions.

Script for updating hosts file via PowerShell

Here’s a step-by-step guide to updating the hosts file via PowerShell:

  1. Open PowerShell ISE as Administrator.
  2. Run the script below to update the hosts file.
Update-HostsEntry -IPAddress "192.168.178.189" -HostName "HPDF79852"

The script adds a new line to the hosts file, linking the IP address “192.168.178.189” to the hostname “HPDF79852”. After running the script, you should be able to access the printer’s web portal using the specified hostname.

function Update-HostsEntry {
    param (
        [string]$IPAddress,
        [string]$HostName
    )

    $hostsFilePath = "C:\Windows\System32\drivers\etc\hosts"

    # Controleer of het script met verhoogde rechten wordt uitgevoerd
    if (-Not ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) {
        Write-Error "Dit script moet met verhoogde rechten (als administrator) worden uitgevoerd."
        return
    }

    # Controleer of het bestand bestaat
    if (-Not (Test-Path -Path $hostsFilePath)) {
        Write-Error "Het hosts bestand bestaat niet op de opgegeven locatie."
        return
    }

    # Lees de inhoud van het hosts bestand
    $hostsFileContent = Get-Content -Path $hostsFilePath

    # Verwijder de laatst toegevoegde entry als het IP-adres wijzigt
    $lastEntry = $hostsFileContent | Select-Object -Last 1
    if ($lastEntry -match "\b$HostName\b") {
        $hostsFileContent = $hostsFileContent | Where-Object { $_ -ne $lastEntry }
    }

    # Voeg de nieuwe regel toe
    $newEntry = "$IPAddress`t$HostName"
    $hostsFileContent += $newEntry

    # Schrijf de bijgewerkte inhoud terug naar het hosts bestand
    $hostsFileContent | Set-Content -Path $hostsFilePath -Force

    Write-Output "Het hosts bestand is succesvol bijgewerkt met $IPAddress`t$HostName."
}
# Voorbeeld van het aanroepen van de functie
# Update-HostsEntry -IPAddress "192.168.178.189" -HostName "example.com"

More information about DNS and the hosts file at Hostnet:
Bypassing DNS with a HOSTS file modification – Guides | Hostnet

More information about Dynamics 365 Business Central can be found here.

Using the hosts file to bypass DNS issues is a helpful technique that is often used. It enables you to quickly and easily resolve network problems. This can be particularly useful when you need quick access to a device or server, such as when managing printers or other network equipment.

In corporate networks, you can use internal DNS to bypass this issue. You can enable or disable Active Directory printer settings using Group Policy settings. These settings are included in Group Policy Objects linked to Active Directory containers such as sites, organizational units, and domains. This structure enhances and extends Active Directory.

0 Shares:
You May Also Like