forked from agiledivider/vagrant-hostsupdater
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Windows cmd scripts to change hosts permissions
Two windows cmd scripts that can work together to change permissions for the current user to modify the hosts file. win-hosts-check.cmd check if the permissions on the file are sufficient. If not it will call win-hosts-set.cmd which will trigger the windows permission dialog asking for enough rights to midify the permissions on the hosts file for the current user. See issue agiledivider#40
- Loading branch information
Showing
2 changed files
with
59 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
@ECHO off | ||
SETLOCAL ENABLEEXTENSIONS | ||
REM modify hosts access rights current user can change the file contents | ||
|
||
call :hostfileperms _perms | ||
echo Permissins on hosts for user %USERNAME%: %_perms% | ||
|
||
If "%_perms%" == "F" goto :PERM_OK | ||
If "%_perms%" == "M" goto :PERM_OK | ||
If "%_perms%" == "RX,W" goto :PERM_OK | ||
If "%_perms%" == "R,W" goto :PERM_OK | ||
If "%_perms%" == "W" goto :PERM_OK | ||
|
||
If "%_perms%" == ":" goto :PERM_INSUFFICIENT | ||
If "%_perms%" == "R" goto :PERM_INSUFFICIENT | ||
If "%_perms%" == "RX" goto :PERM_INSUFFICIENT | ||
|
||
echo Unhandled "%_perms%", raising. | ||
goto :PERM_INSUFFICIENT | ||
|
||
:PERM_INSUFFICIENT | ||
echo|set /p=Raising permissions... | ||
call win-hosts-set.cmd | ||
echo|set /p=done. | ||
call :hostfileperms _perms_new | ||
echo New permissins are: %_perms_new% | ||
IF "%_perms%" == "%_perms_new%" ( | ||
echo Changing permissions failed. Exiting. | ||
exit /b 1 | ||
) | ||
|
||
:PERM_OK | ||
echo Suffice. Exiting. | ||
goto :EOF | ||
|
||
:hostfileperms | ||
setlocal | ||
set _icacls_cmd=Icacls %SystemRoot%\system32\drivers\etc\hosts /T | ||
FOR /f "tokens=2" %%G IN ('%_icacls_cmd% ^|find "%USERNAME%"') DO set _permission_info=%%G | ||
set "_permission_perms=%_permission_info:*:=%" | ||
set _permission=%_permission_perms:~1,-1% | ||
|
||
( endlocal | ||
set "%1=%_permission%" | ||
) | ||
exit /b |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
@if (1==1) @if(1==0) @ELSE | ||
@echo off&SETLOCAL ENABLEEXTENSIONS | ||
>nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system"||( | ||
cscript //E:JScript //nologo "%~f0" | ||
@goto :EOF | ||
) | ||
CACLS %SystemRoot%\system32\drivers\etc\hosts /E /G %USERNAME%:W | ||
@goto :EOF | ||
@end @ELSE | ||
ShA=new ActiveXObject("Shell.Application") | ||
ShA.ShellExecute("cmd.exe","/c \""+WScript.ScriptFullName+"\"","","runas",5); | ||
WScript.Sleep(500) | ||
@end |