我无法获得正确的语法以在批处理文件中使用WMIC [英] I can't get the right syntax to use WMIC in batch file

查看:51
本文介绍了我无法获得正确的语法以在批处理文件中使用WMIC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我要运行的命令.我想获取我的SQL Server 2008实例的服务名称

Here's the command I'm trying to run. I wanted to get the service name for my SQL Server 2008 instance

FOR/F"delims ==" %% G IN('wmic service where(displayname like ^"%% sqlserver2008 %% ^")')'回显%% G

FOR /F "delims==" %%G IN ('wmic service where (displayname like ^"%%sqlserver2008%%^")') DO echo %%G

推荐答案

首先,您需要获取正确的WMIC命令,然后才能使其在FOR命令中运行.

First you need to get the correct WMIC command, then you need to get it to work within a FOR command.

您要查找的基本WMIC命令是:

The basic WMIC command you are looking for is:

wmic service where "displayName like '%%sqlserver2008%%'" get name

现在,FOR/F有一些并发症.WMIC输出采用unicode,并且与FOR/F具有怪异的交互-某种程度上,FOR/F解析的每一行在末尾都包含一个额外的回车符.不需要的回车符包含在每个FOR/F迭代中.uck:(

Now there are a few complications with FOR /F. WMIC output is in unicode and it has a weird interaction with FOR /F - somehow every line that FOR /F parses includes an extra carriage return at the end. The unwanted carriage return is included in each FOR /F iteration. Yuck :(

一种有效的解决方案是请求CSV格式,并在您感兴趣的列之外至少增加一个多余的列.然后,您可以设置DELIMS和TOKENS来获取所需的列.就您而言,状态"列会出现在名称"之后,因此是不错的选择.

One effective solution is to request CSV format, with at least one extra unwanted column beyond the column you are interested in. Then you can set DELIMS and TOKENS to get the desired column(s). In your case, the State column appears after the Name, so it is a good candidate.

WMIC/CSV格式在前面包括一个额外的节点"列.

WMIC /CSV format includes an extra Node column at the front.

WMIC/CSV格式在顶部包含一个空白行和一个标题行,可以使用SKIP = 2跳过该行.

WMIC /CSV format includes a blank line and a header line at the top that can be skipped by using SKIP=2.

这是一个功能齐全的FOR/F解决方案.

Here is a fully functioning FOR /F solution.

for /f "skip=2 tokens=2 delims=," %%A in (
  'wmic service where "displayName like '%%sqlserver2008%%'" get name^,state /format:csv'
) do echo %%A

这篇关于我无法获得正确的语法以在批处理文件中使用WMIC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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