从运行 bat 脚本中获取 PID

Str*_*ear 3 pid batch-file

嗨,我正在尝试找到一种从 bat 脚本中获取我自己的 PID 的方法。我找到了这个:

title=mycmd
tasklist /v /fo csv | findstr /i "mycmd"
Run Code Online (Sandbox Code Playgroud)

输出:

"cmd.exe","15084","RDP-Tcp#5","2","2,768 K","Running","MEDIASERVER\Administrator
","0:00:00","Administrator: =mycmd"
Run Code Online (Sandbox Code Playgroud)

我如何将 PID 编号放入我的 bat 脚本中的变量中?

任何帮助,将不胜感激。

MC *_* ND 5

@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%
Run Code Online (Sandbox Code Playgroud)