From 167678c8cfc8a0677fbd95917254b2eec289089b Mon Sep 17 00:00:00 2001 From: amd64fox <62529699+amd64fox@users.noreply.github.com> Date: Thu, 2 May 2024 10:20:00 +0300 Subject: [PATCH] Improved handling of incorrect hosts file #588 --- run.ps1 | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/run.ps1 b/run.ps1 index 35b12bf..6b2aff3 100644 --- a/run.ps1 +++ b/run.ps1 @@ -592,20 +592,24 @@ $hostsFilePath = Join-Path $Env:windir 'System32\Drivers\Etc\hosts' $hostsBackupFilePath = Join-Path $Env:windir 'System32\Drivers\Etc\hosts.bak' if (Test-Path -Path $hostsFilePath) { - $hosts = Get-Content -Path $hostsFilePath - if ($hosts -match '^[^\#|].+scdn.+|^[^\#|].+spotify.+') { + $hosts = [System.IO.File]::ReadAllLines($hostsFilePath) + $regex = "^(?!#|\|)((?:.*?(?:download|upgrade)\.scdn\.co|.*?spotify).*)" + + if ($hosts -match $regex) { + Write-Host ($lang).HostInfo Write-Host ($lang).HostBak Copy-Item -Path $hostsFilePath -Destination $hostsBackupFilePath -ErrorAction SilentlyContinue if ($?) { + Write-Host ($lang).HostDel + try { - $hosts = $hosts -replace '^[^\#|].+scdn.+|^[^\#|].+spotify.+', '' - $hosts = $hosts | Where-Object { $_.trim() -ne "" } - Set-Content -Path $hostsFilePath -Value $hosts -Force + $hosts = $hosts | Where-Object { $_ -notmatch $regex } + [System.IO.File]::WriteAllLines($hostsFilePath, $hosts) } catch { Write-Host ($lang).HostError -ForegroundColor Red