如果服务名称之间有空格,则无法获取服务名称

时间:2017-03-06 11:53:10

标签: batch-file

For /F "Skip=1 Delims=" %%A In (
    'wmic path win32_service where "name like "BMC Remedy Action%%"" get Name ^| findstr /V /R "^$"'
) do For /F "Delims=" %%B in ("%%A") Do Call :Sub %%B

:Sub
Set "Service=%*"
echo Service Name: %Service%

这给了我空的结果。

我无法使用以下内容,因为我有两个以上以BMC开头的服务。

For /F "Skip=1 Delims=" %%A In (
    'wmic path win32_service where "name like "BMC%%"" get Name ^| findstr /V /R "^$"'
) do For /F "Delims=" %%B in ("%%A") Do Call :Sub %%B

:Sub
Set "Service=%*"
echo Service Name: %Service%

请帮助!

2 个答案:

答案 0 :(得分:0)

有两个问题.1)您在FOR / F表达式中使用两个双引号2)您正在尝试匹配服务的DisplayName而不是名称。

试试这样:

For /F "useback Skip=1 Delims=" %%A In (
    `wmic path win32_service where 'DisplayName like "BMC Remedy Action%%"' get Name ^| findstr /V /R "^$"`
) do For /F "Delims=" %%B in ("%%A") Do Call :Sub %%B

答案 1 :(得分:0)

以下是我可能会这样做的方式,(我已将Name更改为DisplayName,因为您似乎使用了错误的内容。)

@Echo Off
For /F "Skip=1 Delims=" %%A In (
    '"WMIC Service Where (DisplayName Like 'BMC Remedy Action%%') Get Name"'
) Do For /F "Delims=" %%B In ("%%A") Do Set "Service=%%B"
Echo(%Service%
Timeout -1
GoTo :EOF

如果你确实需要从字符串的末尾删除填充的空格:

@Echo Off
For /F "Skip=1 Delims=" %%A In (
    '"WMIC Service Where (DisplayName Like 'BMC Remedy Action%%') Get Name"'
) Do For /F "Delims=" %%B In ("%%A") Do Call :Sub %%B
Echo("%Service%"
Timeout -1
GoTo :EOF

:Sub
Set "Service=%*"
GoTo :EOF