如何找到一个WMIC进程的PID,并使用taskkill杀死它 [英] How to find a process pid with wmic and kill it with taskkill

查看:3266
本文介绍了如何找到一个WMIC进程的PID,并使用taskkill杀死它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要找到WMIC命令进程的PID,然后杀死TASKKILL这个过程。我已经几乎实现了这一点,但唯一的问题是没有对P​​ID变量的末尾换行。
到目前为止,我做了这个:

  C:\\ patryk> FOR / F跳过= 1令牌= *%一个在('WMIC过程,其中的CommandLine LIKE'%sourceprocessor%'和名LIKE'%的java %'获得的ProcessID')做的taskkill / PID |集/ p PID =%一

因此​​,这是通过包含3行WMIC的输出循环:标题(进程ID),发现PID(数字)和一个空行。我跳过第一行,因为它只是头衔。现在我想杀死一个PID过程二线找到。而且还有一个问题,即它在该行的最后一个换行标志,所以整个命令不起作用。
任何人都可以给我一些意见,我怎样才能做到这一点?如何删除这个新行标志?


解决方案

  FOR / F跳过= 2令牌= 2 delims =%在一(
  WMIC过程,其中......得到的ProcessID ^,状态/格式:CSV
)做的taskkill / PID%一

现在你有一个从 WMIC 与开始的aditional的线路输出(从这里跳跃),用逗号(该DELIM)分隔的字段,以及三个领域包括:节点(计算机名)时自动增加,进程ID(第二令牌),并且将不被使用,但可以让我们来检索行中的第二令牌没有结束的CR最终状态字段

或者你也可以到你的初始行添加aditional的命令

  FOR / F。%的在(...)在(%A)%B做taskkil / PID%B做

这aditional的循环将从以%检索WMIC数据删除CR字符。

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.

这篇关于如何找到一个WMIC进程的PID,并使用taskkill杀死它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆