windows - wmic 命令的批处理脚本中的文本乱码

标签 windows batch-file wmic

我正在尝试运行批处理脚本来获取基本的计算机信息,如 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

谁能帮忙修改批处理文件输出的 View ?

最佳答案

您的问题是 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

关于windows - wmic 命令的批处理脚本中的文本乱码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25599445/

相关文章:

windows - 如果我的文件没有更改,如何阻止 saltstack 中的 module.run?

c++ - OpenGL独占模式全屏

windows - 提高 Windows 批处理脚本性能

python - 如何在Python中隐藏 "No Instance(s) available"?

windows - 进程中的线程数是什么意思(wmic)

温控温度

PHP Cron 作业 Windows 和 Linux

c++ - TranslateColors() RGB 和 CMYK 的颜色范围

sql - 批处理 SQL 脚本中可以运行的插入次数是否有最大限制?

batch-file - 如何在批处理文件中压缩