2017-06-02 1 views
3

Bonjour, j'essaie de trouver un moyen d'obtenir mon propre PID à partir d'un script chauve-souris. J'ai trouvé ceci:Obtention d'un PID à partir d'un script de chauve-souris

title=mycmd 
tasklist /v /fo csv | findstr /i "mycmd" 

qui délivre:

"cmd.exe","15084","RDP-Tcp#5","2","2,768 K","Running","MEDIASERVER\Administrator 
","0:00:00","Administrator: =mycmd" 

comment pourrais-je obtenir le numéro PID dans une variable dans mon script de chauve-souris?

toute aide serait appréciée.

Répondre

3

essayer avec getcmdpid, donc vous aurez pas besoin de changer le titre:

call getCmdPID.bat 
echo %errorlevel% 

faire avec tasklist vous aurez besoin pour la boucle pour traiter la sortie:

title mycmd 
for /f "tokens=2 delims=," %%a in (
    'tasklist /v /fo csv ^| findstr /i "mycmd"' 
) do (
    set "mypid=%%~a" 
) 
echo %mypid% 

vérifier aussi ce fil: http://www.dostips.com/forum/viewtopic.php?t=6133

+0

acclame, fonctionne parfaitement :) – StrayanDropbear

4
@echo off 
    setlocal enableextensions disabledelayedexpansion 

    rem Prepare a temporary file reference where to send the wmic output 
    for %%t in ("%temp%\%~nx0.%random%%random%%random%%random%%random%.tmp") do > "%%~ft" (

     rem Call wmic to retrieve its own parent process ID, that is, our cmd instance 
     wmic process where "name='wmic.exe' and commandline like '%%_%random%%random%%random%_%%'" get parentprocessid 

     rem Read the PID from the generated temporary file 
     for /f "skip=1" %%a in ('type "%%~ft"') do set "processID=%%a" 

     rem And remove the temporary file 
    ) & 2>nul del /q "%%~ft" 

    echo %processID%