Created
May 2, 2013 23:06
-
-
Save davidruhmann/5506140 to your computer and use it in GitHub Desktop.
Update HOSTS file for Virtual Domains
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
:: Hide Commands | |
@echo off | |
:: Setup User Interface | |
title Virtual Domain HOSTS Update | |
setlocal EnableExtensions | |
pushd "%~dp0" | |
:: Verify Administrator Access | |
call :IsAdmin 1 %* || exit /b 1 | |
:: Define the HOSTS file location | |
set "HOSTS=%SystemRoot%\System32\Drivers\Etc\HOSTS" | |
if not exist "%HOSTS%" ( | |
echo ERROR: HOSTS file could not be found. Process Aborted. | |
pause >nul | |
goto End | |
) | |
:: Backup HOSTS | |
echo Creating Backup... | |
echo %Temp%\HOSTS.bak | |
echo F | xcopy /q /h /y "%HOSTS%" "%Temp%\HOSTS.bak" | |
if not exist "%Temp%\HOSTS.bak" ( | |
echo ERROR: Unable to backup HOSTS file. Process Aborted. | |
pause >nul | |
goto End | |
) | |
:MainMenu | |
echo. | |
:: Domain selection | |
echo Virtual Domains: | |
echo 1. SolMastr | |
echo 2. 1201DepM | |
echo 3. AblPrep | |
::echo 4. [Other] | |
echo. | |
:Domain | |
set /p "Input= > Please select your domain: " | |
if defined Input set "Input=%Input:"=%" | |
:: Process selection | |
set "Domain=" | |
if /i "%Input%"=="1" set "Domain=solmastr" | |
if /i "%Input%"=="solmastr" set "Domain=solmastr" | |
if /i "%Input%"=="2" set "Domain=1201depm" | |
if /i "%Input%"=="1201depm" set "Domain=1201depm" | |
if /i "%Input%"=="3" set "Domain=ablprep" | |
if /i "%Input%"=="ablprep" set "Domain=ablprep" | |
::if /i "%Input%"=="4" set "Domain=other" | |
::if /i "%Input%"=="Other" set "Domain=other" | |
::if /i "%Input%"=="[Other]" set "Domain=other" | |
if not defined Domain goto Domain | |
:ShowEntries | |
echo. | |
:: Show Current Entries | |
echo Current %Domain% HOSTS Entries: | |
for /f "skip=2 tokens=1,* delims=]" %%A in ('find /n /i "%Domain%" "%HOSTS%"') do echo %%B | |
for /f "tokens=1,*" %%A in ('find /c /i "%Domain%" "%HOSTS%"') do set "Info=%%B" | |
set "Info=%Info::=&echo(%" | |
for /f "tokens=*" %%A in ('"echo(%Info%"') do set "Count=%%A" | |
set /a "Count=Count" | |
echo Total = %Count% | |
:: Options selection | |
echo. | |
echo Entry Actions: | |
echo 1. Remove | |
echo 2. Update or Add ^(IP Address^) | |
echo 3. Comment Toggle ^(Disable or Enable^) | |
echo 7. Select Domain | |
echo 9. Exit | |
:Action | |
echo. | |
set /p "Input= > Please select an action: " | |
if defined Input set "Input=%Input:"=%" | |
:: Process selection | |
set "Action=" | |
if /i "%Input%"=="1" set "Action=remove" | |
if /i "%Input%"=="remove" set "Action=remove" | |
if /i "%Input%"=="2" set "Action=update" | |
if /i "%Input%"=="update" set "Action=update" | |
if /i "%Input%"=="3" set "Action=comment" | |
if /i "%Input%"=="comment" set "Action=comment" | |
if /i "%Input%"=="7" goto MainMenu | |
if /i "%Input%"=="select" goto MainMenu | |
if /i "%Input%"=="9" goto End | |
if /i "%Input%"=="exit" goto End | |
if not defined Action goto Action | |
:: Perform action | |
echo. | |
if /i "%Action%"=="remove" ( | |
if "%Count%"=="0" echo NOTE: There are no entries to remove. & goto Action | |
set /p "=" <nul >%HOSTS% | |
for /f "skip=2 tokens=1,* delims=]" %%A in ('find /v /n /i "%Domain%" "%Temp%\HOSTS.bak"') do echo(%%B>>"%HOSTS%" | |
) | |
:IP | |
set "IP=" | |
if /i "%Action%"=="update" set /p "IP= > Enter the new IP Address: " | |
if defined IP set "IP=%IP:"=%" | |
if /i "%Action%"=="update" ( | |
if not defined IP goto IP | |
if not "%Count%"=="0" ( | |
set /p "=" <nul >%HOSTS% | |
for /f "skip=2 tokens=1,* delims=]" %%A in ('find /v /n /i "%Domain%" "%Temp%\HOSTS.bak"') do echo(%%B>>"%HOSTS%" | |
) | |
echo(%IP% %Domain% #%Domain%>>"%HOSTS%" | |
echo(%IP% ip%Domain% #%Domain%>>"%HOSTS%" | |
echo(%IP% %Domain%.northamerica.cerner.net #%Domain%>>"%HOSTS%" | |
echo(%IP% ip%Domain%.northamerica.cerner.net #%Domain%>>"%HOSTS%" | |
) | |
if /i "%Action%"=="comment" ( | |
if "%Count%"=="0" echo NOTE: There are no entries to comment. & goto Action | |
set /p "=" <nul >%HOSTS% | |
for /f "skip=2 tokens=1,* delims=]" %%A in ('find /v /n /i "%Domain%" "%Temp%\HOSTS.bak"') do echo(%%B>>"%HOSTS%" | |
for /f "skip=2 tokens=1,* delims=]" %%A in ('find /n /i "%Domain%" "%Temp%\HOSTS.bak"') do ( | |
set "Entry=%%B" | |
call :ToggleEntry | |
) | |
) | |
if not exist "%HOSTS%" ( | |
echo ERROR: HOSTS failed to modify. Attempting to roll back changes... | |
echo on && type "%Temp%\HOSTS.bak">>"%HOSTS%" | |
) | |
:: Done | |
echo. | |
echo Done | |
goto ShowEntries | |
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: | |
:RemoveHOSTS {HOSTS} {Domain} | |
setlocal EnableExtensions | |
:: Empty the HOSTS file copy everything back but the domain entries. | |
set /p "=" <nul >%HOSTS% | |
for /f "skip=2 tokens=1,* delims=]" %%A in ('find /v /n /i "%Domain%" "%Temp%\HOSTS.bak"') do echo(%%B>>"%HOSTS%" | |
endlocal & (exit /b 0 ) | |
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: | |
:ToggleEntry {Entry} | |
setlocal EnableExtensions | |
:: Add or Remove the comment character from infront of the set entry. | |
if "%Entry:~0,1%"=="#" ( echo(%Entry:~1%>>"%HOSTS%" ) else ( echo(#%Entry%>>"%HOSTS%" ) | |
endlocal & (exit /b 0 ) | |
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: | |
:IsAdminVerbose [Elevate|Params] | |
setlocal EnableExtensions | |
call :IsAdmin %* | |
set "Code=%ErrorLevel%" | |
if "%Code%" equ "0" echo Administrator: True | |
if "%Code%" equ "1" echo Administrator: False | |
endlocal & ( exit /b %Code% ) | |
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: | |
:IsAdmin [Elevate|Params] | |
:: Check for Administrator privileges and request privileges if Elevate is set. | |
:: NOTE Elevate causes the batch script to restart and forget the arguments. | |
:: NOTE "runas" is an undocumented verb for the ShellExecute function. | |
:: http://msdn.microsoft.com/en-us/library/windows/desktop/bb762153.aspx | |
:: http://msdn.microsoft.com/en-us/library/windows/desktop/gg537745.aspx | |
:: ErrorLevel 0 = Admin, 1 = Not Admin | |
setlocal EnableExtensions | |
:: Check for Access | |
::net session >nul 2>&1 | |
"%SystemRoot%\system32\cacls.exe" "%SystemRoot%\system32\config\system" >nul 2>&1 | |
if %ErrorLevel% EQU 0 ( exit /b 0 ) | |
:: Check Preference | |
set "Elevate=%~1" | |
if not defined Elevate ( exit /b 1 ) | |
:: Eleveate Privileges | |
call :ElevateVB %* || call :ElevatePS %* | |
endlocal & ( exit /b 1 ) | |
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: | |
:ElevateVB [Params] | |
:: ErrorLevel 0 = Success, 1 = File Error, 2 = Elevate Error | |
setlocal EnableExtensions | |
:: Generate VBScript | |
set "VB=%Temp%\Admin.vbs" | |
echo Set UAC = CreateObject^("Shell.Application"^) > "%VB%" | |
echo UAC.ShellExecute "%~s0", "%*", "", "runas", 1 >> "%VB%" | |
:: Execute VBScript | |
if not exist "%VB%" ( exit /b 1 ) | |
"%VB%" | |
endlocal & ( exit /b %ErrorLevel% ) | |
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: | |
:ElevatePS [Params] | |
:: ErrorLevel 0 = Success | |
setlocal EnableExtensions | |
:: Get Command Line and Escape Quotations | |
set "Args=%~s0 %*" | |
set "Args=%Args:"=\"%" | |
:: Execute PowerShell Command | |
PowerShell -Command (New-Object -com 'Shell.Application').ShellExecute('cmd.exe', '/k %Args%', '', 'runas') | |
endlocal & ( exit /b %ErrorLevel% ) | |
:End | |
popd | |
endlocal | |
title %~dp0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment