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

查看:1251
本文介绍了我不能在批处理文件中使用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 / Fdelims ==%% G IN('WMIC服务的地方(像显示名称^%% SQLSERVER2008 %% ^)')DO回声%%摹

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

推荐答案

首先,你需要得到正确的WMIC命令,那么你需要得到它的内工作的命令。

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输出为单code和它与FOR / F一个奇怪的互动 - 以某种方式说FOR / F分析包括在最后一个额外的回车每一行。不需要的回车被包括在每个FOR / F迭代。呸:(

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和令牌来获得所需的列(S)。在你的情况,国家列名之后出现,所以它是一个不错的人选。

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天全站免登陆