bat 获取服务器文件,获取在Bat文件中运行的exe的进程ID

本文详细阐述了如何在遇到WMIC过程调用创建命令中参数转义难题时,通过创建子进程和利用WMIC查询技巧来绕过限制。作者展示了如何通过一系列步骤获取并设置新的进程ID,适用于那些不能直接通过WMIC传递参数的情况。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

扩展foxidrive的答案显示OP的呼吁,需要参数:

set "exe=las2xyz.exe" set "arg1=[ flightpath 2 out 5 lasformat 1 target 0 FIXCLASS 1 step 20 unit *METRIC* fov 20.0 rollfix 1 sn_number *H68_038-003* lsystem *LIDAR_1* DESTSYS 144 minele -100.00 maxele 6000.00 hoff 0.00 eoff 0.00 noff 0.00 bootnr 13110201 leg 1]" set "arg2=C:\Users\Developer-One\Desktop\las2xyz_Data\131102_003243_GPE.sdc" set "arg3=\\192.168.0.102\agis\Macquarie_Barwon_1310\Area_01\sbet_038_13110201.out" set "arg4=131102_003243_cov" for /f "tokens=2 delims==; " %%A in ( 'wmic process call create '"%exe%" "%arg1%" ^, "%arg2%" "%arg3%" - "%arg4%"' ^| find "ProcessId"' ) do set "PID=%%A" echo "%PID%"

引用参数的内容几乎可以包含除双引号或逗号以外的任何内容。 如果在参数之间使用逗号,那么它必须被转义。

可能有另一种语法允许在参数中使用逗号,但是在参数内不允许使用括号。

那么如果你的命令行不能通过WMIC PROCESS CALL CREATE来传递,怎么办?

有一个解决方案。 但它不漂亮;-)我第一次在使用wmic的时候在转义字符串上发布这个

@echo off setlocal enableDelayedExpansion :: Get the PID and UID for this batch process call :getMyPID :: Initialize an existing PIDs list set "PIDs= " :: Get a list of all existing child processes, except for the :: child CMD.EXE process that was created by this FOR /F loop. :: This is necessary because the console session that this script :: is running in might have previously created a child process. for /f %%A in ( '2^>nul wmic process where "ParentProcessID=%myPID% and not CommandLine like '%%%%'" get ProcessID' ) do for %%B in (%%A) do set "PIDs=!PIDs!%%B " :: Create your new process as you normally would. :: For this demonstration, I will simply create a new CMD.EXE session start :: Get the PID of the newly created child process by getting all child PIDs, :: except for the child CMD.EXE process that was created by this FOR /F loop. :: Ignore any PID that already exists in the %PIDs% list. The only PID left :: is your new process. for /f %%A in ( '2^>nul wmic process where "ParentProcessID=%myPID% and not CommandLine like '%%%%'" get ProcessID' ) do for %%B in (%%A) do if "!PIDs: %%B =!" equ "!PIDs!" set "PID=%%B" :: At this point you could append the new PID to the PIDs list using :: set "PIDs=!PIDs!%PID% " :: Then you can repeat the steps of creating a new proces and getting the new PID :: :: But instead, I will simply show the result and exit echo new PID=%PID% exit /b :getMyPID setlocal disableDelayedExpansion :getLock :: Establish a nearly unique temp file name using %time% set "lock=%temp%\%~nx0.%time::=.%.lock" :: Transform the full path into a UID that can be used in a WMIC query set "uid=%lock:\=:b%" set "uid=%uid:,=:c%" set "uid=%uid:'=:q%" set "uid=%uid:_=:u%" setlocal enableDelayedExpansion set "uid=!uid:%%=:p!" endlocal & set "uid=%uid%" :: Establish an exclusive lock on the temp file :: If this fails, then loop back and try again until success :: This guaranees no two process will ever have the same UID 2>nul ( 9>"%lock%" ( %= The FOR /F loops creates a child CMD.EXE process which in turn invokes WMIC. =% %= The child CMD.EXE process contains the WMIC query with the UID in its command line. =% %= The WMIC query identifies the CMD.EXE process with the UID in the command line, =% %= and returns the parent process ID, which happens to be the PID for this batch script =% for /f "skip=1" %%A in ( 'wmic process where "name='cmd.exe' and CommandLine like '%%%%'" get ParentProcessID' ) do for %%B in (%%A) do set "PID=%%B" (call ) %= Guarantee success so we don't accidently loop again =% ))||goto :getLock :: Delete the temp file del "%lock%" 2>nul :: Return the result ( endlocal set "myPID=%PID%" set "UID=%uid%" ) exit /b

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值