识别Powershell中正在执行的批处理文件的PID [英] Identify the PID of a Executing Batch File in Powershell

查看:79
本文介绍了识别Powershell中正在执行的批处理文件的PID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从PowerShell(v1.0)脚本中识别正在执行的批处理文件的P(进程)ID.有人可以建议这样做的方法吗?

I need to identify the P(rocess) ID of an executing batch file from a PowerShell (v1.0) script. Can anyone suggest a way of doing this?

谢谢MagicAndi.

Thanks, MagicAndi.

推荐答案

嗯,是否可行取决于您执行批处理文件的方式.

Well, whether that's possible depends on how you executed the batch file.

通常,您可能会发现的唯一方法是查看用于启动批处理的命令行.如果您在Windows资源管理器中双击一个批处理文件,则会看到类似

In general, the only way you could possibly find this out is to look at the command line used to start the batch. If you double-click a batch file in Windows Explorer you'll get a command line like

cmd /c ""C:\Users\Me\test.cmd" "

然后在Powershell中可以在Win32_Process上使用Get-WMIObject,其中包括命令行:

In Powershell you can then use Get-WMIObject on Win32_Process which includes the command line:

PS Home:\> gwmi Win32_Process | ? { $_.commandline -match "test\.cmd" } | ft commandline,processid -auto

commandline                             processid
-----------                             ---------
cmd /c ""C:\Users\Me\test.cmd" "             1028

但是,如果直接从命令提示符启动批处理,则无法从外部发现批处理正在运行以及由谁启动.

However, if you started the batch directly from a command prompt, then you have no way of externally finding out that a batch is running and who started it.

这篇关于识别Powershell中正在执行的批处理文件的PID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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