使用wmic将参数传递给可执行文件 [英] Getting the arguments passed to a executable using wmic

查看:237
本文介绍了使用wmic将参数传递给可执行文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图获取由另一个程序启动的可执行文件的命令行参数。

I am trying to get commandline arguments of an executable which was launched by another program.

我尝试了这个答案,但是我听不懂语法:(

I tried the command mentioned in this answer, but I can't understand the syntax :(

我试图获取的命令行参数一个进程,我有PID&进程名,在这种情况下,我尝试获取ping命令的参数,该命令用于测试命令...

I am trying to get the commandline arguments of an process, I have the PID & the process name, In this case I am trying get arguments of an ping command which I am using to test the command...

在此先感谢:)

推荐答案

尝试一下:

wmic process where "name='ping.exe'" get commandline /format:list

或者如果您希望按PID查询:

Or if you prefer to query by PID:

wmic process where "processid='NNNN'" get commandline /format:list

wmic 使用查询称为WQL的语言,类似于SQL。您可以执行通配符之类的操作,例如 wmic进程,其中名称如'ping%'将获得命令行(但请确保将 %% 在批处理脚本中),更改输出样式(列表,csv甚至是html)和其他魔术。有关更多信息,请参见命令行中的 wmic /?

wmic uses a query language called WQL, which is similar to SQL. You can do wildcard stuff like wmic process where "name like 'ping%'" get commandline (but be sure to double the %% within a batch script), vary the output style (list, csv, even html), and other magic. See wmic /? from a command line for more info.

如果要捕获任何命令到变量的输出,请使用 for / f 循环。在cmd控制台中帮助以获取更多信息。在cmd控制台中尝试以下操作:

If you want to capture the output of any command to a variable, use a for /f loop. help for in a cmd console for more info. Try this in a cmd console:

for /f "delims=" %I in ('wmic process where "name='ping.exe'" get commandline /format:list ^| find "="') do set "%I"

您会发现确实有些奇怪。该命令的输出将类似于以下内容:

You'll notice something very odd indeed. The output of that command will be similar to this:


\Users\username> set CommandLine = ping- n 60本地主机

在行的开始处打印右引号!那不是很奇怪吗?那是因为WMI查询结果是用UCS-2 LE而不是ANSI编码的。

The closing quotation mark gets printed at the beginning of the line! Isn't that weird? That's because WMI query results are encoded in UCS-2 LE, not ANSI.

我要使用的一种解决方法是使用 / format:csv 并在查询中添加一次性列。

One workaround I like to use is to use /format:csv and add a disposable column to the query.

从批处理脚本中:

for /f "tokens=2 delims=," %%I in (
    'wmic process where "name='ping.exe'" get commandline^,status /format:csv'
) do set "commandline=%%I"

...这样一来,您就不会在变量中捕获任何不可见的背信弃义。

... and that way you won't capture any invisible treachery to your variable.

这篇关于使用wmic将参数传递给可执行文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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