Advertisement
npocmaka

startHiddenMshta.bat

Apr 17th, 2014
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Batch 2.56 KB | None | 0 0
  1. @echo off
  2. setlocal
  3.  
  4.    rem ::: check if help need to be called :::
  5.   rem :
  6.    
  7.    for /f "usebackq tokens=1,2,3,4,5,6,7 delims= " %%A in ('-h -help /help /h "" /? -?') do (
  8.       for %%# in (%%A %%B %%C %%D "%%~E" ) do if /i "%~1" equ "%%~#" goto :help
  9.      
  10.       if "%~1" equ "%%~F" goto :help
  11.       if "%~1" equ "%%~G" goto :help
  12.    )
  13.  
  14.    rem :
  15.   rem :::  end help checks                 :::
  16.  
  17.    
  18.    rem ::: arg parsing and checking         :::
  19.   rem :
  20.    set /A shifter=1
  21.    set "workdir=."
  22.    set "commandline= "
  23.    set "exec="
  24.    
  25.    
  26.    :arg_parser
  27.    
  28.    
  29.    if "%~1" equ "-exec" set "exec=%~2"
  30.    if "%~1" equ "-commandline" set "commandline=%~2"
  31.    if "%~1" equ "-workdir" set "workdir=%~2"
  32.    
  33.    shift & shift
  34.    set /A shifter=shifter + 1
  35.    
  36.    if %shifter% EQU 4 goto :end_arg_parser
  37.    if "%~1" EQU "" goto :end_arg_parser
  38.    goto :arg_parser
  39.    
  40.      
  41.    :end_arg_parser
  42.   rem :
  43.   rem ::: end of arg parsing               :::
  44.  
  45.    rem ::: validate arguments               :::
  46.   rem :
  47.    
  48.    set "exec_path="
  49.    if not defined exec echo executable not defined && exit /b 1
  50.    if exist "%exec%" for /f "usebackq" %%E in ("%exec%") do set "exec_path=%%~dpfsnxE"
  51.    if not defined exec_path for %%E in ("%exec%") do set "exec_path=%%~dpfsnx$PATH:E"
  52.    if not defined exec_path echo executable "%exec%" does not exist && exit /b 2
  53.    for %%W in ("%workdir%") do set "workdir=%%~dpW"
  54.    if not exist "%workdir%" echo target directory  "%workdir%" not exist && exit /b 3
  55.  
  56.    rem :
  57.   rem ::: end of validation                :::
  58.  
  59.    rem ::: construct mshta command          :::
  60.   rem :
  61.    set "vbs_w32process=vbscript:execute("set w32ps=GetObject(""winmgmts:"").Get(""Win32_ProcessStartup"").SpawnInstance_():w32ps.ShowWindow=0:"
  62.    set "vbs_stream_writer=CreateObject(""Scripting.FileSystemObject"").GetStandardStream(1).Write(""Rerurn code: ""&"
  63.    set "vbs_start_process=GetObject(""winmgmts:"").Get(""Win32_Process"").Create(""%exec_path% %commandline%"",""%workdir%"",w32ps,pid)&vbCrLf&""PID: ""&pid)"
  64.    set "vbs_end=:Close")"
  65.  
  66.    rem :
  67.   rem ::: end construct mshta command      :::
  68.  
  69.    rem ::: call mshta                       :::
  70.   rem :
  71.    mshta %vbs_w32process%%vbs_stream_writer%%vbs_start_process%%vbs_end%| findstr "^"
  72.   rem :
  73.   rem ::: end  mshta call                   :::
  74.  
  75.  
  76. endlocal
  77. exit /b 0
  78.  
  79. :help
  80. echo %~nx0 starts a background/invisible process and return its PID
  81. echo %~nx0 -exec executable [-commandline commandLine] [-workdir workdir]
  82. echo by Vasil "npocmaka" Arnaudov
  83. exit /b 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement