Advertisement
npocmaka

StartAs

Jan 22nd, 2013
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. @echo off
  2. rem -- ver.02 --
  3. setlocal
  4.  
  5. if [%1] EQU [] (
  6.     echo %~n0 -exec path_to_exec  [-user user [-pass pass]] [-command commandline] [-system] remote_system
  7.     echo starts a process as a certain user
  8.     echo to start a hidden process set user to SYSTEM
  9.     echo by Vasil "npocmaka" Arnaudov
  10.     goto :eof
  11. )
  12.  
  13. if [%1] EQU [-help] (
  14.     echo %~n0 -exec path_to_exec  [-user user [-pass pass]] [-command commandline] [-system] remote_system
  15.     echo starts a process as a certain user
  16.     echo to start a hidden process set user to SYSTEM
  17.     echo by Vasil "npocmaka" Arnaudov
  18.     goto :eof
  19. )
  20.  
  21. set /A shifter=1
  22. ::set spaces to params that is possible to stay unused
  23. set set user_param=
  24. set pass_param=
  25. set exec=
  26. set command=
  27. ::they're same for SCHTASKS and EVENTCREATE
  28. set remote_params=
  29.  
  30.  
  31. :argParser
  32.     if [%1] EQU [-exec] (
  33.         set executable=%~s2
  34.         set inpath=%~dp$PATH:2
  35.     )
  36.     if [%1] EQU [-user] (
  37.         set user=%2
  38.     )
  39.  
  40.     if [%1] EQU [-pass] (
  41.         set pass=%2
  42.     )
  43.  
  44.     if [%1] EQU [-command] (
  45.         set command='%~2'
  46.     )
  47.    
  48.     if [%1] EQU [-system] (
  49.         set system=%2
  50.     )
  51.    
  52.     shift
  53.     shift
  54.     set /A shifter=%shifter% + 1
  55.    
  56.     if %shifter% EQU 5 (
  57.         goto :endArgParser
  58.     )
  59.  
  60. goto :argParser
  61. :endArgParser
  62.  
  63.  
  64. :: parameters with user and no pass is ommited to avoid pass prompting
  65.  
  66. if "%user%" NEQ "" (
  67.     set user_param=/RU %user%
  68.    
  69.     if "%pass%" NEQ "" (
  70.         set pass_param=/RP %pass%
  71.        
  72.         if "%system%" NEQ "" (
  73.             set remote_params= /S %system% /U %user% /P %pass%
  74.             set pass_param=
  75.             set user_param=
  76.             goto :skipExCheck
  77.         )
  78.     ) else (
  79.         set user_param=
  80.     )
  81.    
  82. ) else (
  83.     if "%system%" NEQ "" (
  84.         set remote_params= /S %system%
  85.         goto :skipExCheck
  86.     )
  87.    
  88. )
  89.  
  90.  
  91. ::there cannot be set password on system user
  92. if "%user%" EQU "SYSTEM" (
  93.     set pass_param=
  94. )
  95.  
  96. if "%system%" NEQ "" (
  97.      set remote_params= /S %system% /U %user% /P %pass%
  98.      goto :skipExCheck
  99. )
  100.  
  101. ::the idea behind this ugly if is
  102. :: to get the path in 8.3 format because there are problems between
  103. :: SCHTASKS and spaces
  104.  
  105.  
  106.  
  107. if not exist %executable% (
  108.     if "%inpath%" equ "" (
  109.         echo path to executable item does not exist
  110.         exit /B 1        
  111.     ) else (
  112.           for %%f in ("%inpath%\%executable%") do set executable=%%~snf
  113.     )
  114. ) else (
  115.     for %%f in ("%executable%") do set executable=%%~snf
  116. )
  117.  
  118. :: if task is executed against remote machine existence checking is unnecessary
  119. :skipExCheck
  120.  
  121.  
  122.  
  123. ::getting the current time
  124. FOR /F "skip=1 tokens=1-3" %%A IN ('WMIC Path Win32_LocalTime Get Hour^,Minute^,Second /Format:table') DO (
  125.     SET /A hours=%%A > nul 2>&1
  126.     SET /A minutes=%%B > nul 2>&1
  127.     SET /A seconds=%%C > nul 2>&1    
  128. )
  129. set /a time_stamp=%hours%%minutes%%seconds%
  130.  
  131. if "%time_stamp%" equ "" (
  132.     echo "some kind of error"
  133.     exit /B 2
  134. )
  135.  
  136. ::the heart of the script
  137. SCHTASKS /Create /SC ONEVENT /EC Application /MO *[System/EventID=101] /TN start_at_%time_stamp%  /TR "'%executable%' %command%" %user_param% %pass_param%  /F /RL HIGHEST %remote_params%
  138. EVENTCREATE /ID 101 /T INFORMATION /D start_task %remote_params%
  139. SCHTASKS /Delete /TN start_at_%time_stamp% /f %remote_params%
  140. endlocal
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement