Files
management-scripts/Fix-UpdateLocalHosts.ps1
T

80 lines
3.2 KiB
PowerShell
Raw Normal View History

$DNSHelper = $Global:ToolsDirectory + '\ekdns.exe'
2026-11-17 00:13:07 -05:00
# SHA-256 of ekdns.exe from each v2.3.0 Windows release archive.
If ([Environment]::Is64BitOperatingSystem) {
$DNSHelperURL = 'https://xfer.emberkom.com/shares/tools/folders/9b9988fb-c53a-4833-a4ad-68d01a3021bb/files/ekdns-x64-exe/download/ekdns-x64.exe'
$DNSHelperHash = '308E36680C0E4AD7CE802F4B1D8C38E89B0D8E24BBCE4F33550721C78BA2224C'
} Else {
2026-11-17 00:13:07 -05:00
$DNSHelperURL = 'https://xfer.emberkom.com/shares/tools/folders/9b9988fb-c53a-4833-a4ad-68d01a3021bb/files/ekdns-x86-exe/download/ekdns-x86.exe'
$DNSHelperHash = 'F987E75D8FFB87B587B57D2394131B072957C0E3EB6B73B4FB784E9B0D65277B'
}
2026-11-17 00:13:07 -05:00
$InstalledHash = (Get-FileHash -LiteralPath $DNSHelper -Algorithm SHA256 -ErrorAction SilentlyContinue).Hash
If ($InstalledHash -ne $DNSHelperHash) {
$StagedHelper = $DNSHelper + '.' + [guid]::NewGuid().ToString('N') + '.tmp'
Try {
Write-Output "Downloading latest version of ekDNSHelper"
$null = Download-File -URL $DNSHelperURL -File $StagedHelper
If ((Get-FileHash -LiteralPath $StagedHelper -Algorithm SHA256 -ErrorAction Stop).Hash -ne $DNSHelperHash) {
Throw 'Downloaded ekdns.exe failed SHA-256 verification.'
}
If ([IO.File]::Exists($DNSHelper)) {
[IO.File]::Replace($StagedHelper, $DNSHelper, [NullString]::Value)
} Else {
[IO.File]::Move($StagedHelper, $DNSHelper)
}
} Catch {
Write-Error $_.Exception.Message
Exit 1
} Finally {
Remove-Item -LiteralPath $StagedHelper -Force -ErrorAction SilentlyContinue
}
2024-06-07 14:30:07 -04:00
}
# Delete the old version of ekDNSHelper if it exists
If ( Test-Path ($Global:ToolsDirectory + '\dnshelper.exe') ) {
Write-Output "Deleting old version of ekDNSHelper: ${Global:ToolsDirectory}\dnshelper.exe"
Remove-Item -Path ($Global:ToolsDirectory + '\dnshelper.exe') -Force -ErrorAction SilentlyContinue
}
2024-07-01 11:19:20 -04:00
$RefreshScript = $Global:ToolsDirectory + '\refresh-local-hosts.bat'
2024-07-01 11:24:24 -04:00
$todaysDate = Get-LongDate
2024-07-01 11:19:20 -04:00
$RefreshScriptHeader = @"
@echo off
REM Refresh IP to name mappings in local hosts file
REM This script was created by an automatic process and should not be edited manually
REM Author: David Yoder, Emberkom LLC (Fix-UpdateLocalHosts.ps1)
2024-07-01 11:24:24 -04:00
REM Updated: ${todaysDate}`n
2024-07-01 11:19:20 -04:00
"@
2024-06-07 14:30:07 -04:00
$Hosts = @(
'auth.services.mspbackups.com',
'ws.mspbackups.com'
)
If ($BackupBucket.Length -ge 2 ) {
$Hosts += $BackupBucket
}
$HostString = ''
ForEach ($Hostname in $Hosts) {
If ($HostString.Length -gt 0) { $HostString = $HostString + ',' + $Hostname }
Else { $HostString = $Hostname }
}
If ( Test-Path $DNSHelper ) {
$commandString = """${DNSHelper}"" add -host ${HostString}"
2024-07-01 11:19:20 -04:00
$content = $RefreshScriptHeader + $commandString
Write-Output "Creating latest refresh script"
Try {
2026-11-17 00:13:07 -05:00
Set-Content -LiteralPath $RefreshScript -Value $content -Force -ErrorAction Stop
Write-Output "Updating local hosts file with name-to-IP mappings for the following host(s): ${HostString}"
& "${RefreshScript}"
If ($LASTEXITCODE -ne 0) { Exit $LASTEXITCODE }
2024-07-01 11:19:20 -04:00
}
2026-11-17 00:13:07 -05:00
Catch { Write-Error $_.Exception.Message ; Exit 1 }
2024-06-07 14:30:07 -04:00
} Else {
Write-Error "Cannot find the DNS Helper executable: ""${DNSHelper}"""
2026-11-17 00:13:07 -05:00
Exit 1
2024-06-07 14:30:07 -04:00
}