Advertisement
npocmaka

StartPID

Jan 22nd, 2013
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. @echo off
  2. setlocal enabledelayedexpansion
  3.  
  4. if {%~1} NEQ {} (
  5.     if {%~1} NEQ {-help} (
  6.         call :proc %*
  7.         exit /b %ERRORLEVEL%
  8.     )
  9. )
  10.  
  11. call :echoHelp
  12. exit /b
  13.  
  14. :proc
  15.     call :argParser %*
  16.  
  17.     if {%exec%} EQU {} (
  18.         call :err 1000
  19.         exit /b %ERRORLEVEL%
  20.     )
  21.  
  22.     if {%host%} NEQ {} (
  23.         set host=/NODE:%host%
  24.         if {%user%} NEQ {} (
  25.             set user=/USER:%user%
  26.             if {%pass%} NEQ {} (
  27.                 set pass=/PASSWORD:%pass%
  28.             )
  29.         )
  30.     )
  31.  
  32.     if {%record%} NEQ {} (
  33.         set record=/RECORD:%record%
  34.     )
  35.  
  36.     set global_params=%record% %host% %user% %pass%
  37.  
  38.     for /f "usebackq tokens=*" %%G IN (`wmic  %global_params%  process call create "%exec% %commandline%"^,"%workdir%"`) do (
  39.         rem echo %%G
  40.         echo %%G | find "ProcessId" > nul && (
  41.             for /f  "tokens=2 delims=;= " %%H in ('echo %%G') do (
  42.                 call set /A PID=%%H
  43.             )
  44.         )
  45.         echo %%G | find "ReturnValue" > nul && (
  46.             for /f  "tokens=2 delims=;= " %%I in ('echo %%G') do (
  47.                 call set /A RETCOD=%%I
  48.             )
  49.         )
  50.         call :concat "%%G" 
  51.     )
  52.  
  53.     rem successful execution
  54.     if %PID% NEQ 0 (
  55.         echo %PID%
  56.         exit /b
  57.         rem exit /B %PID%
  58.     ) else (
  59.         call :err %RETCOD%
  60.     )
  61. exit /b %ERRORLEVEL%
  62.  
  63.  
  64. :argParser
  65.     set comstr=-exec-commandline-workdir-host-user-pass-record
  66.     for /L %%i in (1,1,7) do (
  67.         echo %comstr% | find "%~1" > nul && (
  68.             set _tmp=%~1
  69.             set !_tmp:-=!=%~2
  70.         )
  71.         shift
  72.         shift
  73.     )
  74.     set _tmp=
  75. exit /b
  76.  
  77. :echoHelp
  78.         echo creates a process and returns it's PID
  79.     echo %~n0 -exec executubale {-commandline command_line} { -workdir working_directory}
  80.     echo  {-host  remote_host {-user user {-pass password}}} {-record path_to_xml_output}
  81.     echo\
  82.     echo localhost cant' be used as in -host variable
  83.     echo Examples:
  84.     echo %~n0  -exec "notepad" -workdir "c:/"  -record "test.xml" -commandline "/A startpid.txt"
  85.     echo %~n0  -exec "cmd" -workdir "c:/"  -record "test.xml" -host remoteHost -user User
  86.         echo by Vasil "npocmaka" Arnaudov
  87. exit /b
  88.  
  89. :concat
  90.  
  91.     call set output=%output% ^& echo %~1
  92. exit /b
  93.  
  94. :err
  95.     if %1 EQU 2          (set errmsg=Access Denied)
  96.     if %1 EQU 3          (set errmsg=Insufficient Privilege)
  97.     if %1 EQU 8          (set errmsg=Unknown failure ^& echo Hint: Check if the executable and workdit exists or if command line parameters are correct.)
  98.     if %1 EQU 9          (set errmsg=Path Not Found ^& echo Hint: check if  the work exists on the remote machine.)
  99.     if %1 EQU 21         (set errmsg=Invalid Parameter ^& echo Hint: Check executable path.Check if  host and user are corect.)
  100.     if %1 EQU 1000       (set errmsg=Executable not defined.)
  101.     if {%errmsg%} EQU {} (set errmsg=%output% ^& echo Hint: brackets,quotes or commas in the password may could breack the script.)
  102.  
  103.     echo %errmsg%
  104. exit /b %1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement