wmic冻结进程_如何找到一个WMIC进程的PID,并使用taskkill杀死它

本文档探讨了如何通过WMIC命令找到包含特定关键字的进程PID,并使用Taskkill命令杀死该进程。作者遇到的问题是PID变量末尾有一个换行符,导致任务无法正确结束。解决方案包括调整WMIC命令的输出格式,如使用CSV格式并跳过额外的行,或者在初始的FOR /F循环中添加另一个FOR循环来去除PID后面的换行符。
摘要由CSDN通过智能技术生成

I need to find a process PID with WMIC command, and then kill this process with taskkill. I've almost achieved that, but the only problem is there is a newline on the end of the PID variable.

So far i made this:

c:\patryk>for /F "skip=1 tokens=*" %a in ('wmic process where "CommandLine like '%sourceprocessor%' and name like '%java%'" get ProcessId') do taskkill /pid | set /p pid=%a

So this is looping through the output of wmic which contains 3 lines: title (ProcessId), found PID (a number) and an empty line. I'm skipping first line, because it's just title. Now i want to kill a process with pid found in second line. And there is a problem, that it has an newline mark at the end of the line, so whole command does not work.

Can anybody give me some advice how can i achieve that? How can i remove this newline mark?

解决方案for /F "skip=2 tokens=2 delims=," %a in (

'wmic process where " .... " get ProcessID^,Status /format:csv'

) do taskkill /pid %a

Now you have an output from wmic with an aditional line at the start (from here the skip), with fields separated with commas (the delim), and three fields included: the node (computername) that is automatically added, the processid (the second token) and a final status field that will not be used but allows us to retrieve the second token in the line without the ending CR

Or you can add an aditional for command to your initial line

for /f ... %a in ( ... ) do for %b in (%a) do taskkil /pid %b

This aditional for loop will remove the CR character from the retrieved wmic data that is in %a.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值