Войти

Показать полную графическую версию : [решено] как добавлять в hosts из списка доменов


leonid.poydolov@fb
20-01-2023, 09:40
Подскажите как добавлять в hosts из списка доменов, чтобы в hosts подставлялось 127.0.0.1 ok.ru и т.д.
нашел пару скриптов но они только по одному добавляют или уже с ip
список доменов

$SitesToBlock =("ok.ru",
"vk.ru",
"dsen.ru")
код 1

Function BlockSiteHosts ( [Parameter(Mandatory=$true)]$Url) {
$hosts = 'C:\Windows\System32\drivers\etc\hosts'
$is_blocked = Get-Content -Path $hosts |
Select-String -Pattern ([regex]::Escape($Url))
If(-not $is_blocked) {
$hoststr="127.0.0.1" + $Url
Add-Content -Path $hosts -Value $hoststr
}
}
BlockSiteHosts ("twitter.com")
или код 2
$filehost = ([environment]::getfolderpath("Windows"))+'\system32\drivers\etc\hosts'
$cont = Get-Content $filehost
$adres = "10.10.10.10 server.domain.ru #1","10.10.10.11 server1.domain.ru"
$adres = $SitesToBlock
if (Test-Connection -ComputerName test.domain.ru -count 2 -quiet){
$cont | Where {!($adres -match "$_")} | out-file $filehost -enc ascii
}
else {
$adres | Where {!($cont -match "$_")} | Foreach {"`r`n$_"} | out-file $filehost -append -encoding ascii
}

DJ Mogarych
20-01-2023, 10:19
#Requires -RunAsAdministrator

$SitesToBlock = @(
"ok.ru"
"vk.ru"
"dsen.ru"
)

$hostsFile = "$env:SystemRoot\system32\drivers\etc\hosts"

$hosts = gc $hostsFile
if ($hosts -eq $null) {$hosts = @()}

$SitesToBlock |% {
if ($hosts -match "(?<!#.+)\s+$_") {
Write-Host -fore Green "Запись $_ уже есть"
}
else {
Write-Host -fore Yellow "Добавляется запись $_"
$hosts += "127.0.0.1`t$_"
}
}

$hosts |Set-Content $hostsfile -Force -Confirm:$false




© OSzone.net 2001-2012