updateHostsWindows.bat: minor consistency changes

* fix a few typos
* fix indentation
* lowercase commands since on Windows case doesn't matter
* add `/y /v` flags in copy commands
* remove unneeded `pushd` command since we `cd` after that anyway
* add `wscript.exe` when calling the .vbs script
This commit is contained in:
XhmikosR 2020-02-22 15:38:36 +02:00
parent df0ea73da2
commit 229b3593cf

View File

@ -1,61 +1,61 @@
:: ::
:: This script will first create a backup of the original or current hosts :: This script will first create a backup of the original or the current hosts
:: file and save it in a file titled "hosts.skel" :: file and save it in a file named "hosts.skel".
:: ::
:: If "hosts.skel" exists, the new hosts file with the customized unified hosts :: If the "hosts.skel" file exists, the new hosts file with the customized unified
:: will be copied to the proper path. Next, the DNS cache will be refreshed. :: hosts will be copied to the proper path. Next, the DNS cache will be refreshed.
:: ::
:: THIS BAT FILE MUST BE LAUNCHED WITH ADMINISTRATOR PRIVILEGES :: THIS BAT FILE MUST BE LAUNCHED WITH ADMINISTRATOR PRIVILEGES
:: Admin privileges script based on https://stackoverflow.com/a/10052222 :: Admin privileges script based on https://stackoverflow.com/a/10052222
:: ::
::
@ECHO OFF @echo off
TITLE Update Hosts title Update Hosts
:: Check if we are administrator. If not, exit immediately. :: Check if we are an administrator. If not, exit immediately.
:: BatchGotAdmin :: BatchGotAdmin
:------------------------------------- :: Check for permissions
REM --> Check for permissions if "%PROCESSOR_ARCHITECTURE%" equ "amd64" (
IF "%PROCESSOR_ARCHITECTURE%" EQU "amd64" ( >nul 2>&1 "%SYSTEMROOT%\SysWOW64\cacls.exe" "%SYSTEMROOT%\SysWOW64\config\system"
>nul 2>&1 "%SYSTEMROOT%\SysWOW64\cacls.exe" "%SYSTEMROOT%\SysWOW64\config\system" ) else (
) ELSE ( >nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system"
>nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system" )
)
:: If the error flag set, we do not have admin rights.
REM --> If error flag set, we do not have admin. if %ERRORLEVEL% neq 0 (
if '%errorlevel%' NEQ '0' ( echo Requesting administrative privileges...
echo Requesting administrative privileges... goto UACPrompt
goto UACPrompt ) else (
) else ( goto gotAdmin ) goto gotAdmin
)
:UACPrompt
echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs" :UACPrompt
set params= %* echo Set UAC = CreateObject^("Shell.Application"^) > "%TEMP%\getadmin.vbs"
echo UAC.ShellExecute "cmd.exe", "/c ""%~s0"" %params:"=""%", "", "runas", 1 >> "%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" wscript.exe "%TEMP%\getadmin.vbs"
exit /B del "%TEMP%\getadmin.vbs"
exit /b
:gotAdmin
pushd "%CD%" :gotAdmin
CD /D "%~dp0" cd /d "%~dp0"
:BackupHosts :BackupHosts
:: Backup default hosts file :: Backup the default hosts file
if not exist "%WINDIR%\System32\drivers\etc\hosts.skel" ( if not exist "%WINDIR%\System32\drivers\etc\hosts.skel" (
COPY %WINDIR%\System32\drivers\etc\hosts %WINDIR%\System32\drivers\etc\hosts.skel copy /v "%WINDIR%\System32\drivers\etc\hosts" "%WINDIR%\System32\drivers\etc\hosts.skel"
) )
:UpdateHosts :UpdateHosts
:: Update hosts file :: Update hosts file
python updateHostsFile.py --auto --minimise python updateHostsFile.py --auto --minimise
:: Move new hosts file in-place :: Copy over the new hosts file in-place
COPY hosts %WINDIR%\System32\drivers\etc\ copy /y /v hosts "%WINDIR%\System32\drivers\etc\"
:: Flush the DNS cache :: Flush the DNS cache
ipconfig /flushdns ipconfig /flushdns
:: Summary note :: Summary note
pause pause