How to format batch command result on text file

后端 未结 4 1142
清歌不尽
清歌不尽 2020-12-07 06:31

I am retrieving computer information using the batch script below,

(
    systeminfo | findstr /c:\"Host Name\" /c:\"Domain\" /c:\"OS Name\" /c:\"OS Version\"         


        
相关标签:
4条回答
  • 2020-12-07 06:40

    This is a character encoding issue: systeminfo returns ASCII/ANSI text, but wmic returns Unicode text.

    Squashman already showed a way to convert the wmic output to ASCII/ANSI text in his answer, although I would write it a bit differently in order to avoid conversion artefacts like orphaned carriage-return (CR) characters:

    systeminfo | findstr /c:"Host Name" /c:"Domain" /c:"OS Name" /c:"OS Version" /c:"System Manufacturer" /c:"System Model" /c:"System type" /c:"Total Physical Memory" /c:"Available Physical Memory" > "getPCInfo.txt"
    for /F "delims=" %%H in ('wmic BIOS get SerialNumber /VALUE') do for /F "tokens=1* delims==" %%I in ("%%H") do >> "getPCInfo.txt" echo %%I:              %%J
    

    Anyway, here is another method using a temporary file:

    systeminfo | findstr /c:"Host Name" /c:"Domain" /c:"OS Name" /c:"OS Version" /c:"System Manufacturer" /c:"System Model" /c:"System type" /c:"Total Physical Memory" /c:"Available Physical Memory" > "getPCInfo.txt"
    wmic bios get serialnumber /VALUE > "getPCInfo.tmp"
    type "getPCInfo.tmp" >> "getPCInfo.txt"
    del "getPCInfo.tmp"
    

    This works only if the hosting cmd instance is not run in Unicode mode (see its /U option), but the default is ASCII/ANSI (/A) anyway.

    0 讨论(0)
  • 2020-12-07 06:45

    Here's an untested PowerShell version, only because of how long systeminfo takes:

    PCInfo.ps1

    $Properties = @{  
      Host_Name = (GWMI Win32_OperatingSystem).CSName
      OS_Name = (GWMI Win32_OperatingSystem).Caption
      OS_Version = GWMI Win32_OperatingSystem | % {$_.Version+' build '+$_.BuildNumber}
      System_Manufacturer = (GWMI Win32_ComputerSystem).Manufacturer
      System_Model = (GWMI Win32_Computersystem).Model
      BIOS_Version = (GWMI Win32_BIOS).SMBIOSBIOSVersion
      Total_Physical_Memory = GWMI Win32_PhysicalMemory | Measure Capacity -Sum | % {[String]$([Math]::Round(($_.Sum / 1MB),0))+' MB'}
      Available_Physical_Memory = GWMI Win32_OperatingSystem | % {[String]$([Math]::Round(($_.FreePhysicalMemory * 1KB / 1MB),0))+' MB'}
      Domain = GWMI Win32_Computersystem | % {$_.DNSHostName +'.'+$_.Domain}
      Serial_Number = (GWMI Win32_BIOS).SerialNumber}
    $ColumnWidth = ($Properties.Keys | Measure -Max Length).Maximum
    $Properties.GetEnumerator() | Sort Name| % {"{0,-$ColumnWidth}: {1}" -F $_.Key, $_.Value}
    

    To run it, change the paths as necessary and enter the following at the Command Prompt:

    PowerShell -NoP -ExecutionPolicy RemoteSigned -File "C:\Users\YHTAN\Desktop\PCInfo.ps1">"C:\Users\YHTAN\Desktop\PCInfo.txt"
    
    0 讨论(0)
  • 2020-12-07 06:56

    This should work for you.

    @echo off
    
    systeminfo | findstr /c:"Host Name" /c:"Domain" /c:"OS Name" /c:"OS Version" /c:"System Manufacturer" /c:"System Model" /c:"System type" /c:"Total Physical Memory" /c:"Available Physical Memory">"getPCinfo.txt"
    
    FOR /F "tokens=1,2 delims==" %%G IN ('wmic bios get serialnumber /value ^|find /I "serialnumber"') DO >>"getPCinfo.txt" echo %%G :   %%H
    
    0 讨论(0)
  • 2020-12-07 07:02

    I don't like overly long lines in scripts - you loose overview too fast.
    This batch uses findstr's default RegEx mode, anchors at line begin with /B
    and substitutes the spaces in the search strings with a dot.

    :: Q:\Test\2018\06\22\Get-PCInfo.cmd
    @Echo off
    FOR /F "tokens=1,2 delims==" %%A IN ('
      wmic bios get serialnumber /value ^|find /I "serialnumber"
    ') DO For %%C in (%%B) Do Set "BIOS=%%A:              %%C"
    
    Set "Find=Host.Name Domain OS.Name OS.Version System.Manufacturer"
    Set "Find=%Find% System.Model System.type Total.Physical.Memory"
    Set "Find=%Find% BIOS.Version Available.Physical.Memory"
    
    ( systeminfo | findstr /I /B "%Find%"
      Echo=%BIOS%
    ) >getPCinfo.txt
    

    Sample output:

    > type getPCinfo.txt
    Host Name:                 HOST
    OS Name:                   Microsoft Windows 10 Pro
    OS Version:                10.0.16299 N/A Build 16299
    System Manufacturer:       System manufacturer
    System Model:              System Product Name
    System Type:               x64-based PC
    BIOS Version:              American Megatrends Inc. 1408   , 09/21/2010
    Total Physical Memory:     24.566 MB
    Available Physical Memory: 14.753 MB
    Domain:                    Doamin
    SerialNumber:              Number
    
    0 讨论(0)
提交回复
热议问题