2016-05-23 89 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。

相關問題