2016-05-23 62 views
0

如果我在批处理文件中使用命令行启动新进程。 我怎样才能得到新创建的进程ID。通过命令行获取新创建的进程ID

//在批处理文件

C:\用户\ PRASHANT>记事本

在这里,我已经打开了新的记事本程序,我怎样才能得到一个记事本进程id

回答

0

你可以启动预先批处理文件,不断将任务列表转储到文本文件中,仅检索第一列(内存大小可能不断变化)并将其写入tasks_now.txt

FOR /F "tokens=1,2 delims=, skip=1" %%f IN ('tasklist /FO:CSV') DO (echo %%f %%g >> tasks_now.txt) 

然后你就可以通过之前使用文件比较命令比较tasks_now.txt到任务列表:

fc tasks_now.txt tasks_earlier.txt > diff.txt 

如果%errorlevel%是0,那么没有差异发生,并没有创建新的进程。现在你复制_now以备下一轮使用。

否则,您可以在diff.txt上运行FIND以查找新进程。如果之前已经存在一个同名的任务,它可能需要检查tasks_earlier.txt,如果它已经存在(再次查找) - 如果没有,那么你有新的任务。

+0

脆。这不会区分从批处理脚本启动并由用户或系统启动的进程。采样将错过在任意选择的采样点之间发生的任何事情。 – IInspectable

0

您将需要wmic来创建进程并在一次操作中获取其进程ID。

的基本原理是:

for /f "usebackq tokens=2 delims=;= " %%A IN (`wmic process call create '"notepad","%cd%",null' ^|find "ProcessId"`) do set /A PID=%%A 

notepad是命令行来运行,并且%cd%是工作目录(在这种情况下,电流的一个)。

之后,您应该拥有%PID%中的PID。

但是,对于更复杂的命令行和参数有一些引用问题。还有一个更复杂的,但强大的脚本正是这样做的,我在这里找到:http://ss64.org/viewtopic.php?pid=5876#p5876

的代码,学分用户smerch上ss64.org:

@echo off 
setlocal enabledelayedexpansion 

if "%~1" NEQ "" (
    if "%~1" NEQ "-help" (
     call :proc %* 
     exit /b %ERRORLEVEL% 
) 
) 

call :echoHelp 
exit /b 

:proc 
    call :argParser %* 

    if "%exec%" EQU "" (
     call :err 1000 
     exit /b %ERRORLEVEL% 
) 

    if "%host%" NEQ "" (
     set host=/NODE:%host% 
     if "%user%" NEQ "" (
      set user=/USER:%user% 
      if "%pass%" NEQ "" (
       set pass=/PASSWORD:%pass% 
     ) 
    ) 
) 

    if "%record%" NEQ "" (
     set record=/RECORD:%record% 
) 

    set global_params=%record% %host% %user% %pass% 

    for /f "usebackq tokens=*" %%G IN (`wmic %global_params% process call create "%exec% %commandline%"^,"%workdir%"`) do ( 
     rem echo %%G 
     set _tmp=%%G 
     set _tmp=!_tmp:^>=^^^>! 
     echo !_tmp! | find "ProcessId" > nul && (
      for /f "tokens=2 delims=;= " %%H in ('echo !_tmp!') do (
       call set /A PID=%%H 
     ) 
    ) 
     echo !_tmp! | find "ReturnValue" > nul && (
      for /f "tokens=2 delims=;= " %%I in ('echo !_tmp!') do (
       call set /A RETCOD=%%I 
     ) 
    ) 
     call :concat 
) 
    set _tmp= 

    rem successful execution 
    if "%PID%" NEQ "" (
     echo %PID% 
     exit /b 
     rem exit /B %PID% 
) else (
     call :err %RETCOD% 
) 
exit /b %ERRORLEVEL% 


:concat 

    call set output=%output% ^& echo !_tmp:^>=^^^>! 
exit /b 

:argParser 

    set comstr=-exec-commandline-workdir-host-user-pass-record 
    :nextShift 
    set /A shifter=shifter+1 
    echo %comstr% | find "%~1" > nul && (
     set _tmp=%~1 
     set !_tmp:-=!=%~2 
) 
    shift & shift 
    if %shifter% LSS 7 goto :nextShift 
    set _tmp= 
    set shifter= 
exit /b 

:echoHelp 

    echo %~n0 -exec executubale {-commandline command_line} { -workdir working_directory} 
    echo {-host remote_host {-user user {-pass password}}} {-record path_to_xml_output} 
    echo\ 
    echo localhost cant' be used as in -host variable 
    echo Examples: 
    echo %~n0 -exec "notepad" -workdir "c:/" -record "test.xml" -commandline "/A startpid.txt" 
    echo %~n0 -exec "cmd" -workdir "c:/" -record "test.xml" -host remoteHost -user User 
exit /b 

:err 
    if %1 EQU 2   (set errmsg=Access Denied) 
    if %1 EQU 3   (set errmsg=Insufficient Privilege) 
    if %1 EQU 8   (set errmsg=Unknown failure ^& echo Hint: Check if the executable and workdit exists or if command line parameters are correct.) 
    if %1 EQU 9   (set errmsg=Path Not Found ^& echo Hint: check if the workdir exists on the remote machine.) 
    if %1 EQU 21   (set errmsg=Invalid Parameter ^& echo Hint: Check executable path. Check if host and user are corect.) 
    if %1 EQU 1000  (set errmsg=Executable not defined.) 
    if "%errmsg:~0,1%" EQU "" (set errmsg=%output% ^& echo Hint: brackets, quotes or commas in the password may could breack the script.) 

    echo %errmsg% 
exit /b %1 
1
for /f "tokens=2 delims==;" %A in ('wmic process call create notepad ^| findstr /i /c:processid') do set pid=%A 
set pid=%pid:~1,4% 

这将启动使用WMIC的程序返回一个PID。 Findstr删除多余的行,第二行处理WMIC的uniciode。

相关问题