Advertisement
npocmaka

startPID.2.0

May 17th, 2013
451
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. @echo off
  2. setlocal
  3.  
  4. if [%~1] EQU [] (
  5.         call :echoHelp
  6.         goto :eof
  7. )
  8.  
  9. if [%~1] EQU [-help] (
  10.         call :echoHelp
  11.         goto :eof
  12. )
  13.  
  14. set /A shifter=1
  15. set workdir=.
  16.  
  17.  
  18. :argParser
  19.         if [%~1] EQU [-exec] (
  20.                 set exec=%~2
  21.         )
  22.         if [%~1] EQU [-commandline] (
  23.                 set commandline=%~2
  24.         )
  25.         if [%~1] EQU [-workdir] (
  26.                 set workdir=%~2
  27.         )
  28.         if [%~1] EQU [-host] (
  29.                 set host=%~2
  30.         )
  31.         if [%~1] EQU [-user] (
  32.                 set user=%~2
  33.         )
  34.         if [%~1] EQU [-pass] (
  35.                 set pass=%~2
  36.         )
  37.         if [%~1] EQU [-record] (
  38.                 set record=%~2
  39.         )
  40.         shift
  41.         shift
  42.         set /A shifter=%shifter% + 1
  43.        
  44.         if %shifter% EQU 7 (
  45.                
  46.                 goto :endArgParser
  47.         )
  48.  
  49. goto :argParser
  50. :endArgParser
  51.  
  52. if "%exec%" EQU "" (
  53.         echo executable not defined
  54.         goto :eof
  55. )
  56.  
  57. if "%host%" NEQ "" (
  58.         set host_param=/NODE:%host%
  59.         if [%user%] NEQ [] (
  60.                 set user_param=/USER:%user%
  61.                 if [%pass%] NEQ [] (
  62.                         set pass_param=/PASSWORD:%pass%
  63.                 )
  64.         )
  65. )
  66.  
  67. if [%record%] NEQ [] (
  68.         set record_param=/RECORD:%record%
  69. )
  70.  
  71. set global_params=%record_param% %host_param% %user_param% %pass_param%
  72.  
  73.  call echo wmic  %global_params%  process call create "%exec% %commandline%"^,"%workdir%"
  74.  
  75. for /f "usebackq tokens=*" %%G IN (`wmic  %global_params%  process call create "%exec% %commandline%"^,"%workdir%"`)  do (
  76.         echo %%G | find "ProcessId" >nul && (
  77.                 for /f  "tokens=2 delims=;= " %%H in ('echo %%G ^| find  "ProcessId"') do (
  78.                         call set /A PID=%%H
  79.                         goto :endLoop1
  80.                 )
  81.         )
  82.        
  83.         echo %%G | find "ReturnValue" >nul && (
  84.                 for /f  "tokens=2 delims=;= " %%I in ('echo %%G ^| find  "ReturnValue"') do (
  85.                         call set /A RETCOD=%%I
  86.                         goto :endLoop2
  87.                 )
  88.         )
  89.        
  90.         call :concat "%%G"    
  91. )
  92. goto :endloop3
  93.  
  94. ::successful execution
  95. :endLoop1
  96. if %PID% NEQ 0 (
  97.         echo %PID%
  98.         exit /B %PID%
  99. )
  100.  
  101. ::unsuccessful with code
  102. ::check the return code and give hints
  103. :endLoop2
  104.  
  105. echo return code : %RETCOD%
  106.  
  107. if %RETCOD% EQU 2 (
  108.         echo -Access Denied
  109. )
  110.  
  111. if %RETCOD% EQU 3 (
  112.         echo -Insufficient Privilege
  113. )
  114.  
  115. if %RETCOD% EQU 8 (
  116.         echo -Unknown failure  
  117.         echo Hint: Check if the executable and workdit exists or if command line parameters are correct.
  118. )
  119.  
  120. if %RETCOD% EQU 9 (
  121.         echo -Path Not Found
  122.         echo Hint: check if  the work exists on the remote machine.
  123. )
  124.  
  125. if %RETCOD% EQU 21 (
  126.         echo -Invalid Parameter
  127.         echo Hint: Check executable path.Check if  host and user are corect.
  128. )
  129.  
  130. exit /b 0
  131. goto :eof
  132.  
  133. ::unsuccessful with no code
  134. :endloop3
  135. echo %output%
  136. echo HINT :brackets,quotes or commas in the password  could break the script
  137. goto :eof
  138.  
  139. endlocal
  140. goto :eof
  141.  
  142.  
  143. :echoHelp
  144.         echo %~n0 -exec executubale [-commandline command_line] [ -workdir working_directory] [-host  remote_host [-user user [-pass password]]] [-record path_to_xml_output]
  145.         echo\
  146.         echo localhost cant' be used as in -host variable
  147.        echo Examples:
  148.        echo %~n0  -exec "notepad" -workdir "c:/"  -record "test.xml" -commandline "/A startpid.txt"
  149.        echo %~n0  -exec "cmd" -workdir "c:/"  -record "test.xml" -host remoteHost -user User
  150.        echo\
  151.        echo by Vasil "npocmaka" Arnaudov
  152.  
  153. goto :eof
  154.  
  155. :concat
  156.        call set output=%output% %1
  157.  
  158. goto :eof
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement