如何在 WMIC 中像字符串一样转义逗号 [英] How do I escape comma in WMIC inside like string

查看:30
本文介绍了如何在 WMIC 中像字符串一样转义逗号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够运行如下查询:

I wish to be able to run a query like the following:

wmic path Win32_Service where "DisplayName like 'FooBarService % (X, Y)'" get *

但是,由于 like 字符串中的逗号,它不起作用.我得到的错误是无效动词".我尝试用反斜杠转义它,我尝试使用括号转义它,因为下划线是要转义的,两者都导致无效动词".错误.

But, it doesn't work because of the comma inside the like string. The error I get is "Invalid Verb." I tried escaping it with a backslash, and I tried escaping it using brackets as underscores are meant to be escaped, and both resulted in the "Invalid Verb." error.

作为一个不太理想的解决方法,我可以用下划线替换逗号,它可以工作,但下划线将匹配任何单个字符而不仅仅是逗号,所以我宁愿找到一种方法来转义逗号.

As a less-than-ideal workaround, I can replace the commas with underscores, and it works, but the underscore will match any single character rather than just the comma, so I'd rather find a way to escape the commas.

有没有办法像这个例子中那样转义逗号?

Is there a way to escape the comma like in this example?

推荐答案

我发现在 like 子句中包含逗号的一种方法是将整个 where括号中的表达式.不幸的是,我还发现这意味着我不能同时在字符串中包含关闭括号(但打开括号是可以的).我尝试了 /trace:on 选项,看看幕后发生了什么,它帮助我找到了程序接受的一些东西:

One way I have found to include a comma in the like clause is to place the entire where expression in parentheses. Unfortunately, I also found that this means I cannot include a close paren in the string at the same time (but an open paren is okay). I experimented with the /trace:on option to see what was going on under the covers a little bit and it helped me find a couple things the program accepts:

这是我使用逗号的示例,但它显然不能包含关闭括号:

Here is an example I got to work with a comma, but it apparently cannot contain a close paren:

<代码>C:\>wmic/trace:on path Win32_Service where (Description like '%(%, %') get DisplayName

这是一个我必须同时使用左括号和右括号的示例,但显然它不能包含逗号(显然,这与您的原始示例非常相似):

And here is an example I got to work with both open and close parentheses, but apparently it cannot contain a comma (obviously, this is quite similar to your original example):

<代码>C:\>wmic/trace:on path Win32_Service where "Description like '%(TAPI)%'" get DisplayName

似乎解析器不够复杂,无法处理这些情况,但是通过跟踪,您可以看到它使用的 WMI Win32 函数,因此也许您可以编写自己的程序来直接使用这些函数.我认为 IWbemServices::ExecQuery 能够满足您的需求.

It seems like the parser just isn't complex enough to handle these cases, but with tracing on, you can see the WMI Win32 functions that it uses, so maybe you could write your own program that uses the functions directly. I think IWbemServices::ExecQuery is capable of what you're looking to do.

这篇关于如何在 WMIC 中像字符串一样转义逗号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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