updateHostsWindows.bat :: refactoring, fixed
authorFadeMind <redacted>
Wed, 4 Sep 2019 14:44:17 +0000 (16:44 +0200)
committerFadeMind <redacted>
Wed, 4 Sep 2019 14:44:17 +0000 (16:44 +0200)
- based on https://stackoverflow.com/a/10052222
- additional flags --auto --minimise (purged useless comments in final file = smaller size)  used
- tested on Windows 10 x64 - works well

updateHostsWindows.bat

index 0d7bd86d028e9e71053891c66809f9bf170114ce..2c4415543565d8666b01aaa058900f09b65393c3 100644 (file)
@@ -1,3 +1,4 @@
+::\r
 :: This script will first create a backup of the original or current hosts\r
 :: file and save it in a file titled "hosts.skel"\r
 ::\r
@@ -5,26 +6,56 @@
 :: will be copied to the proper path. Next, the DNS cache will be refreshed.\r
 ::\r
 :: THIS BAT FILE MUST BE LAUNCHED WITH ADMINISTRATOR PRIVILEGES\r
+:: Admin privileges script based on https://stackoverflow.com/a/10052222\r
+::\r
+::\r
 @ECHO OFF\r
 TITLE Update Hosts\r
 \r
 :: Check if we are administrator. If not, exit immediately.\r
+:: BatchGotAdmin\r
+:-------------------------------------\r
+REM  --> Check for permissions\r
+    IF "%PROCESSOR_ARCHITECTURE%" EQU "amd64" (\r
+>nul 2>&1 "%SYSTEMROOT%\SysWOW64\cacls.exe" "%SYSTEMROOT%\SysWOW64\config\system"\r
+) ELSE (\r
 >nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system"\r
-if %ERRORLEVEL% NEQ 0 (\r
-    ECHO This script must be run with administrator privileges!\r
-    ECHO Please launch command prompt as administrator. Exiting...\r
-    EXIT /B 1\r
 )\r
 \r
-if not exist "%WINDIR%\System32\drivers\etc\hosts.skel" (\r
-       COPY %WINDIR%\System32\drivers\etc\hosts %WINDIR%\System32\drivers\etc\hosts.skel\r
-)\r
+REM --> If error flag set, we do not have admin.\r
+if '%errorlevel%' NEQ '0' (\r
+    echo Requesting administrative privileges...\r
+    goto UACPrompt\r
+) else ( goto gotAdmin )\r
+\r
+:UACPrompt\r
+    echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"\r
+    set params= %*\r
+    echo UAC.ShellExecute "cmd.exe", "/c ""%~s0"" %params:"=""%", "", "runas", 1 >> "%temp%\getadmin.vbs"\r
+\r
+    "%temp%\getadmin.vbs"\r
+    del "%temp%\getadmin.vbs"\r
+    exit /B\r
+\r
+:gotAdmin\r
+    pushd "%CD%"\r
+    CD /D "%~dp0"\r
 \r
-:: Update hosts file\r
-python updateHostsFile.py -a\r
+:BackupHosts\r
+       :: Backup default hosts file\r
+       if not exist "%WINDIR%\System32\drivers\etc\hosts.skel" (\r
+               COPY %WINDIR%\System32\drivers\etc\hosts %WINDIR%\System32\drivers\etc\hosts.skel\r
+       )\r
 \r
-:: Move new hosts file in-place\r
-COPY hosts %WINDIR%\System32\drivers\etc\\r
+:UpdateHosts\r
+       :: Update hosts file\r
+       python updateHostsFile.py --auto --minimise\r
+       \r
+       :: Move new hosts file in-place\r
+       COPY hosts %WINDIR%\System32\drivers\etc\\r
 \r
-:: Flush the DNS cache\r
-ipconfig /flushdns\r
+       :: Flush the DNS cache\r
+       ipconfig /flushdns\r
+       \r
+       :: Summary note\r
+       pause\r
git clone https://git.99rst.org/PROJECT