gpt4 book ai didi

batch-file - 通过运行 bat 脚本获取 PID

转载 作者:行者123 更新时间:2023-12-04 00:00:26 29 4
gpt4 key购买 nike

您好,我正在尝试从 bat 脚本中获取我自己的 PID。我发现了这个:

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

输出:

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

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

任何帮助将不胜感激。

最佳答案

@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%

关于batch-file - 通过运行 bat 脚本获取 PID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44330587/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com