您的问题是 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 转换为 ASCII。我仍然将结果通过管道传输到 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