Text garble in batch script for wmic command

后端 未结 1 1011
栀梦
栀梦 2020-11-29 11:58

I\'m trying to run the batch script to acquired the basic computer information like CPU,RAM and active Network card.Here is my code

@Echo OFF

set newline=^&         


        
相关标签:
1条回答
  • 2020-11-29 12:20

    Your problem is SYSTEMINFO produces ANSII output (as do most commands), but WMIC produces Unicode output. The two do not mix well.

    Below are three solutions that all produce ANSII output.

    1) Pipe the WMIC output to MORE

    MORE converts Unicode to ANSII. I also pipe that result to FINDSTR to eliminate blank lines. The only problem with this solution is that a quirk with the conversions causes the WMIC output to have an extra carriage return at the end of each line (<CR><CR><LF> instead of <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 .
    )
    


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

    The remaining solutions are all properly formatted, without any extra <CR>

    2) Write WMIC output to a temp file, followed by TYPE

    The temp file is in Unicode format, and TYPE properly converts the Unicode to ANSII. I still pipe the result to FINDSTR to eliminate blank lines.

    @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) Run WMIC through two FOR /F loops.

    The first FOR /F converts the WMIC output to ANSII, but it has the extra <CR> at the end of each line. The second FOR /F strips off the unwanted trailing <CR>. FOR /F automatically strips blank lines.

    @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
    
    0 讨论(0)
提交回复
热议问题