没有属性名称的 WMIC 输出属性值

Paw*_*sos 6 environment-variables wmic cmd.exe

我正在输入类似的东西

Desktop>wmic environment where(name="PATH" and systemVariable=FALSE) get variableValue
VariableValue
xxx
Run Code Online (Sandbox Code Playgroud)

但我不想VariableValue进入输出。我想要简单的 getxxx 有可能吗?

Dav*_*ill 11

我不希望 VariableValue 进入输出。我只想得到 xxx 有可能吗?

使用批处理文件:

@echo off
setlocal
for /f "usebackq skip=1 tokens=*" %%i in (`wmic environment where ^(name^="PATH" and systemVariable^=FALSE^) get variableValue ^| findstr /r /v "^$"`) do echo %%i
endlocal
Run Code Online (Sandbox Code Playgroud)

使用命令行:

for /f "usebackq skip=1 tokens=*" %i in (`wmic environment where ^(name^="PATH" and systemVariable^=FALSE^) get variableValue ^| findstr /r /v "^$"`) do @echo %i
Run Code Online (Sandbox Code Playgroud)

笔记:

  • for /f循环wmic输出。
  • skip=1跳过标题行(包含VariableValue
  • findstr /r /v "^$"wmic输出中删除尾随的空行。

示例输出:

C:\Users\DavidPostill\AppData\Roaming\npm
Run Code Online (Sandbox Code Playgroud)

进一步阅读


小智 7

要省略标题行,只需将输出通过管道传递到more并告诉它省略第一行:

wmic environment where(name="PATH" and systemVariable=FALSE) get variableValue | more +1
Run Code Online (Sandbox Code Playgroud)

https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/more


小智 5

也许为时已晚,但我认为有一个稍微更优雅的解决方案。

wmic 允许您使用样式表来格式化输出。

考虑这个例子:

wmic os get OSArchitecture /format:csv
Run Code Online (Sandbox Code Playgroud)

输出是

Node,OSArchitecture
MY-COMPUTER,64bit
Run Code Online (Sandbox Code Playgroud)

通过参数,/format:csv您告诉 wmic 使用默认位于的 csv.xls 样式表%WINDIR%\wbem\en-US(替换en-Us为您的语言环境)。

现在是小魔法:您可以创建自己的 xsl,告诉 wmic 使用它并根据需要格式化输出

例如,创建一个样式表single-value-only.xsl

<?xml version="1.0"?>
<!-- Maybe you should refine this stylesheet a bit for a broader or production use but this basically works-->
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output encoding="utf-16" omit-xml-declaration="yes"/>
<xsl:param name="norefcomma"/>
<xsl:template match="/">
<xsl:value-of select="COMMAND/RESULTS[1]/CIM/INSTANCE[1]/PROPERTY/VALUE"/>
</xsl:template> 
</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)

并运行 wmic

wmic os get OSArchitecture /format:"C:\path\of\single-value-only.xsl"
Run Code Online (Sandbox Code Playgroud)

结果是

64bit
Run Code Online (Sandbox Code Playgroud)

如果您在批处理脚本中并希望将值放入变量 MY_VAR

for /f %%i "delims=" in (`wmic os get OSArchitecture /format:"C:\path\of\single-value-only.xsl"`) do set MY_VAR=%%i
Run Code Online (Sandbox Code Playgroud)