批处理脚本中使用wmic命令时文本混乱

4

我正在尝试运行批处理脚本,以获取基本的计算机信息,如CPU、RAM和活动网络适配器。以下是我的代码:

@Echo OFF

set newline=^& echo.

echo Manufacturer Information> test1.txt

systeminfo|findstr /c:"Host Name" /c:"OS Name" /c:"System Model:" /c:"System Type:" /c:"Total Physical Memory:" >>test1.txt

echo CPU Information:>> test1.txt

wmic cpu get Name /Format:list >> test1.txt

echo %newline%Process Information:>> test1.txt

wmic computersystem get NumberofProcessors /Format:list >> test1.txt

echo %newline%NIC Information:>> test1.txt

wmic nicconfig where "IPEnabled=TRUE" get ipaddress, macaddress,defaultipgateway /format:list >>test1.txt

输出结果如下:

Manufacturer Information Host Name: DK-IT OS Name:
Microsoft Windows 8.1 Single Language System Model:
Inspiron 7537 System Type: x64-based PC Total Physical Memory: 6,043 MB CPU Information:

 N a m e = I n t e l ( R )   C o r e ( T M )   i 5 - 4 2 0 0 U   C P U   @   1 . 6 0 G H z 



Process Information:


 N u m b e r O f P r o c e s s o r s = 1 



NIC Information:


 D e f a u l t I P G a t e w a y = { " 1 0 . 5 . 1 . 1 " }     I P A d d r e s s = { " 1 0 . 5 . 4 . 5 4 " , " f e 8 0 : : 1 0 e b : 1 d

2 d : 2 0 8 8 : 8 b a 1 " } M A C A d d r e s s = 0 C : 8 B : F D : 9 C : 8 0 : 4 7

 D e f a u l t I P G a t e w a y =     I P A d d r e s s = { " 1 9 2 . 1 6 8 . 1 9 9 . 1 " }     M A C A d d r e s s = 0 0 : 5 0 : 5 6 :

C 0 : 0 0 : 0 1

D e f a u l t I P G a t e w a y = I P A d d r e s s = { " 1 9 2 . 1 6 8 . 1 9 0 . 1 " , " f e 8 0 : : 6 4 b 2 : 2 a a a : e f 6 4 : f a 9 a " } M A C A d d r e s s = 0 0 : 5 0 : 5 6 : C 0 : 0 0 : 0 8

有人可以帮忙修改批处理文件输出的显示吗?

1
请参阅批处理:如何在解析输出时纠正变量覆盖行为以及此问题的答案中链接的其他主题,了解如何将wmic的Unicode输出(每个字符2个字节)转换为单字节编码文本,并在将输出写入文本文件之前使用单字节编码。 - Mofi
1个回答

5
您的问题是SYSTEMINFO生成ANSII输出(大部分命令均如此),而WMIC生成Unicode输出。两者不兼容。

以下是三种解决方案,均生成ANSII输出。

1)将WMIC输出导入MORE中

MORE将Unicode转换为ANSII。我还将该结果导入FINDSTR以消除空行。这种方法的唯一问题是,转换存在一个怪癖,导致WMIC输出每行末尾多出一个回车符(<CR><CR><LF>而不是 <CR><LF>)。

@echo OFF
>test1.txt (
  echo Manufacturer Information:
  systeminfo|findstr /c:"Host Name" /c:"OS Name" /c:"System Model:" /c:"System Type:" /c:"Total Physical Memory:"

  echo(
  echo CPU Information:
  wmic cpu get Name /Format:list | more | findstr .
  wmic computersystem get NumberofProcessors /Format:list | more | findstr .

  echo(
  echo NIC Information:
  wmic nicconfig where IPEnabled=TRUE get ipaddress, macaddress,defaultipgateway /format:list | more | findstr .
)


------------------------------

其余解决方案都已经正确格式化,没有额外的<CR>

2)将WMIC输出写入临时文件,然后使用TYPE

临时文件采用Unicode格式,TYPE可以正确地将Unicode转换为ANSII。我仍然将结果管道传输到FINDSTR以消除空白行。

@echo OFF
>test2.txt (
  echo Manufacturer Information:
  systeminfo|findstr /c:"Host Name" /c:"OS Name" /c:"System Model:" /c:"System Type:" /c:"Total Physical Memory:"

  echo(
  echo CPU Information:
  call :wmic cpu get Name /Format:list
  call :wmic computersystem get NumberofProcessors /Format:list

  echo(
  echo NIC Information:
  call :wmic nicconfig where IPEnabled=TRUE get ipaddress, macaddress,defaultipgateway /format:list
)
exit /b

:wmic
wmic %* >test.tmp
type test.tmp | findstr .
del test.tmp
exit /b


3) 通过两个 FOR /F 循环运行 WMIC。

第一个 FOR /F 将 WMIC 输出转换为 ANSII 格式,但每行末尾都有额外的 <CR>。第二个 FOR /F 去除不需要的尾部 <CR>。FOR /F 自动去除空白行。

@echo OFF
>test3.txt (
  echo Manufacturer Information:
  systeminfo|findstr /c:"Host Name" /c:"OS Name" /c:"System Model:" /c:"System Type:" /c:"Total Physical Memory:"

  echo(
  echo CPU Information:
  call :wmic cpu get Name /Format:list
  call :wmic computersystem get NumberofProcessors /Format:list

  echo(
  echo NIC Information:
  call :wmic nicconfig where IPEnabled=TRUE get ipaddress, macaddress,defaultipgateway /format:list
)
exit /b

:wmic
for /f "delims=" %%A in ('"wmic %*"') do for /f "delims=" %%B in ("%%A") do echo %%B
exit /b

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接