Bonjour,
J'ai besoin de mettre à jour automatiquement le fichier hosts de Windows 10 à partir d'une source RAW ici: https://raw.githubusercontent.com/no.../hostnames.txt
Comment automatiser cette tâche avec un fichier batch ?
Merci de votre aide.
Bonjour,
J'ai besoin de mettre à jour automatiquement le fichier hosts de Windows 10 à partir d'une source RAW ici: https://raw.githubusercontent.com/no.../hostnames.txt
Comment automatiser cette tâche avec un fichier batch ?
Merci de votre aide.
Trouvé, par contre comment forcer à écraser les fichiers existants dans batch ?
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 @echo off call:download "https://raw.githubusercontent.com/notracking/hosts-blocklists/master/hostnames.txt" "C:\Windows\System32\drivers\etc\hosts" exit /b :download (echo src = "%~1" echo Set v1 = CreateObject ("MSXML2.XMLHTTP"^) echo Set v2 = CreateObject ("ADODB.Stream"^) echo v1.open "GET", src, false echo v1.send (^) echo v2.open echo v2.Type = 1 echo v2.Write v1.ResponseBody echo v2.SaveToFile "%~2" ) >"%~dpn0.vbs" cscript "%~dpn0.vbs" del "%~dpn0.vbs" >nul goto:eof
- Il faut ajouter dans le vbscript adSaveCreateOverWrite l'option 2 dans ADO SaveToFile Method pour remplacer le fichier avec les données de l'objet Stream actuellement ouvert, si le fichier existe déjà.
- Exécuter le batch en tant qu'administrateur
Code Batch : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48 @echo off Color 9B & Mode con cols=80 lines=5 Title Download and update the hosts file from hosts-blocklists ::::::::::::::::::::::::::::::::::::::::: :: Automatically check & get admin rights ::::::::::::::::::::::::::::::::::::::::: REM --> Check for permissions Reg query "HKU\S-1-5-19\Environment" >nul 2>&1 REM --> If error flag set, we do not have admin. if '%errorlevel%' NEQ '0' ( Echo. ECHO ************************************** ECHO Running Admin shell... Please wait... ECHO ************************************** goto UACPrompt ) else ( goto gotAdmin ) :UACPrompt echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs" set params = %*:"="" echo UAC.ShellExecute "cmd.exe", "/c ""%~s0"" %params%", "", "runas", 1 >> "%temp%\getadmin.vbs" "%temp%\getadmin.vbs" del "%temp%\getadmin.vbs" exit /B :gotAdmin :::::::::::::::::::::::::::: ::START :::::::::::::::::::::::::::: Set "URL=https://raw.githubusercontent.com/notracking/hosts-blocklists/master/hostnames.txt" Set "FileLocation=%Windir%\System32\Drivers\etc\hosts" Call:download "%url%" "%FileLocation%" exit /b :download (echo src = "%~1" echo Set v1 = CreateObject ("MSXML2.XMLHTTP"^) echo Set v2 = CreateObject ("ADODB.Stream"^) echo v1.open "GET", src, false echo v1.send(^) echo v2.open echo v2.Type = 1 echo v2.Write v1.ResponseBody echo v2.SaveToFile "%~2",2 ) >"%~dpn0.vbs" cscript /nologo "%~dpn0.vbs" del "%~dpn0.vbs" >nul goto:eof
Merci hackoofr pour tes conseils et la correction du code.
Je te souhaite de bonne fête![]()
et bonne fête à vous tous
Voici une autre version qui vous permet de télécharger aussi le fichier hosts avec écrasement de l'ancien fichier en utilisant le batch et le powershell pour le téléchargement et la notification (BalloonTip) ; So Enjoy it
Code Batch : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58 @echo off Mode 80,5 & color 0A Title Downloading hosts file from web using powershell and batch by Hackoo 2018 ::::::::::::::::::::::::::::::::::::::::: :: Automatically check & get admin rights ::::::::::::::::::::::::::::::::::::::::: REM --> Check for permissions Reg query "HKU\S-1-5-19\Environment" >nul 2>&1 REM --> If error flag set, we do not have admin. if '%errorlevel%' NEQ '0' ( Echo( ECHO ************************************** ECHO Running Admin shell... Please wait... ECHO ************************************** goto UACPrompt ) else ( goto gotAdmin ) :UACPrompt echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs" set params = %*:"="" echo UAC.ShellExecute "cmd.exe", "/c ""%~s0"" %params%", "", "runas", 1 >> "%temp%\getadmin.vbs" "%temp%\getadmin.vbs" del "%temp%\getadmin.vbs" exit /B :gotAdmin :::::::::::::::::::::::::::: ::START :::::::::::::::::::::::::::: Mode 110,3 & color 0A Set "URL=https://raw.githubusercontent.com/notracking/hosts-blocklists/master/hostnames.txt" Set "FileLocation=%Windir%\System32\Drivers\etc\hosts" echo( echo Downloading "%URL%" Call :BalloonTip 'information' 10 '"Downloading hosts File"' "'Please wait... Downloading hosts File....'" 'info' 4 Call :Download "%url%" "%FileLocation%" Rem Notepad "%FileLocation%" Exit ::********************************************************************************* :Download <url> <File> Powershell.exe -command "(New-Object System.Net.WebClient).DownloadFile('%1','%2')" exit /b ::********************************************************************************* :BalloonTip $notifyicon $time $title $text $icon $Timeout PowerShell ^ [reflection.assembly]::loadwithpartialname('System.Windows.Forms') ^| Out-Null; ^ [reflection.assembly]::loadwithpartialname('System.Drawing') ^| Out-Null; ^ $notify = new-object system.windows.forms.notifyicon; ^ $notify.icon = [System.Drawing.SystemIcons]::%1; ^ $notify.visible = $true; ^ $notify.showballoontip(%2,%3,%4,%5); ^ Start-Sleep -s %6; ^ $notify.Dispose() %End PowerShell% exit /B ::********************************************************************************
Bonjour hackoofr,
Je te souhaite une bonne année ainsi qu'aux tiens.
Je te remercie pour le partage de ton code.
Avec Windows 10 pense tu qu'il soit possible de mettre à jour le fichier host de façon transparent (sans popup et notification) mais d'affiché une notification persistante uniquement en cas d'erreur.
Pour ma part, je lance le fichier bat tout les heures... Les notifications de mises à jours risque de vite devenir agaçantes à la longue.
Partager