WMIC上的无效动词获取变量

时间:2019-05-29 15:36:10

标签: batch-file scripting delimiter wmic

我正在尝试仅将主硬盘序列号获取一个变量,以供以后echo使用。

我已经使用了它并且有效:

for /f "tokens=2 delims== " %%k in ('WMIC path win32_physicalmedia get serialnumber /value') do (for /f "delims=" %%l in ("%%k") do set "SerialNumber=%%l")

但是,它只提供最后一个序列号,而我想获取主序列号。

如果我使用了此命令,

"wmic path win32_physicalmedia where tag='\\\\.\\PHYSICALDRIVE0' get serialnumber /value" 

然后我只得到所需的序列号。

问题是当我尝试修改工作代码时,在运行bat时出现错误:

  

无效动词。

尝试上班:

for /f "tokens=2 delims== " %%k in ('wmic path win32_physicalmedia where tag='\\\\.\\PHYSICALDRIVE0' get serialnumber /value') do (for /f "delims=" %%l in ("%%k") do set SerialNumber=%%l)

错误:

  

无效动词

如果我只跑:

wmic path win32_physicalmedia where tag='\\\\.\\PHYSICALDRIVE0' get serialnumber /value in CMD

我得到以下输出:

SerialNumber=     WD-WCC2E1XJADRT

所以我知道该命令有效。

是否插入空格都没有关系,我只想 = 之后的内容。我在具有WMIC支持的WINPE中执行此操作,因此无法选择Powershell。

总脚本

@echo OFF
setlocal
for %%v in (a b c d e f g h i j k l m n o p q r s t u v w x y z) do if exist %%v:\drives.txt set w=%%v

for /f "tokens=2 delims== " %%k in ('wmic path win32_physicalmedia where tag='\\\\.\\PHYSICALDRIVE0' get serialnumber /value)') do (for /f "delims=" %%l in ("%%k") do set SerialNumber=%%l)

for /F "tokens=2" %%i in ('date /t') do set "Date=%%i"
echo %SerialNumber%,%Date% >> %w%:\drives.txt

2 个答案:

答案 0 :(得分:3)

如果您将命令修改为此:

WMIC Path Win32_PhysicalMedia Where "Tag='\\\\.\\PHYSICALDRIVE0'" Get SerialNumber /Value

= 将受到保护。

像这样尝试:

@Echo Off
For /F "Tokens=1*Delims==" %%A in ('
    WMIC Path Win32_PhysicalMedia Where "Tag='\\\\.\\PHYSICALDRIVE0'" Get SerialNumber /Value 2^>Nul
') Do For %%C In (%%B) Do Set "SerialNumber=%%C"
Echo [%SerialNumber%] & Pause

我添加了最后一行只是为了显示所有返回的变量和值。

答案 1 :(得分:1)

在Powershell中非常容易。您可以将Powershell 6(pwsh)的zip解压缩到用于Winpe的文件共享上。 https://github.com/PowerShell/PowerShell/releases/download/v6.2.1/PowerShell-6.2.1-win-x64.zip

library(plotly)
library(tidyr) ## to melt data.frame

## create long-format data.frame
df <- data.frame(
        Group        = c("2016-11", "2016-12", "2017-01", "2017-02", "2017-03"),
        DidNot       = c(3, 0.8, 1.5, 0.8, 1.7),
        Exceeded     = c(45, 50.6, 32.3, 49.5, 55.6),
        Met          = c(51.2, 48.5, 66.2, 49.5, 42.4),
        Unacceptable = c(0.7, 0, 0, 0.1, 0.2)
     ) %>%
     gather(key = "criterion", value = "measurement", -Group)

## specify color maps
colors <- c("red", "gold", "blue", "green")
names(colors) <- c("Unacceptable", "DidNot", "Met", "Exceeded")

## create plot
plot_ly(df, x = ~Group) %>%
  add_lines(y = ~measurement, color = ~criterion, colors = colors) %>%
  layout(xaxis = list(title = "Time"), 
         yaxis = list(title = "Percent")
  )

这是另一种.bat方式('%'是sql通配符,您必须在.bat文件中将它们加倍):

$SerialNumber = Get-CimInstance win32_physicalmedia | 
  Where tag -Match physicaldrive0 | Select -Expand serialnumber