Thursday, March 16, 2017

Elevated batch processing of two different sets of code

This is a small task I gave to myself because I was annoyed by constantly having to change the IP for my college and then at home. To you, who might find it in light for a different purpose, ahoy you are in the right place. I had been using these two batch files, my batch-files version 1.1 and 1.2, for some time but I felt that it just cluttered my desktop so, to avoid it, I created a single batch processing file that could perform the tasks of those two. Ofcourse to tackle this task, the batch file had to be aware of its state during execution. 


This was solved using a boolean at the top of the file. The file reads its own instance as it executes - yes batch files are awesome like that, and the boolean state is determined to know which block of script is to be executed. Hence, batch-file version 2.0 was born. This was the easy part. The hard part comes with elevated access.

My batch-file version 2.0 was working fine and dandy but the real issue was that it required me to right click and 'run as Administrator' every single time. I couldn't have that - not with my plan to make this into a handy little shortcut by the end. So for a lack of a better word, I borrowed some code from a fellow SO user. It's called Elevate and I've referenced the 4th version for this. 

Version History:
Version 1.1 : LabConfiguration.bat

@echo off
set varip=10.0.80.40
REM WRITE YOUR COMPUTER IP ABOVE
set vardefaultgway=10.0.80.1
set varsubnetmask=255.255.255.192
netsh interface ip set address name="Ethernet" static %varip% %varsubnetmask% %vardefaultgway%


Version 1.2 : HomeConfiguration.bat

@echo off
netsh interface ipv4 set address name="Ethernet" source=dhcp


Version 2.0 : ToggleConfiguration.bat

@echo off
REM LABA
REM ----------PRESET SETTINGS-------
set varip=10.0.80.40
REM WRITE YOUR COMPUTER IP ABOVE
set vardefaultgway=10.0.80.1
set varsubnetmask=255.255.255.192
REM --------------------------------
for /F "usebackq skip=1 tokens=2" %%a in ("%~F0") do set "BOOLEAN_VALUE=%%a" & goto continue
:continue
ECHO BOOLEAN_VALUE=%BOOLEAN_VALUE%
IF %BOOLEAN_VALUE% EQU GHAR GOTO 2
:1
netsh interface ip set address name="Ethernet" static %varip% %varsubnetmask% %vardefaultgway%
GOTO UPDATE_BOOLEAN_VALUE
:2
netsh interface ipv4 set address name="Ethernet" source=dhcp
:UPDATE_BOOLEAN_VALUE
REM The code to change the BOOLEAN_VALUE i.e. a toggler
if %BOOLEAN_VALUE% equ GHAR (set "BOOLEAN_VALUE=LABA") else set "BOOLEAN_VALUE=GHAR"
(
echo @echo off
echo REM %BOOLEAN_VALUE%
for /F "usebackq skip=2 delims=" %%a in ("%~F0") do echo %%a
) > _new_.bat
move /Y _new_.bat "%~F0" > NUL
if %BOOLEAN_VALUE% equ GHAR (set "BOOLEAN_VALUE=LABA") else set "BOOLEAN_VALUE=GHAR"
echo Setting applied for:: %BOOLEAN_VALUE%
pause
exit


Version 3.0 : IPautosetter.bat

@echo off
REM LABA
 CLS
 ECHO.
 ECHO =============================
 ECHO LAB IP CONFIGURATOR
 ECHO =============================
REM ----------PRESET SETTINGS-------
set varip=10.0.80.40
REM WRITE YOUR COMPUTER IP ABOVE
set vardefaultgway=10.0.80.1
set varsubnetmask=255.255.255.192
REM --------------------------------
:init
 setlocal DisableDelayedExpansion
 set cmdInvoke=1
 set winSysFolder=System32
 set "batchPath=%~0"
 for %%k in (%0) do set batchName=%%~nk
 set "vbsGetPrivileges=%temp%\OEgetPriv_%batchName%.vbs"
 setlocal EnableDelayedExpansion
:checkPrivileges
  NET FILE 1>NUL 2>NUL
  if '%errorlevel%' == '0' ( goto gotPrivileges ) else ( goto getPrivileges )
:getPrivileges
  if '%1'=='ELEV' (echo ELEV & shift /1 & goto gotPrivileges)
  ECHO.
  ECHO Set UAC = CreateObject^("Shell.Application"^) > "%vbsGetPrivileges%"
  ECHO args = "ELEV " >> "%vbsGetPrivileges%"
  ECHO For Each strArg in WScript.Arguments >> "%vbsGetPrivileges%"
  ECHO args = args ^& strArg ^& " "  >> "%vbsGetPrivileges%"
  ECHO Next >> "%vbsGetPrivileges%"
  if '%cmdInvoke%'=='1' goto InvokeCmd 
  ECHO UAC.ShellExecute "!batchPath!", args, "", "runas", 1 >> "%vbsGetPrivileges%"
  goto ExecElevation
:InvokeCmd
  ECHO args = "/c """ + "!batchPath!" + """ " + args >> "%vbsGetPrivileges%"
  ECHO UAC.ShellExecute "%SystemRoot%\%winSysFolder%\cmd.exe", args, "", "runas", 1 >> "%vbsGetPrivileges%"
:ExecElevation
 "%SystemRoot%\%winSysFolder%\WScript.exe" "%vbsGetPrivileges%" %*
 exit /B
:gotPrivileges
 setlocal & cd /d %~dp0
 if '%1'=='ELEV' (del "%vbsGetPrivileges%" 1>nul 2>nul  &  shift /1)
 ::::::::::::::::::::::::::::
 ::START
 ::::::::::::::::::::::::::::
 REM Run shell as admin (example)
@echo off
REM LABA
for /F "usebackq skip=1 tokens=2" %%a in ("%~F0") do set "BOOLEAN_VALUE=%%a" & goto continue
:continue
ECHO BOOLEAN_VALUE=%BOOLEAN_VALUE%
IF %BOOLEAN_VALUE% EQU GHAR GOTO 2
:1
netsh interface ip set address name="Ethernet" static %varip% %varsubnetmask% %vardefaultgway%
GOTO UPDATE_BOOLEAN_VALUE
:2
netsh interface ipv4 set address name="Ethernet" source=dhcp
:UPDATE_BOOLEAN_VALUE
REM The code to change the BOOLEAN_VALUE i.e. a toggler
if %BOOLEAN_VALUE% equ GHAR (set "BOOLEAN_VALUE=LABA") else set "BOOLEAN_VALUE=GHAR"
(
echo @echo off
echo REM %BOOLEAN_VALUE%
for /F "usebackq skip=2 delims=" %%a in ("%~F0") do echo %%a
) > _new_.bat
move /Y _new_.bat "%~F0" > NUL
if %BOOLEAN_VALUE% equ GHAR (set "BOOLEAN_VALUE=LABA") else set "BOOLEAN_VALUE=GHAR"
echo Setting applied for:: %BOOLEAN_VALUE%
pause
exit
REM ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
REM :: built with Elevate.cmd - Version 4
REM :: Automatically check & get admin rights
REM :: referenced from: http://stackoverflow.com/a/12264592/5040900
REM :: modified by Siddhant-Rimal 03-16-2017
REM ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::


Known Issues:
1.  "The filename,directory name, or volume label syntax is incorrect. System cannot find the file specified". Don't worry, its just a case of referencing error on first-run. Please close the batch file and run again, it should not show the error again. Its a self-healing code and creates correct references on the second run.
2. The batch file was working fine but it stopped running suddenly! - Yes, this is also a known issue. Its not actually the Batch-file failing to run, its the side-effect of the said Deadpool-like healing-factor from point#1. The batch file hard-codes the path where it is executed into its own code when it is executed the first time. If you notice the code, there is a certain reference command !batchPath! . This is overwritten on the first pass(i.e. first execution of the batch file). Since the batch file writes itself, it is unavoidable. It is advisable to keep a copy of version3.0 in a text file and use the other copy as a batch-file.

0 comments

Post a Comment